2013-05-02 3 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 분 대상이있는 경우 드로어 블을 배경으로 설정하십시오. 나는 그 비난 된 전화를 붙였다.

관련 문제