How to get image from ImageView and set it to other ImageView?
Asked Answered
C

3

6

I have two ImageViews as belows :

        <ImageView android:id="@+id/image1"
          android:layout_width="match_parent"  
          android:layout_height="match_parent"  
          android:src="@drawable/ic_launcher" />

        <ImageView android:id="@+id/image2"
          android:layout_width="match_parent"  
          android:layout_height="match_parent"  
          android:adjustViewBounds="true" 
          android:scaleType="fitCenter"
           />

the first imageView displays image in its actual size, the second ImageView should displays image in fitCenter size.

I have been trying this code :

    ImageView img1 = (ImageView) findViewById(R.id.image1);
    ImageView img2 = (ImageView) findViewById(R.id.image2);

    img2.setImageDrawable(img1.getDrawable());

but the second ImageView just displays image as the first one. Yes, in its actual size.

Can anyone help to find the solution?

Thanks

Calamondin answered 24/3, 2015 at 0:43 Comment(0)
W
8

You can try something like this:

ImageView img2 = (ImageView) findViewById(R.id.image2);
img2.setScaleType(ImageView.ScaleType.CENTER_CROP);
img2.setImageDrawable(img1.getDrawable());
Whaleboat answered 24/3, 2015 at 7:42 Comment(0)
C
0

DOn't use the same drawable like that. Drawables have bounds, and doing this they'll share those bounds. You need to make a copy of the Drawable, and set that as the drawable for img2.

Drawable clone = drawable.getConstantState().newDrawable();

will make a copy of the drawable.

Cobble answered 24/3, 2015 at 0:47 Comment(0)
L
0

Using Glide lib. it can help you

ImageView imV1 = (ImageView) findViewById(R.id.im_v1);
ImageView imV2 = (ImageView) findViewById(R.id.im_v2);
Glide.with(getActivity()).load("https://www.google.es/images/srpr/logo11w.png")
            .thumbnail(0.5f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(imV1);
 Drawable d = ((ImageView) imV1).getDrawable();
 Bitmap bmp =  ((GlideBitmapDrawable)d.getCurrent()).getBitmap();
imV2.setImageBitmap(bmp);
Lyle answered 6/9, 2016 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.