setColorFilter()和可变绘图对象

4
这段代码有什么问题?我已经苦苦钻研了几个小时,但在按下这个按钮时,dwb2 ColorFiltered只会显示原始资源:(
public ButtonEx(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub

        Drawable dwb = getResources().getDrawable(R.drawable.mainboard_btn_pen_colour);
        Drawable dwb2 = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.mainboard_btn_extra_time1)
                                                .copy( Bitmap.Config.ARGB_8888, true));
        dwb2.setColorFilter(getResources().getColor(R.color.mod_selected), PorterDuff.Mode.MULTIPLY);

        mLstDrawables.addState(new int[] {android.R.attr.state_pressed}, dwb2);
        mLstDrawables.addState(StateSet.WILD_CARD, dwb);

        setBackgroundDrawable(mLstDrawables);
    }
1个回答

4

最终采用了解决问题的方法:

    Drawable dwb = getResources().getDrawable(R.drawable.mainboard_btn_pen_colour);
    Bitmap immutable = BitmapFactory.decodeResource(getResources(), R.drawable.mainboard_btn_extra_time1);
    Bitmap mutable = immutable.copy(Bitmap.Config.ARGB_8888, true);
    Canvas c = new Canvas(mutable);
    Paint p = new Paint();
    p.setColor(getResources().getColor(R.color.mod_selected));
    p.setColorFilter(new PorterDuffColorFilter(getResources().getColor(R.color.mod_selected), PorterDuff.Mode.MULTIPLY));
    c.drawBitmap(mutable, 0.f, 0.f, p);
    BitmapDrawable dwb3 = new BitmapDrawable(getResources(), mutable);

    mLstDrawables.addState(new int[] {android.R.attr.state_pressed}, dwb3);
    mLstDrawables.addState(StateSet.WILD_CARD, dwb);

    setBackgroundDrawable(mLstDrawables);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接