2013-05-02 73 views
7

我想將多個彩色濾鏡應用於可繪圖的鏈。那可能嗎?或者,也許要創建一個過濾器,它是我想應用的過濾器的組合。將許多彩色濾鏡應用於同一個可繪圖

例如,我想:

Drawable d = ...; 
d.setColorFilter(0x3F000000, Mode.OVERLAY).setColorFilter(0xFF2D2D2D, Mode.SCREEN) 

回答

6

這是我使用結束的辦法:處理在CanvasDrawable位圖和應用,因爲我需要儘可能多的圖層,使用Paint,它不僅適用彩色濾光片,也適用於任何形式的圖像混合。

... 
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); 
} 

警告:setBackgroundDrawable(Drawable d)已被棄用,而setBackground(Drawable d)只能從API 16,所以如果你有像我這樣的分目標API-14最大目標API-17,你有沒有「乾淨」的方式將drawable設置爲背景。我堅持已棄用的電話。