有兩種方法:
方法1:
ImageView imageView = (ImageView) findViewById(R.id.arrow_image); Drawable tipsArrow = imageView.getDrawable(); tipsArrow.setColorFilter(mContext.getResources().getColor(R.color.red_bg1), PorterDuff.Mode.SRC_ATOP); imageView.setImageDrawable(tipsArrow);
?
方法2:
ImageView imageView = (ImageView) findViewById(R.id.arrow_image); Bitmap baseBitmap = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.img_pop_arrow); Bitmap afterBitmap = Bitmap.createBitmap(baseBitmap.getWidth(), baseBitmap.getHeight(), baseBitmap.getConfig()); Canvas canvas = new Canvas(afterBitmap); Paint paint = new Paint(); float[] src = new float[]{0, 0, 0, 0, 253,0, 0, 0, 0, 54,0, 0, 0, 0, 102,0, 0, 0, 1, 0 }; ColorMatrix colorMatrix = new ColorMatrix(); colorMatrix.set(src); paint.setColorFilter(new ColorMatrixColorFilter(src)); canvas.drawBitmap(baseBitmap, new Matrix(), paint); imageView.setImageBitmap(afterBitmap);
?
方法1比方法2更加簡潔,但會有個問題,如果同個activity里面,有多個ImageView,每個ImageView操作的圖片路徑都是一樣的,那么它們是會互相影響的。
比如,第一個ImageView要將圖片設置為綠色,第二個ImageView要將圖片設置為紅色,那么出來的結果就是有問題,兩張都變成綠色了。
?
方法2雖然比方法1復雜,但可以避免以上問題。同時它使用了ColorMatrix,這是個有趣的東西,值得深入研究,這里就不展開說。
?
Have fun with Android!