Android中的Button、ImageButton自定義點擊效果
使用Button時為了讓用戶有“按下”的效果,有兩種實現方式:1.在代碼裏麵。
imageButton.setOnTouchListener(new OnTouchListener(){ @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ //更改為按下時的背景圖片 v.setBackgroundResource(R.drawable.pressed); }else if(event.getAction() == MotionEvent.ACTION_UP){ //改為抬起時的圖片 v.setBackgroundResource(R.drawable.released); } return false; } });
2.用XML文件實現。
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:andro> <item android:state_pressed="false" android:drawable="@drawable/button_add" /> <item android:state_pressed="true" android:drawable="@drawable/button_add_pressed" /> <item android:state_focused="true" android:drawable="@drawable/button_add_pressed" /> <item android:drawable="@drawable/button_add" /> </selector>
這個文件放在drawable目錄下麵。命名為button_add_x.xml
使用的時候
<ImageButton android: android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="#00000000" android:src="@drawable/button_add_x" > </ImageButton>
那一個遊戲要是有幾百個按鈕怎麼辦呢?
於是:以下代碼被醞釀出來了:
/** * 按下這個按鈕進行的顏色過濾 */ public final static float[] BT_SELECTED=new float[] { 2, 0, 0, 0, 2, 0, 2, 0, 0, 2, 0, 0, 2, 0, 2, 0, 0, 0, 1, 0 }; /** * 按鈕恢複原狀的顏色過濾 */ public final static float[] BT_NOT_SELECTED=new float[] { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0 }; /** * 按鈕焦點改變 */ public final static OnFocusChangeListener buttonOnFocusChangeListener=new OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (hasFocus) { v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } else { v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } } }; /** * 按鈕觸碰按下效果 */ public final static OnTouchListener buttonOnTouchListener=new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { if(event.getAction() == MotionEvent.ACTION_DOWN){ v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } else if(event.getAction() == MotionEvent.ACTION_UP){ v.getBackground().setColorFilter(new ColorMatrixColorFilter(BT_NOT_SELECTED)); v.setBackgroundDrawable(v.getBackground()); } return false; } }; /** * 設置圖片按鈕獲取焦點改變狀態 * @param inImageButton */ public final static void setButtonFocusChanged(View inView) { inView.setOnTouchListener(buttonOnTouchListener); inView.setOnFocusChangeListener(buttonOnFocusChangeListener); }
使用時,調用方法
public final static void setButtonFocusChanged(View inView)
即可。
【原理】
利用Drawable類的setColorFilter方法對圖片進行顏色偏移過濾處理。
以下為效果圖,登陸按鈕此時為獲取焦點狀態。
代碼可以適當修改實現3個不同的狀態:正常,獲取焦點,點擊。
出處:https://blog.csdn.net/sytzz/archive/2010/06/16/5673662.aspx最後更新:2017-04-02 16:47:34
上一篇:
使用單例模式實現自己的HttpClient工具類
下一篇:
java.text.format 將字符串“060503”轉化為06:05:03或者將"20081002102030“轉化為2008-10-02 10:00:30
java中報錯java.sql.Timestamp cannot be cast to java.sql.Date
數字虛擬幣資產交易平台開發,加密虛擬幣交易平台製作
Coin Test
OBTAINING SPRING 3 ARTIFACTS WITH MAVEN
Android---浮動搜索框(SearchManager)
過濾器和攔截器異同
《STM32庫開發實戰指南:基於STM32F4》----第3章 初識STM32 3.1 什麼是STM32
緩存係列文章–無底洞問題
phantomjs技巧之golang後端生成highcharts圖片文件
《iOS6 application development》學習之路:No.1