如何将ImageView复制到另一个ImageView?

时间:2018-04-01 19:01:36

标签: java android imageview clone deep-copy

说,ImageView i1中有一个图像,我想将ImageView i1复制到ImageView i2,而不仅仅是将i2引用到i1而是实际复制。

ImageView i2 = new ImageView(context);
i2 = i1;    //i2 only refers to i1

如果有一个类,我们将ImageView作为其构造函数的参数

传递
public class CopyEx{
public ImageView i2;

public CopyEx(ImageView i, Context c){
   i2 = new ImageView(c);
   i2 = i;    //This also only refers to i and not copy it
}
}

唯一可行的方法是做

i2.setImageDrawable(i1.getDrawable());
and
i2.setTag(i1.getTag());

那么有没有办法复制ImageView?

1 个答案:

答案 0 :(得分:1)

将此用于复制

BitmapDrawable drawable = (BitmapDrawable) imageView1.getDrawable();
Bitmap bitmap = drawable.getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

并用它来粘贴

Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
ImageView image2 = (ImageView) findViewById(R.id.imageView2);
image2.setImageBitmap(bmp);