Create Dynamo Rule

I am trying to dynamically resize an image in relative layout using code

int height = v.getHeight(); int width = v.getWidth(); height += 50; width += 50; RelativeLayout.LayoutParams layout = new RelativeLayout.LayoutParams(height, width); layout.setMargins(200, 200, 200, 200); layout.addRule(RelativeLayout.CENTER_IN_PARENT); v.setLayoutParams(layout); 

Here v is the view (the image in this case). The rule works fine. The image is placed in the center, but I want the image to be placed where it is. That is, if the image is in the place (100 100), its size should be increased, and the image should be placed in the same place or in the place where I need to place it. Can anyone suggest me please.

0
source share
1 answer

What you ask for cannot be done simply by creating rules. You need to do extra work for such alignment.

1) use addRule to align the parent left and align the parent top, then set the left and top margins to align the image to any position where you want 100, 100 or 200, 50, etc., after which you can resize images without repositioning.

2) if you save the image according to CENTER_IN_PARENT, then the image size is a bit typical. you need to enlarge the left and top pad to fit the larger size to align the image in the center.

Suppose the image size is 100x100 and the new size is 150x150
Now you need to set the layout parameters in width x height to 200x200 and set the left and top fill of the image to 50.

I hope its clear

0
source

Source: https://habr.com/ru/post/1343414/


All Articles