android Button 切換背景,實現動態按鈕和按鈕顏色漸變


    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文件中添加如下代碼:
    ①實現按鈕切換后實現按鈕背景圖片轉變。
[html]  view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <item android:state_pressed="true" android:color="#00000000"/>  
  4.     <item android:drawable="@drawable/bt_from1"/>  
  5. </selector>  
在篩選器中,上述屬性的設定是並列關系的(與關系),可以根據下列提供的屬性組合出適應不同場合的篩選。根據篩選的條件可以設置不同狀態的背景顏色和背景圖片。
②實現按鈕切換后實現按鈕顏色漸變。
          
[html]  view plain copy
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android"> /   
  3. <item android:state_pressed="true">//定義當button 處於pressed 狀態時的形態。   
  4.  <shape>  
  5.  <gradient android:startColor="#8600ff" />  
  6.  <stroke android:width="2dp" android:color="#000000" />   
  7.  <corners android:radius="5dp" />   
  8.  <padding android:left="10dp" android:top="10dp"   
  9.  android:bottom="10dp" android:right="10dp"/>   
  10.  </shape>  
  11. </item>   
  12. <item android:state_focused="true">//定義當button獲得 focus時的形態   
  13.  <shape>   
  14.  <gradient android:startColor="#eac100"/>   
  15.  <stroke android:width="2dp" android:color="#333333" color="#ffffff"/>   
  16.  <corners android:radius="8dp" />   
  17.  <padding android:left="10dp" android:top="10dp"   
  18.  android:bottom="10dp" android:right="10dp"/>   
  19.  </shape>   
  20. </item>  
  21. </selector>   
    5、在對布局xml文件中對按鈕添加背景屬性,背景引入以上創建的xml文件即可實現,動態的按鈕背景和顏色。
[html]  view plain copy
 
  1. <Button     
  2. android:id="@+id/btn_user_selected"    
  3. android:layout_width="wrap_content"    
  4. android:layout_height="wrap_content"    
  5. android:background="@drawable/btn_frome1/>    
    6、背景選擇器相關屬性
        android:state_selected :選中
        android:state_focused  :獲得焦點
        android:state_pressed  :點擊
        android:state_enabled  :設置是否響應事件,指所有事件
二、在java代碼中實現背景切換。
1、針對單個按鍵實現背景切換
[html]  view plain copy
 
  1. public boolean onTouch(View v, MotionEvent event) {    
  2.    Button upStepBtn = (Button) v;    
  3.       if(event.getAction() == MotionEvent.ACTION_DOWN){    
  4.            upStepBtn. getBackground().setAlpha(255);  //設置透明背景  
  5.        }else if(event.getAction() == MotionEvent.ACTION_UP){    
  6.           upStepBtn.setBackgroundResource(R.drawable. R.drawable.bt_from1 );    
  7.           finish();     
  8.        }    
  9.        return false;    
  10.    }    
通過監聽按鈕的不同狀態來更改按鈕的背景圖片
public boolean onTouch(View v,MotionEvent event){
 
}
參數v:事件源對象
參數event:事件封裝類的對象,其中封裝了觸發事件的詳細信息,同樣包括事件的類型、觸發時間等信息。
event.getAction() == MotionEvent.ACTION_DOWN   ======>按鈕被按下
event.getAction() == MotionEvent.ACTION_UP                ======>按鈕被釋放
2、針對多個按鍵實現背景切換
[html]  view plain copy
 
  1. private ImageView IB_1,IB_2;  
  2. public boolean onTouch(View v, MotionEvent event) {  
  3. if (v == IB_1) {  
  4. if (event.getAction() == MotionEvent. ACTION_UP ) {  
  5. IB_1.setBackgroundResource(R.drawable.bt_from1);          
  6. } else {  
  7.     IB_1.getBackground().setAlpha(255);//設置透明背景   
  8. }  
  9. if (v == IB_2) {  
  10. if (event.getAction() == MotionEvent. ACTION_UP ) {  
  11. IB_2.setBackgroundResource(R.drawable.bt_from2);  
  12. } else {  
  13.     IB_2.getBackground().setAlpha(255);//  
  14. }             
  15. }  
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);

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM