修改 drawable 图片指定颜色

public class ChangeDrawableColor {

    public static void setStateListDrawable(Context context,final ImageView iv,int normal,
            int selected,int beColor) {
        //创建 StateListDrawable
        final StateListDrawable state = new StateListDrawable();
        //按下状态的图片
        state.addState(new int[] {android.R.attr.state_pressed}, getDrawable(context,selected,beColor));
        //普通状态下的图片
        state.addState(new int[] {},getDrawable(context,normal,beColor));
        iv.setImageDrawable(state);
    }

    //更改指定色值
    private static Drawable getDrawable(Context context,int drawableID,int beColor) {
        Bitmap bm = BitmapFactory.decodeResource(context.getResources(),
                drawableID);
        int w = bm.getWidth();
        int h = bm.getHeight();
        //创建像素方阵
        int px[] = new int[w * h];
        //放入
        bm.getPixels(px, 0, w, 0, 0, w, h);
        for (int i = 0; i < px.length; i++) {
            int r = Color.red(px[i]);
            int g = Color.green(px[i]);
            int b = Color.blue(px[i]);
            int a = Color.alpha(px[i]);
            if (r ==  255 && g == 255 && b == 255) {
                r = Color.red(beColor);
                g = Color.green(beColor);
                b = Color.blue(beColor);
                px[i] = Color.argb(a, r, g, b);
            }
        }
        bm = Bitmap.createBitmap(px, 0, w, w, h, Bitmap.Config.ARGB_8888);
        return new BitmapDrawable(context.getResources(), bm);
    }

}

调用

ChangeDrawableColor.setStateListDrawable(
context,imageView,
R.drawable.default,
R.drawable.pressed,
changeColor);
  1. 通过 提取色 轮廓
public class FixedGridLayout extends ViewGroup {
    int mCellWidth;
    int mCellHeight;

    public FixedGridLayout(Context context) {
        super(context);
    }

    public FixedGridLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        // Read the resource attributes.
        TypedArray a = context.obtainStyledAttributes(
                attrs, R.styleable.FixedGridLayout);
        mCellWidth = a.getDimensionPixelSize(
                R.styleable.FixedGridLayout_cellWidth, -1);
        mCellHeight = a.getDimensionPixelSize(
                R.styleable.FixedGridLayout_cellHeight, -1);
        a.recycle();

    }

    public void setCellWidth(int px) {
        mCellWidth = px;
        requestLayout();
    }

    public void setCellHeight(int px) {
        mCellHeight = px;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int cellWidthSpec = MeasureSpec.makeMeasureSpec(mCellWidth,
                MeasureSpec.AT_MOST);
        int cellHeightSpec = MeasureSpec.makeMeasureSpec(mCellHeight,
                MeasureSpec.AT_MOST);

        int count = getChildCount();
        for (int index=0; index<count; index++) {
            final View child = getChildAt(index);
            child.measure(cellWidthSpec, cellHeightSpec);
        }
        // Use the size our parents gave us
        setMeasuredDimension(resolveSize(mCellWidth*count, widthMeasureSpec),
                resolveSize(mCellHeight*count, heightMeasureSpec));
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int cellWidth = mCellWidth;
        int cellHeight = mCellHeight;
        int columns = (r - l) / cellWidth;
        if (columns < 0) {
            columns = 1;
        }
        int x = 0;
        int y = 0;
        int i = 0;
        int count = getChildCount();
        for (int index=0; index<count; index++) {
            final View child = getChildAt(index);

            int w = child.getMeasuredWidth();
            int h = child.getMeasuredHeight();

            int left = x + ((cellWidth-w)/2);
            int top = y + ((cellHeight-h)/2);

            child.layout(left, top, left+w, top+h);
            if (i >= (columns-1)) {
                // advance to next row
                i = 0;
                x = 0;
                y += cellHeight;
            } else {
                i++;
                x += cellWidth;
            }
        }
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int cnt = getChildCount();
        Paint p = new Paint();
//        p.setColor(Color.CYAN);
        p.setColor(Color.WHITE);
        for (int i=0; i<cnt; i++) {
            View v = getChildAt(i);
            setStateDrawable((ImageView)v, p);
        }
    }

    private void setStateDrawable(ImageView v, Paint p) {
        BitmapDrawable bd = (BitmapDrawable) v.getDrawable();
        Bitmap b = bd.getBitmap();
        Bitmap bitmap = Bitmap.createBitmap(bd.getIntrinsicWidth(), bd.getIntrinsicHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawBitmap(b.extractAlpha(), 0, 0, p);

        StateListDrawable sld = new StateListDrawable();
        sld.addState(new int[]{android.R.attr.state_pressed}, new BitmapDrawable(bitmap));

        v.setBackgroundDrawable(sld);
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

空白的泡

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值