【发布时间】:2018-07-12 23:21:53
【问题描述】:
我的问题
假设我有一个位图 - 称它为 bmap,尺寸为 100x75 (bmap.getWidth() x bmap.getHeight())
假设我有一个矩形rect,位于手机屏幕上的某个点 (x,y),尺寸为 500x350 (width x height)
我如何编写一段代码,使该位图在边界矩形内居中。
注意:因为我使用的是ContraintLayout,所以没有父母或相对论的概念。
此外,我想要(0,1] 范围内的scale 变量,它可以缩放bmap,但它的位置保持在边界矩形rect 的中心。
我的可怕尝试:
ImageView imgView = new ImageView(this.context);
imgView.setMaxWidth((int)(width*scale));
imgView.setMinimumWidth((int)(width*scale));
imgView.setMaxHeight((int)(height*scale));
imgView.setMinimumHeight((int)(height*scale));
imgView.setImageBitmap(bmap);
imgView.setX(x+((width-bmap.getWidth())/2));
imgView.setY(y+((height-bmap.getHeight())/2));
imgView.setAdjustViewBounds(true);
constraintLayout.addView(imgView);
这给出了以下结果(scale = 1 代表绿色圆圈,scale = 0.6 代表红色圆圈
有什么想法吗?我真的被困住了。
【问题讨论】:
-
ConstraintLayout 里面的圆和矩形都在吗?
-
如何以及何时获得宽度、高度、x 和 y 值?
-
圆形和矩形都在 ConstraintLayout 中。宽度和高度是从矩形获得的。 x 和 y 是从矩形中获得的
-
我从来没有这样做过,没有
标签: android bitmap imageview android-constraintlayout