1

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!

Feri
  • 1,071
  • 7
  • 19
  • try calling view.invalidate() after changing size. – David Heisnam Aug 20 '15 at 19:00
  • @DavidH it didn't work – Feri Aug 20 '15 at 19:11
  • another thing you could try would be using setImageBitmap() in place of setImageDrawable(), as I believe setImageResource() is deprecated. – David Heisnam Aug 20 '15 at 19:23
  • @DavidH I use setImageDrawable because I want to copy the content of the source ImageView and it is available only in Drawable. And I do not think setImageResource is deprecated because Android Studio has not told me anything... – Feri Aug 20 '15 at 19:29
  • Oh yeah, it's not deprecated but see this quote from android docs for setImageResource() "This does Bitmap reading and decoding on the UI thread, which can cause a latency hiccup. If that's a concern, consider using setImageDrawable(android.graphics.drawable.Drawable) or setImageBitmap(android.graphics.Bitmap) and BitmapFactory instead." – David Heisnam Aug 20 '15 at 19:33
  • @DavidH didn't solve this problem, but was a very useful information anyway :) – Feri Aug 20 '15 at 19:47
  • @DavidH your comment on using setImageDrawable did work! I think drawable object is immutable and because of this, it didn't work. – Feri Aug 20 '15 at 20:35
  • Oh that's cool! Good to know you've managed to solve it. – David Heisnam Aug 20 '15 at 20:39
  • You can mutate drawable by using getDrawable().mutate(). That was was my first suggestion but I deleted it. You could also try it without using setImageBitmap() if you're looking for a cleaner solution – David Heisnam Aug 20 '15 at 20:41
  • I tried it, but it didn't! it's really strange. Also, you can post your answer so that i can mark it answerd if you want. – Feri Aug 20 '15 at 20:45
  • It's alright. All I've been doing is mostly just suggestions and not a sure solution. Anyway, I'm glad that it finally works. It really is a strange case, tbh. Good luck! – David Heisnam Aug 20 '15 at 20:47

1 Answers1

1

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.

David Heisnam
  • 2,463
  • 1
  • 21
  • 32
  • it didn't! actually, event without re-sizing, my code doesn't work. But it is supposed to. because i use a big size to add it to the AbsoluteLayout. – Feri Aug 20 '15 at 20:20
  • @Feri at this point, it'll help to know if you're using a custom Drawable class or anything that you haven't included in the post, but might be related. – David Heisnam Aug 20 '15 at 20:24
  • no!all of the related stuff are included! You know, it copies the current drawable in the source ImageView(if it is scaled, it copies exactly the scaled image) But after that there is no change. – Feri Aug 20 '15 at 20:28
  • @Feri That means the simplest solution could be using mutate() while getting the Drawable like so- im.setImageDrawable((ImageView)view.getDrawable().mutate()). You could use this without the setImageBitmap() – David Heisnam Aug 20 '15 at 20:45