Questo è l'approccio ho finito utilizzando: Manipolare il Drawable
bitmap in una Canvas
e applicare tutti i livelli come ho bisogno, utilizzando Paint
, funziona non solo per filtri colorati, ma anche per qualsiasi tipo di fusione di immagini.
...
Drawable myBackground = createBackground(getResources().getColor(R.color.Green));
setBackgroundDrawable(myBackground);
...
private Drawable createBackground(int color) {
Canvas canvas = new Canvas();
Bitmap buttonImage = BitmapFactory.decodeResource(getResources(), R.drawable.btn_image);
Bitmap buttonShadows = BitmapFactory.decodeResource(getResources(), R.drawable.btn_shadows);
Bitmap buttonHighLights = BitmapFactory.decodeResource(getResources(), R.drawable.btn_highlights);
Bitmap result = Bitmap.createBitmap(buttonImage.getWidth(), buttonImage.getHeight(), Bitmap.Config.ARGB_8888);
canvas.setBitmap(result);
Paint paint = new Paint();
paint.setFilterBitmap(false);
// Color
paint.setColorFilter(new PorterDuffColorFilter(color, Mode.MULTIPLY));
canvas.drawBitmap(buttonImage, 0, 0, paint);
paint.setColorFilter(null);
// Shadows
paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY));
canvas.drawBitmap(buttonShadows, 0, 0, paint);
// HighLights
paint.setXfermode(new PorterDuffXfermode(Mode.SCREEN));
canvas.drawBitmap(buttonHighLights, 0, 0, paint);
paint.setXfermode(null);
return new BitmapDrawable(getResources(), result);
}
Caveat:setBackgroundDrawable(Drawable d)
è deprecato, mentre setBackground(Drawable d)
è disponibile solo via api 16 in poi, quindi se avete come me un bersaglio min obiettivo di api-14 max api-17 non hai modo "pulito" per imposta il drawable come sfondo. Mi sono attenuto alla chiamata deprecata.