ImageView的selector效果,imageviewselector
在平時開發中如Button我們給它加上selector分別呈現pressed以及normal效果能給我們的使用者體驗上大大增色不少,可是我們當我們是用ImageView來”當作”一個一個”Button”的時候發現直接設定selector卻不起作用,當然此時我們的應用就表現的暗淡了。那我們就只能找到方法來解決這種情況。
首先定義一個selector檔案:
<selector xmlns:android="http://schemas.android.com/apk/res/android" > <item android:state_pressed="true"> <shape android:shape="rectangle"> <corners android:radius="5dp" /> <solid android:color="#50000000"/> </shape> </item> <item > <shape android:shape="rectangle"> <corners android:radius="5dp" /> <solid android:color="#00000000"/> </shape> </item></selector>
第二步,給ImageView的src設定該selector。
<ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:contentDescription="@null" android:scaleType="centerCrop" android:src="@drawable/share_image_selector" />
然後我們的ImageVIew上要呈現的圖片資源就是用
mImageView.setBackgroundResource(R.drawable.icon);
也就是說我們是給imageview設定backgroundResource,然後給src設定我們設定好的selector,在視覺上我們的selector是顯示在ImageView的上方,當然我們點擊ImageView的時候就是觸發selector,這個時候就會有按下的效果了。
所以說,一個好的app在體驗上要做到細心使用者用起來才會舒暢,由於忘了是哪位Androider的部落格上看到,由於是在檢查代碼的時候發現了這個效果記錄分享一下,感謝那位Androider。