android Button 切換背景,實現動態按鈕和按鈕顏色漸變
一、添加android 背景篩選器selector實現按鈕背景改變
1、右鍵單擊項目->new->Others->Android->Android Xml File->next.
2、在 New Android Xml File對話框中的 Resource Type 下拉框中選擇Drawable。在File中輸入要創建的文件名。
3、在Root Element:中選擇 selector(選擇器)->next->finish;或者跳過該項選擇,可以在生成的xml文件中添加selector的相關代碼也是可以的。
4、在生成的xml文件中添加如下代碼:
①實現按鈕切換后實現按鈕背景圖片轉變。
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android" >
- <item android:state_pressed="true" android:color="#00000000"/>
- <item android:drawable="@drawable/bt_from1"/>
- </selector>
在篩選器中,上述屬性的設定是並列關系的(與關系),可以根據下列提供的屬性組合出適應不同場合的篩選。根據篩選的條件可以設置不同狀態的背景顏色和背景圖片。
②實現按鈕切換后實現按鈕顏色漸變。
- <?xml version="1.0" encoding="utf-8"?>
- <selector xmlns:android="http://schemas.android.com/apk/res/android"> /
- <item android:state_pressed="true">//定義當button 處於pressed 狀態時的形態。
- <shape>
- <gradient android:startColor="#8600ff" />
- <stroke android:width="2dp" android:color="#000000" />
- <corners android:radius="5dp" />
- <padding android:left="10dp" android:top="10dp"
- android:bottom="10dp" android:right="10dp"/>
- </shape>
- </item>
- <item android:state_focused="true">//定義當button獲得 focus時的形態
- <shape>
- <gradient android:startColor="#eac100"/>
- <stroke android:width="2dp" android:color="#333333" color="#ffffff"/>
- <corners android:radius="8dp" />
- <padding android:left="10dp" android:top="10dp"
- android:bottom="10dp" android:right="10dp"/>
- </shape>
- </item>
- </selector>
5、在對布局xml文件中對按鈕添加背景屬性,背景引入以上創建的xml文件即可實現,動態的按鈕背景和顏色。
- <Button
- android:id="@+id/btn_user_selected"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/btn_frome1/>
6、背景選擇器相關屬性
android:state_selected :選中
android:state_focused :獲得焦點
android:state_pressed :點擊
android:state_enabled :設置是否響應事件,指所有事件
二、在java代碼中實現背景切換。
1、針對單個按鍵實現背景切換
- public boolean onTouch(View v, MotionEvent event) {
- Button upStepBtn = (Button) v;
- if(event.getAction() == MotionEvent.ACTION_DOWN){
- upStepBtn. getBackground().setAlpha(255); //設置透明背景
- }else if(event.getAction() == MotionEvent.ACTION_UP){
- upStepBtn.setBackgroundResource(R.drawable. R.drawable.bt_from1 );
- finish();
- }
- return false;
- }
通過監聽按鈕的不同狀態來更改按鈕的背景圖片
public boolean onTouch(View v,MotionEvent event){
}
參數v:事件源對象
參數event:事件封裝類的對象,其中封裝了觸發事件的詳細信息,同樣包括事件的類型、觸發時間等信息。
event.getAction() == MotionEvent.ACTION_DOWN ======>按鈕被按下
event.getAction() == MotionEvent.ACTION_UP ======>按鈕被釋放
2、針對多個按鍵實現背景切換
- private ImageView IB_1,IB_2;
- public boolean onTouch(View v, MotionEvent event) {
- if (v == IB_1) {
- if (event.getAction() == MotionEvent. ACTION_UP ) {
- IB_1.setBackgroundResource(R.drawable.bt_from1);
- } else {
- IB_1.getBackground().setAlpha(255);//設置透明背景
- }
- if (v == IB_2) {
- if (event.getAction() == MotionEvent. ACTION_UP ) {
- IB_2.setBackgroundResource(R.drawable.bt_from2);
- } else {
- IB_2.getBackground().setAlpha(255);//
- }
- }
3、設置BUTTON背景為透明
在“一”中使用了在篩選器中設置背景顏色設為透明,在“二”中實現在java中設置背景為透明。接下了,詳細說一下透明背景的設置。
1、Button或者ImageButton的背景設為透明或者半透明
①、半透明<Button android:background="#e0000000" ... />
②、透明<Button android:background="#00000000" ... />
理解:顏色和不透明度 (alpha) 值以十六進制表示法表示。任何一種顏色的值范圍都是 0 到 255(00 到 ff)。對於 alpha,00 表示完全透明,ff 表示完全不透明。表達式順序是“aabbggrr”,其中“aa=alpha”(00 到 ff);“bb=blue”(00 到 ff);“gg=green”(00 到 ff);“rr=red”(00 到 ff)。例如,如果您希望對某疊加層應用不透明度為 50% 的藍色,則應指定以下值:7fff0000
RGB
設置背景圖片透明度:
View v = findViewById(R.id.content);//找到你要設透明背景的layout 的id
v.getBackground().setAlpha(100);//0~255透明度值
設置背景顏色透明度:
ImageView.setBackgroundColor(Color.TRANSPARENT);