0

If it possible to write ImageView to another ImageView?

For example:

        ImageView image1= (ImageView)findByVie(R.id.image1)
        ImageView image2= (ImageView)findByVie(R.id.image2)

        Glide.with(context).load(url).into(image1);

    image2=image1 
// I want to write image from image 1 
//to image 2 without loading it again from internet.
Expiredmind
  • 788
  • 1
  • 8
  • 29

3 Answers3

1

Use this:

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

Glide.with(context).load(url).into(image1);
image2.setImageDrawable(image1.getDrawable();

However, with the way Glide works, it caches images it has loaded so calling Glide.with(context).load(url).into(image2); again would just load the image from the cache, not from the remote server again.

EDIT:

If you call getDrawable straight after calling Glide then it won't work, because Glide is still fetching the image so the first imageview is still empty. You need to use glides callback in order to make sure the image has been loaded before calling getDrawable

Glide.with(context).load(url).listener(new RequestListener).into(image1); 
MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
  • image2.setImageDrawable(image1.getDrawable(); i tried that way and the image was empty. Okay i thought about the cache mechanism and i wasnt sure how glide handle with it – Expiredmind Oct 28 '16 at 08:58
  • 1
    you need to call it after the image has loaded, at the minute Glide is being told to get the image and the second imageview is being populated before the image has loaded, if you call Glide.with(context).load(url).listener(new RequestListener).into(image1) then add the getDrawable code in the callback then it should work – MichaelStoddart Oct 28 '16 at 09:04
0
Bitmap bm=((BitmapDrawable)imageView1.getDrawable()).getBitmap();
imgview2.setImageBitmap(bm);
D.J
  • 1,439
  • 1
  • 12
  • 23
0

As your are using Glide to load image from url. Glide will use its cache to load image for second imageview it won't use internet to load same image in second imageview.

You can also use the following way to load image once for sure

  Glide.with(this).load("url").into(new SimpleTarget<GlideDrawable>() {
                @Override
                public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {

                    image1.setImageDrawable(resource);
 image2.setImageDrawable(resource);


                }
            });
Sahil Manchanda
  • 9,812
  • 4
  • 39
  • 89