亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

[Android] 修改ImageView的圖片顏色

asal 2016-11-12 14:12:02 539
abstrak:有兩種方法:方法1:ImageView imageView = (ImageView) findViewById(R.id.arrow_image); Drawable tipsArrow = imageView.getDrawable();       &n

有兩種方法:
方法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要將圖片設(shè)置為綠色,第二個ImageView要將圖片設(shè)置為紅色,那么出來的結(jié)果就是有問題,兩張都變成綠色了。
方法2雖然比方法1復(fù)雜,但可以避免以上問題。同時它使用了ColorMatrix,這是個有趣的東西,值得深入研究,這里就不展開說。

Nota Keluaran

Penyertaan Popular