Android踩坑日记:点击变暗效果的ImageView实现原理
生活随笔
收集整理的這篇文章主要介紹了
Android踩坑日记:点击变暗效果的ImageView实现原理
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
很多時候我們使用ImagView顯示圖片,無論是Gilde,Fresco等圖片顯示框架,比如設置中心更換頭像,網格相冊點擊預覽,選擇等情況,會遇到點擊變暗的交互需求。
- 源碼分析
????我們想的辦法是自定義一個ImageView,當點擊圖片時,是不是有回調方法來同時改變圖片的濾鏡或者蒙版等。
????特意去看了View.java的源碼(ImageView繼承View),想看看View被點擊之后是是否有回調函數可用。
意思就是case: MotionEvent.ACTION_DOWN,按下去的時候事件發生時,檢測View是否被點擊了,如果點擊了就setPressed(true);把狀態標記為已點擊
對應的case: MotionEvent.ACTION_UP,松開手的時候會檢測是否是unpressed,如果是就setPressed(false);把狀態標記為未點擊。
setPress(boolean pressed)這個方法,定義如下
/*** Sets the pressed state for this view.** @see #isClickable()* @see #setClickable(boolean)** @param pressed Pass true to set the View's internal state to "pressed", or false to reverts* the View's internal state from a previously set "pressed" state.*/public void setPressed(boolean pressed) {final boolean needsRefresh = pressed != ((mPrivateFlags & PFLAG_PRESSED) == PFLAG_PRESSED);if (pressed) {mPrivateFlags |= PFLAG_PRESSED;} else {mPrivateFlags &= ~PFLAG_PRESSED;}if (needsRefresh) {refreshDrawableState();}dispatchSetPressed(pressed);}就是說View按下時會調用這個方法改變view的狀態,那么我們就可以在這個方法中做文章,重寫這個方法。當參數是true時,使用顏色矩陣ColorMetrix來改變drawable的濾鏡,當參數是false時,還原圖像
/*點擊變暗效果的ImageView*/ public class MaskImageView extends ImageView {private boolean touchEffect = true;public final float[] BG_PRESSED = new float[] { 1, 0, 0, 0, -50, 0, 1, 0, 0, -50, 0, 0, 1, 0, -50, 0, 0, 0, 1, 0 };public final float[] BG_NOT_PRESSED = new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 };public MaskImageView(Context context) {super(context);}public MaskImageView(Context context, AttributeSet attrs) {super(context, attrs);}public MaskImageView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public MaskImageView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {super(context, attrs, defStyleAttr, defStyleRes);}@Overridepublic void setPressed(boolean pressed) {updateView(pressed);super.setPressed(pressed);}/*** 根據是否按下去來刷新bg和src** @param pressed*/private void updateView(boolean pressed){//如果沒有點擊效果if( !touchEffect ){return;}if( pressed ){/*** 通過設置濾鏡來改變圖片亮度*/this.setDrawingCacheEnabled(true);this.setColorFilter( new ColorMatrixColorFilter(BG_PRESSED) ) ;//此為src,背景用getBackground()this.getDrawable().setColorFilter( new ColorMatrixColorFilter(BG_PRESSED) );}else{this.setColorFilter( new ColorMatrixColorFilter(BG_NOT_PRESSED) ) ;this.getDrawable().setColorFilter( new ColorMatrixColorFilter(BG_NOT_PRESSED) );}} }總結
以上是生活随笔為你收集整理的Android踩坑日记:点击变暗效果的ImageView实现原理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Android踩坑日记:监听软键盘多次调
- 下一篇: Android踩坑日记:使用Fesco图