歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> Android代碼實現控件按下顯示底色效果

Android代碼實現控件按下顯示底色效果

日期:2017/3/1 9:44:26   编辑:Linux編程

控件設置OnTouchListener,代碼如下(控件在xml中需要設置background):

btn.setOnTouchListener(new OnTouchListener() {

@Override
public boolean onTouch(View v, MotionEvent event) {
Drawable drawable = v.getBackground();
if (drawable == null)
return false;

if (Integer.parseInt(Build.VERSION.SDK) > Build.VERSION_CODES.DONUT)// 1.6版本以上使用
{
drawable.mutate();
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
drawable.setColorFilter(Color.argb(100, 0, 0, 0), Mode.DST_IN); //此處值可自行調整
v.setBackgroundDrawable(drawable);
break;
case MotionEvent.ACTION_UP:
drawable.clearColorFilter();
v.setBackgroundDrawable(drawable);
break;
case MotionEvent.ACTION_CANCEL:
drawable.clearColorFilter();
v.setBackgroundDrawable(drawable);
break;
default:
break;
}
return false;
}
});

更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11

Copyright © Linux教程網 All Rights Reserved