Android ImageView的selector效果實例詳解
Android ImageView的selector效果實例詳解
在平時開發(fā)中如Button我們給它加上selector分別呈現(xiàn)pressed以及normal效果能給我們的用戶體驗上大大增色不少,可是我們當我們是用ImageView來”當作”一個一個”Button”的時候發(fā)現(xiàn)直接設(shè)置selector卻不起作用,當然此時我們的應(yīng)用就表現(xiàn)的暗淡了。那我們就只能找到方法來解決這種情況。
首先定義一個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設(shè)置該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上要呈現(xiàn)的圖片資源就是用
mImageView.setBackgroundResource(R.drawable.icon);
也就是說我們是給imageview設(shè)置backgroundResource,然后給src設(shè)置我們設(shè)置好的selector,在視覺上我們的selector是顯示在ImageView的上方,當然我們點擊ImageView的時候就是觸發(fā)selector,這個時候就會有按下的效果了。
以上就是Android ImageView的自定義開發(fā),對于Android很多東西都需要重寫,不美觀肯定不是好的APP,本站還有很多關(guān)于Android開發(fā)的文章,請大家參閱,謝謝大家對本站的支持!
相關(guān)文章
基于Android設(shè)計模式之--SDK源碼之策略模式的詳解
本篇文章介紹了,基于Android設(shè)計模式之--SDK源碼之策略模式的詳解。需要的朋友參考下2013-04-04
Android EditText實現(xiàn)輸入金額類型詳解
EditText是Android中一個非常實用的控件,有很多InputType,可以來達到不同的輸入效果,下面這篇文章主要給大家介紹了關(guān)于Android EditText實現(xiàn)輸入金額類型的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-09-09
Android控件ListView用法(讀取聯(lián)系人示例代碼)
本文以一個讀取聯(lián)系人的代碼為大家講解下Android控件中ListView的使用方法,這個listView有個setAdapter 適配器,里面可以直接實現(xiàn)接口,或者寫個類2013-06-06
Android Studio使用教程(二):基本設(shè)置與運行
這篇文章主要介紹了Android Studio使用教程(二):基本設(shè)置與運行,本文講解了項目結(jié)構(gòu)、偏好設(shè)置、常用功能介紹、創(chuàng)建模擬器等內(nèi)容,需要的朋友可以參考下2015-05-05
Android WebView通過動態(tài)的修改js去攔截post請求參數(shù)實例
這篇文章主要介紹了Android WebView通過動態(tài)的修改js去攔截post請求參數(shù)實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-03-03

