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);
- 通过 提取色 轮廓
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);
}
}