关闭

修改 drawable 图片指定颜色

257人阅读 评论(0) 收藏 举报
分类:
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);
    }
}
1
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:372999次
    • 积分:6805
    • 等级:
    • 排名:第3629名
    • 原创:284篇
    • 转载:79篇
    • 译文:3篇
    • 评论:115条
    欢迎加群
    Android交流群号:460115164 希望大家能在一起交流和学习。
    最新评论