问题
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