简体   繁体   English

如何制作BitmapDrawable的深层副本?

[英]How do I made a deep copy a BitmapDrawable?

I'm having trouble cloning a BitmapDrawable. 我在克隆BitmapDrawable时遇到问题。 I tried the answer in this post but it creates a "shallow" copy, and I need a "deep" copy so I can alter the pixels in the clone without affecting the original. 我尝试了这篇文章中的答案,但是它创建了一个“浅”副本,并且我需要一个“深”副本,因此我可以在不影响原始副本的情况下更改克隆中的像素。

I also tried this: 我也试过这个:

    Bitmap bitmap = bdOriginal.getBitmap();
    BitmapDrawable bdClone = new BitmapDrawable(getResources(), bitmap.copy(bitmap.getConfig(), true));

But it seems to create an immutable clone even though I set the mutable parameter in Bitmap.copy() to "true". 但是,即使我将Bitmap.copy()中的可变参数设置为“ true”,它似乎也会创建不可变的克隆。 That is, color filters don't appear to change the clone. 也就是说,滤色器似乎没有改变克隆。 Am I doing it wrong? 我做错了吗? (EDIT: I used the debugger to confirm bitmap.mIsMutable = true) (编辑:我使用调试器确认bitmap.mIsMutable = true)

To summarize, I need a clone of a BitmapDrawable that can be altered with color filters without affecting the original. 总而言之,我需要一个BitmapDrawable的克隆,可以使用彩色滤光片对其进行更改而不会影响原始图像。 Any suggestions? 有什么建议么?

Thanks in advance... 提前致谢...

  1. Create new Bitmap of the same size. 创建相同大小的新Bitmap
  2. Create canvas for this new Bitmap 为此新的Bitmap创建画布
  3. Draw your first Bitmap into this canvas. 将您的第一个Bitmap绘制到此画布中。

Example: 例:

Bitmap copy = Bitmap.createBitmap(original.getWidth(), original.getHeight(), original.getConfig());
Canvas copiedCanvas = new Canvas(copy);
copiedCanvas.drawBitmap(original, 0f, 0f, null);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM