Android ImageButton自定義按鈕的按下效果的代碼實現(xiàn)方法分享
使用Button時為了讓用戶有“按下”的效果,有兩種實現(xiàn)方式:
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文件實現(xiàn)。
<?xml version="1.0" encoding="UTF-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<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:id="@+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#00000000"
android:src="@drawable/button_add_x" >
</ImageButton>
我自己摸索摸索,發(fā)現(xiàn)這樣的實現(xiàn)過程雖然通用性好,但是很麻煩,一個按鈕實現(xiàn)效果需要多張圖片甚至再加一個布局…
那一個游戲要是有幾百個按鈕怎么辦呢?
于是:以下代碼被醞釀出來了:
/**
* 按下這個按鈕進行的顏色過濾
*/
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;
}
};
/**
* 設(shè)置圖片按鈕獲取焦點改變狀態(tài)
* @param inImageButton
*/
public final static void setButtonFocusChanged(View inView)
{
inView.setOnTouchListener(buttonOnTouchListener);
inView.setOnFocusChangeListener(buttonOnFocusChangeListener);
}
使用時,調(diào)用方法
public final static void setButtonFocusChanged(View inView)
即可。
【原理】
利用Drawable類的setColorFilter方法對圖片進行顏色偏移過濾處理。
- android imageview圖片居中技巧應用
- Android控件系列之ImageView使用方法
- Android開發(fā)ImageView圖片無法顯示解決過程
- android ImageView 的幾點經(jīng)驗總結(jié)
- Android實現(xiàn)ImageView圖片雙擊放大及縮小
- Android控件之ImageView用法實例分析
- Android編程簡單實現(xiàn)ImageView點擊時背景圖修改的方法
- Android使用CircleImageView實現(xiàn)圓形頭像的方法
- Android開發(fā)筆記之:在ImageView上繪制圓環(huán)的實現(xiàn)方法
- Android中Image的簡單實例詳解
相關(guān)文章
Android Studio使用教程(四):Gradle基礎(chǔ)
這篇文章主要介紹了Android Studio使用教程(四):Gradle基礎(chǔ),本文講解了什么是Gradle、安裝Gradle、Gradle 基本概念等內(nèi)容,需要的朋友可以參考下2015-05-05Android 中出現(xiàn)java.net.BindException: bind failed: EADDRINUSE 問
這篇文章主要介紹了Android 中出現(xiàn)java.net.BindException: bind failed: EADDRINUSE 問題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-04-04Android應用內(nèi)調(diào)用第三方應用的方法
這篇文章主要介紹了Android應用內(nèi)調(diào)用第三方應用的方法,有需要的朋友可以參考一下2014-01-01Android自定義控件ViewGroup實現(xiàn)標簽云
這篇文章主要為大家詳細介紹了Android自定義控件ViewGroup實現(xiàn)標簽云,具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-05-05Android帶進度條的下載圖片示例(AsyncTask異步任務(wù))
本文主要介紹Android帶進度條的下載圖片示例(AsyncTask異步任務(wù))的方法解析。具有很好的參考價值。下面跟著小編一起來看下吧2017-04-04Android程序開發(fā)之Fragment實現(xiàn)底部導航欄實例代碼
流行的應用的導航一般分為兩種,一種是底部導航,一種是側(cè)邊欄。本文給大家介紹Fragment實現(xiàn)底部導航欄,對Fragment實現(xiàn)底部導航欄相關(guān)知識感興趣的朋友一起學習吧2016-03-03