How can I set a color filter on a drawable to be used in the RemoteView?
I have ImaveView
one RemoteView
for which I have to apply a filter. When not in RemoteView
, this is what I do and it works well:
Drawable icon = getResources().getDrawable(R.drawable.icon);
icon.setColorFilter(color, PorterDuff.Mode.SRC_IN);
image.setImageDrawable(icon);
RemoteView
doesn't seem to have a method to set drawable, which is not a resource. How should I do it?
Thank.
+3
source to share
1 answer
I had a similar problem. For me the solution was to use a bitmap. These two methods should give you an answer, or at least some solution.
private void setCurrentStatus(Context context, RemoteViews remoteViews) {
Bitmap source = BitmapFactory.decodeResource(context.getResources(), R.mipmap.ic_launcher);
Bitmap result = changeBitmapColor(source, Color.YELLOW);
remoteViews.setBitmap(R.id.iv_icon, "setImageBitmap", result);
}
private Bitmap changeBitmapColor(Bitmap sourceBitmap, int color) {
Bitmap resultBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0,
sourceBitmap.getWidth() - 1, sourceBitmap.getHeight() - 1);
Paint p = new Paint();
ColorFilter filter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN);
p.setColorFilter(filter);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, p);
return resultBitmap;
}
R.id.iv_icon is the ImageView id from layout
You can always extract from ImageView and convert it to bitmap.
+2
source to share