主要涉及兩個技術點:1、圖標加灰色過濾;2、Android的圖片資源預設是靜態的,單實例;如果兩個IM好友的頭像一樣,最簡單的都是用的軟體自帶頭像,有一個線上,一個離線,直接改變頭像的灰度,則兩個用戶的頭像都會變灰或者線上,答案是:Drawable.mutate()。代碼如下: ...
主要涉及兩個技術點:
1、圖標加灰色過濾;
2、Android的圖片資源預設是靜態的,單實例;如果兩個IM好友的頭像一樣,最簡單的都是用的軟體自帶頭像,有一個線上,一個離線,直接改變頭像的灰度,則兩個用戶的頭像都會變灰或者線上,答案是:Drawable.mutate()。
代碼如下:
Drawable mDrawable = context.getResources().getDrawable(R.drawable.face_icon); //Make this drawable mutable. //A mutable drawable is guaranteed to not share its state with any other drawable. mDrawable.mutate(); ColorMatrix cm = new ColorMatrix(); cm.setSaturation(0); ColorMatrixColorFilter cf = new ColorMatrixColorFilter(cm); mDrawable.setColorFilter(cf);