android自定義按鈕示例(重寫imagebutton控件實現(xiàn)圖片按鈕)
由于項目這種類型的圖片按鈕比較多,所以重寫了ImageButton類。
package me.henji.widget;
import android.content.Context;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.ImageButton;
/**
* 自定義圖片按鈕(ImageButton),按下顏色改變
* @author Leo
* @created 2013-3-15
*/
public class CmButton extends ImageButton implements OnTouchListener, OnFocusChangeListener {
public CmButton(Context context) {
super(context);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}
public CmButton(Context context, AttributeSet attrs) {
this(context, attrs, android.R.attr.imageButtonStyle);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}
public CmButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setFocusable(true);
this.setOnTouchListener(this);
this.setOnFocusChangeListener(this);
}
@Override
public void onFocusChange(View v, boolean hasFocus) {
// 灰色效果
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
if (hasFocus) {
((ImageButton) v).getDrawable().setColorFilter(new ColorMatrixColorFilter(cm));
} else {
((ImageButton) v).getDrawable().clearColorFilter();
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// 灰色效果
ColorMatrix cm = new ColorMatrix();
cm.setSaturation(0);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
((ImageButton) v).getDrawable().setColorFilter(new ColorMatrixColorFilter(cm));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
((ImageButton) v).getDrawable().clearColorFilter();
}
return false;
}
}
布局文件
<me.henji.widget.CmButton
android:id="@+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:src="@drawable/button_login"
android:text="@string/login_login" />
相關文章
Android巧用ViewPager實現(xiàn)左右循環(huán)滑動圖片
這篇文章主要為大家詳細介紹了Android巧用ViewPager實現(xiàn)左右循環(huán)滑動圖片的相關資料,感興趣的小伙伴們可以參考一下2016-05-05Android中使用ListView實現(xiàn)漂亮的表格效果
這篇文章主要介紹了Android中使用ListView實現(xiàn)漂亮的表格效果,本文用詳細的代碼實例創(chuàng)建了一個股票行情表格,需要的朋友可以參考下2014-10-10Android實現(xiàn)志愿者系統(tǒng)詳細步驟與代碼
這篇文章主要介紹了Android實現(xiàn)志愿者系統(tǒng),本系統(tǒng)采用MVC架構設計,SQLite數(shù)據(jù)表有用戶表、成員表和活動表,有十多個Activity頁面。打開應用,進入歡迎界面,3s后跳轉登錄界面,用戶先注冊賬號,登錄成功后進入主界面2023-02-02android實用工具類分享(獲取內(nèi)存/檢查網(wǎng)絡/屏幕高度/手機分辨率)
這篇文章主要介紹了android實用工具類,包括獲取內(nèi)存、檢查網(wǎng)絡、屏幕高度、手機分辨率、獲取版本號等功能,需要的朋友可以參考下2014-03-03Android?Flutter實現(xiàn)評分組件的示例代碼
在很多應用中,我們都需要收集用戶的評分,比如商品滿意度、配送滿意度、應用使用體驗等等。本文就利用flutter_rating_bar實現(xiàn)簡易的評分組件,感興趣的可以2022-11-11