ImageView ScaleType.FIT_XY not working with setImageDrawable

1.5k Views Asked by At

I need to copy an ImageView in my application and change it's size whenever the user wants. What I do is here:

public void testClick(View view)
{
    ImageView im = new ImageView(this);
    im.setImageDrawable(((ImageView) view).getDrawable());
    Point size = getSize();//size of the window
    al.addView(im, new AbsoluteLayout.LayoutParams(view.getWidth(), view.getHeight(), size.x / 2, size.y / 2));
    im.setScaleType(ImageView.ScaleType.FIT_XY);
    im.setOnTouchListener(touchListener);
}
public void changeSize()
{
   ...
   view.setLayoutParams(new AbsoluteLayout.LayoutParams(width + deltaX, height + deltaY, params.x, params.y));
}

It doesn't work! means when I increase the size of the view, the image inside is not stretched. But the weird thing happens when I change the line im.setImageDrawable(((ImageView) view).getDrawable()); with im.setImageResource(R.drawable.my_image); It works! any idea?

I do not want to use setImageResource because I need to copy the drawable of the clicked view, not a static one from resources.

Edit: When I use it in Galaxy Note 6.(I think it is Android 5) it works. but on my Android 4.1.2 device, it doesn't!


Edit2: I used

BitmapDrawable bd=(BitmapDrawable)((ImageView) view).getDrawable();
im.setImageBitmap(bd.getBitmap());

and it workd!

1

There are 1 best solutions below

4
On

To fix your problem, use below code to change your ImageView size

mImageView.getLayoutParams().width = myWidth;
mImageView.getLayoutParams().height = myHeight;

And then call

mImageView.requestLayout();

to have the size change take effect.

I believe this should solve your problem.