이것은 내가 사용 종료 된 접근 방식입니다하십시오 Canvas
에 Drawable
비트 맵을 조작하고 내가 필요한만큼 레이어를 적용, 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 분 대상이있는 경우 드로어 블을 배경으로 설정하십시오. 나는 그 비난 된 전화를 붙였다.