【问题标题】:setColorFilter() and mutable drawablessetColorFilter() 和可变可绘制对象
【发布时间】:2012-08-21 10:03:37
【问题描述】:

这段代码有什么问题?我一直在努力获得 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);
    }

【问题讨论】:

    标签: android bitmap drawable mutable


    【解决方案1】:

    最终解决了这个问题:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-10-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-28
      • 1970-01-01
      • 2021-06-06
      • 2012-05-22
      • 1970-01-01
      相关资源
      最近更新 更多