How to change the ImageView size at runtime within FrameLayout

喜欢而已 提交于 2020-01-03 05:54:10

问题


I have one ImageView inside FrameLayout. I am increasing the size of FrameLayout programmatically but ImageView size is not changing. I have set the ImageView paramas as MatchParent. Please Help me how to increase the size of image within frameLayout?

here I have added the image into FrameLayout:

img_pinch=new ImageView(getApplication());
img_pinch.setImageBitmap(m_bitmap);
img_pinch.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

FrameLayout fm=new FrameLayout(getApplication());
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(400,300);

fm.addView(img_pinch,params);

below is the code to change the size of frameLayout:

int x = (int) temp_img_logo.getWidth();
int y = (int) temp_img_logo.getHeight();

temp_img_logo.setLayoutParams(new android.widget.AbsoluteLayout.LayoutParams(x+5,y+5,(int)temp_img_logo.getX(),(int)temp_img_logo.getY()));

temp_img_logo.invalidate();

temp_img_logo is the instance of FrameLayout.


回答1:


You need to re-assign the imageview in order to increase the size accordingly , Try this,

At the begining ,

img_pinch=new ImageView(getApplication());
img_pinch.setImageBitmap(m_bitmap);
img_pinch.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

FrameLayout fm=new FrameLayout(getApplication());
FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(400,300);
fm.addView(img_pinch,params);

When you need to increase the size of frame layout ,

temp_img_logo .removeAllViews();

temp_img_logo .setLayoutParams(new LayoutParams(400, 400)); // 400 will be use as example
img_pinch.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
temp_img_logo .addView(img_pinch);
temp_img_logo .invalidate();


来源:https://stackoverflow.com/questions/29456390/how-to-change-the-imageview-size-at-runtime-within-framelayout

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!