方法一、通过属性动画来改变图片的颜色
1 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { |
final EditText et = findViewById(R.id.et);
final ImageView iv = findViewById(R.id.iv);
iv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//获取ImageView上的图片
iv.setDrawingCacheEnabled(true);
Bitmap bitmap = iv.getDrawingCache();
//按照输入的颜色转换图标
String s = et.getText().toString();
Bitmap newBitmap = changeBitmapColor(bitmap,s);
iv.setDrawingCacheEnabled(false);
iv.setImageBitmap(newBitmap);
}
});1
## 实现方法:
public Bitmap changeBitmapColor(Bitmap sourceBitmap,String aimColorStr){
//验证参数合理
if(sourceBitmap == null || sourceBitmap.isRecycled()){
throw new RuntimeException(“source exception!!!”);
}
int aimColor;
try {
aimColor = Color.parseColor(aimColorStr.trim());
} catch (Exception e){
throw new RuntimeException(“aimColorStr error!!!”);
}
//按照图标的大小创建数组
int mBitmapWidth = sourceBitmap.getWidth();
int mBitmapHeight = sourceBitmap.getHeight();
int mArrayColorLengh = mBitmapWidth * mBitmapHeight;
int[] mArrayColor = new int[mArrayColorLengh];
//循环bitmap 的每个像素点,查看alpha值
int count = 0;
for (int i = 0; i < mBitmapHeight; i++) {
for (int j = 0; j < mBitmapWidth; j++) {
//获得Bitmap 图片中每一个点的color颜色值
int color = sourceBitmap.getPixel(j, i);
int a = Color.alpha(color);
if(a != 0){//不等于0 即不透明部分,设置成我们想要的颜色
mArrayColor[count] = aimColor;
} else {//透明仍然为透明
int aimColor2 = Color.parseColor("#00000000");
mArrayColor[count] = aimColor2;
}
count++;
}
}
//根据数组创建新的Bitmap
Bitmap newBitmap = Bitmap.createBitmap(mBitmapWidth,mBitmapHeight, Bitmap.Config.ARGB_8888);
newBitmap.setPixels(mArrayColor,0,mBitmapWidth,0,0,mBitmapWidth,mBitmapHeight);
return newBitmap;
}
1 |
|
//提取图像Alpha位图
public static Bitmap getAlphaBitmap(Bitmap mBitmap,int mColor) {
// BitmapDrawable mBitmapDrawable = (BitmapDrawable) mContext.getResources().getDrawable(R.drawable.enemy_infantry_ninja);
// Bitmap mBitmap = mBitmapDrawable.getBitmap();
//BitmapDrawable的getIntrinsicWidth()方法,Bitmap的getWidth()方法
//注意这两个方法的区别
//Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmapDrawable.getIntrinsicWidth(), mBitmapDrawable.getIntrinsicHeight(), Config.ARGB_8888);
Bitmap mAlphaBitmap = Bitmap.createBitmap(mBitmap.getWidth(), mBitmap.getHeight(), Config.ARGB_8888);
Canvas mCanvas = new Canvas(mAlphaBitmap);
Paint mPaint = new Paint();
mPaint.setColor(mColor);
//从原位图中提取只包含alpha的位图
Bitmap alphaBitmap = mBitmap.extractAlpha();
//在画布上(mAlphaBitmap)绘制alpha位图
mCanvas.drawBitmap(alphaBitmap, 0, 0, mPaint);
return mAlphaBitmap;
}
1 |
|
ImageView deviceIV.setDrawingCacheEnabled(true);
Bitmap deviceBmp = deviceIV.getDrawingCache();
int color = Color.parseColor(“#00000000”);
Bitmap bitmap = getAlphaBitmap(deviceBmp,color);
deviceIV.setImageBitmap(bitmap);
时常会出现null,原因是超出系统提供最大的DrawingCache值,所以要在此代码前添加如下代码:
deviceIV.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
deviceIV.layout(0, 0, deviceIV.getMeasuredWidth(), deviceIV.getMeasuredHeight());
`