修改 drawable 图片指定颜色

原创 2015年11月17日 18:53:12
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);
    }
}
版权声明:本文为博主原创文章,未经博主允许不得转载。

Drawable xml中图片和字体颜色的定义

在drawable xml中为不同状态指定不同的图片和字体颜色。

帧动画显示drawable文件夹下的指定图片

package com.noahedu.copybook.widget; import java.lang.reflect.Field; import android.content.Conte...
  • ssmoom
  • ssmoom
  • 2013年11月29日 17:28
  • 616

Drawable资源管理图片处理

  • 2015年10月21日 16:59
  • 2.34MB
  • 下载

gif-drawable播放本地gif图片

  • 2016年03月30日 10:09
  • 7.61MB
  • 下载

android 图片倒影 bitmap与drawable的转换 把图片处理成指定大小 图片圆角处理 dip转换成px

//图片倒影处理 public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) { final int reflecti...

android_gif_drawable(播放gif图片)

  • 2017年02月10日 10:35
  • 42.39MB
  • 下载

Android-用户资源访问:字符串、颜色、尺寸、布局、数组、Drawable

字符串(string)资源 字符串资源文件位于res/

自定义Drawable实现圆形图片

  • 2015年10月02日 12:19
  • 5.96MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:修改 drawable 图片指定颜色
举报原因:
原因补充:

(最多只允许输入30个字)