android view重写onTouchEvent事件更换背景(android 动态设置背景图 )
2012-12-28 11:37
549 查看
根据MotionEvent event.getAction()可以动态更换View样式
event.getAction()==MotionEvent.ACTION_UP||event.getAction()==MotionEvent.ACTION_CANCEL //ACTION_MOVE事件以后松手后走不到ACTION_UP而是ACTION_CANCEL
event.getAction()==MotionEvent.ACTION_UP||event.getAction()==MotionEvent.ACTION_CANCEL //ACTION_MOVE事件以后松手后走不到ACTION_UP而是ACTION_CANCEL
package cn.qiluzhixiao.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.view.KeyEvent; import android.view.MotionEvent; import android.widget.Button; import android.widget.Toast; public class MyButton extends Button { private Drawable img_on, img_off; public MyButton(Context context) { super(context); // TODO Auto-generated constructor stub } public MyButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyButton(Context context, AttributeSet attrs) { super(context, attrs); Resources res = getResources(); img_on = res.getDrawable(R.drawable.android_spinner_right_pressed); img_off = res.getDrawable(R.drawable.android_spinner_right); //调用setCompoundDrawables时,必须调用Drawable.setBounds()方法,否则图片不显示 img_off.setBounds(0, 0, img_off.getMinimumWidth(), img_off.getMinimumHeight()); img_on.setBounds(0, 0, img_on.getMinimumWidth(), img_on.getMinimumHeight()); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub switch (event.getAction()) { case 0: break; default: break; } if(event.getAction()==MotionEvent.ACTION_UP||event.getAction()==MotionEvent.ACTION_CANCEL) { this.setCompoundDrawables(null, null, img_off, null); //设置右图标 } if(event.getAction()==MotionEvent.ACTION_DOWN) { this.setCompoundDrawables(null, null, img_on, null); //设置右图标 } return super.onTouchEvent(event); } }
相关文章推荐
- android在滑动控件过程中动态设置view背景透明的问题
- 给一个android的TextView设置背景颜色动态显示,到那时好像不起作用。我是少了什么东西么?
- android开发之绘制View,并且重写OnTouchEvent事件使飞机移动
- 【Android】viewpager嵌套4个带侧滑的listview 事件冲突处理解决方案,可动态设置是否可以侧滑
- Android实战简易教程-第四枪(ScrollView和HorizontalScrollView动态添加控件并提供事件监听)
- Android布局自定义Shap圆形ImageView,可以单独设置背景与图片
- Android中使用selector动态改变imageView或ImageButton的背景和在GridView中应用
- Android - 动态设置自定义View的id - setId
- Android webview背景设置为透明无效
- 获取android 手机屏幕的大小以及动态设置背景图片的两种方法;
- android 如何动态设置View的margin和padding
- 设置不同事件View背景颜色
- Android之WebView——解决加载Https以及根据网页动态设置WebView高度等难题
- ANDROID设置TEXTVIEW控件的背景透明度和字体透明度
- Android 动态设置TextView drawableLeft的方式
- Android布局自定义Shap圆形ImageView,可以单独设置背景与图片
- Android 动态设置view的id,id设置为数字
- Android布局自定义Shap圆形ImageView可以单独设置背景与图片
- Android SufaceView 背景设置透明
- Android UI之代码动态设置ImageView的宽度和高度