My Problem
Suppose I have a bitmap - call it bmap
with dimensions 100x75
(bmap.getWidth() x bmap.getHeight()
)
Suppose I have a rectangle, rect
, sitting at point on the phone screen (x,y), with dimensions 500x350
(width x height
)
How might I write a piece of code, which centers this bitmap within the bounding rect.
Note: Because I am using ContraintLayout
there is not concept of parents or relativity.
Additionally, I would like a scale
variable in the range (0,1]
, which scales bmap
but it maintains its position in the center of the bounding rectangle rect
.
My horrible attempt:
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);
This gives the following result (with scale = 1
for the green circle and scale = 0.6
for the red circle
Any ideas? I'm really stuck.