[英]Android - Compress Bitmap with applied PorterDuffColorFilter
I have a Bitmap
with a PorterDuffColorFilter
applied on it. 我有一个应用了PorterDuffColorFilter
的Bitmap
。 But when I'm compressing this Bitmap
to the file, the PorterDuffColorFilter
disappears. 但是,当我将此Bitmap
压缩到文件时, PorterDuffColorFilter
消失了。 How I can compress my Bitmap
and don't lose PorterDuffColorFilter
? 如何压缩我的Bitmap
并且不丢失PorterDuffColorFilter
?
Here's some code which I'm using now: 这是我现在正在使用的一些代码:
public void PrepareFiles(Bitmap original_img)
{
try
{
Bitmap b = convert(original_img, 0xFFFF0000);
String path1 = "/sdcard/red.png";
File f1 = new File(path1);
if (!f1.exists()) {
f1.createNewFile();
}
else
{
f1.delete();
f1.createNewFile();
}
FileOutputStream fos1 = new FileOutputStream(path1);
b.compress(Bitmap.CompressFormat.PNG, 100, fos1);
b.recycle();
}
catch (Exception ex)
{
Toast.makeText(this, ex.toString(), Toast.LENGTH_LONG).show();
}
}
public Bitmap convert(Bitmap src, int color)
{
BitmapDrawable temp = new BitmapDrawable(src);
temp.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.ADD));
return temp.getBitmap();
}
You cannot set a ColorFilter
on a Bitmap
. 您不能ColorFilter
Bitmap
上设置ColorFilter
。 In your code you are setting the ColorFilter
on a BitmapDrawable
. 在您的代码中,将ColorFilter
设置在BitmapDrawable
。 Doing so doesn't change the underlying Bitmap
instance. 这样做不会更改基础的Bitmap
实例。 To solve your problem you will have to create a new Bitmap
, attach a Canvas
to it and draw the original bitmap on the canvas using a Paint
which has the required ColorFilter
. 为了解决您的问题,您将必须创建一个新的Bitmap
,将其附加到Canvas
上,然后使用具有所需ColorFilter
的Paint
在画布上绘制原始位图。 Here's an example: 这是一个例子:
// The original bitmap
Bitmap src = ...;
// an empty bitmap with the same dimensions as the original one
Bitmap filteredBitmap = Bitmap.create(src.getWidth(), src.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(filteredBitmap);
Paint paint = new Paint(); // no need to set Paint.FILTER_BITMAP flag
paint.setColorFilter(colorFilter); // set the required color filter
canvas.drawBitmap(src, 0f, 0f, paint);
After this save the 'filteredBitmap' to a file. 之后,将“ filteredBitmap”保存到文件中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.