【问题标题】:How do you copy an ImageView to another ImageView?如何将 ImageView 复制到另一个 ImageView?
【发布时间】:2018-04-01 19:01:36
【问题描述】:

说,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 呢?

【问题讨论】:

    标签: java android imageview clone deep-copy


    【解决方案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);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-08-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-31
      相关资源
      最近更新 更多