1

I have ImageView. I change margins and size of ImageVew on set new bitmap. For example, user click button, I set bitmap to ImageView and change params (this is code from my own view):

RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
p.leftMargin = (int) ((pw - lw) / 2f);
_topMargin = (int) ((ph - lh) / 2f);
lp.topMargin = _topMargin;
lp.width = lw;
lp.height = lh;
requestLayout();

All work. But also I want change params, if change size parent view of my ImageView. If I try use code on change params from this event - nothing todo (ImageView don't change params).

How I can fix this issue?

I see now one way: on change parent size, I don't change ImageView params, only set some internal boolean flag. After finish parent changing, I change my ImageView params. But I don't know, what parent event I need watch, after which I can change my ImageView params.

My solution.

I use this method.

Community
  • 1
  • 1
Yura Shinkarev
  • 5,134
  • 7
  • 34
  • 57

1 Answers1

3
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
lp.leftMargin = (int) ((pw - lw) / 2f);
_topMargin = (int) ((ph - lh) / 2f);
lp.topMargin = _topMargin;
lp.width = lw;
lp.height = lh;
setLayoutParams(lp);//<---set layout params
requestLayout();

After changing layout params you need to set it back.

vipul mittal
  • 17,343
  • 3
  • 41
  • 44