如图,B不动时是白色底,点击上半部分+号为灰色,-号为白色;点击下半部分-号为灰色,+号为白色。这个按钮该如何实现?菜鸟求指导。
解决方案 »
- android百度地图覆盖物多线程问题
- 大牛呀,给个soundTouch的demo吧!
- 谁能帮我实现一个简单的效果。
- 为什么onScroll事件只对左右滑屏有响应,上下不行
- Android开发getApplicationContext出错
- pppd: This system lacks kernel support for PPP
- android--ImageView(context)的疑问?
- android中怎么实现截取播放器播放画面?
- 打开首页,是SlidingMenu,想默认直接显示左侧的菜单(listview)而不是内容页(content
- 怎么我发现Arcgis不容百度...
- android手机客户端获取图片问题
- Activity没有转换过来
因为可以为单个imageButton设置click效果..
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.Button;/**
* 双向按钮
*
* @author chendy
*
*/
public class TwoKeyButton extends Button {
// 禁用时的背景图
private Drawable disBitmap;
// 正常状态时的背景图
private Drawable normalBitmap;
// 上半部分被按下的背景图
private Drawable upBitmap;
// 下半部分被按下的背景图
private Drawable downBitmap; private String TAG = getClass().getName(); private onKeyTouchListener keyTouchListener; public void setOnKeyTouchListener(onKeyTouchListener keyTouchListener) {
this.keyTouchListener = keyTouchListener;
} public TwoKeyButton(Context context) {
super(context);
} @SuppressWarnings("deprecation")
public TwoKeyButton(Context context, AttributeSet attrs) {
super(context, attrs);
int resId = 0;
resId = attrs.getAttributeResourceValue(null, "disableBackground", 0);
if (0 != resId) {
disBitmap = getResources().getDrawable(resId);
}
resId = attrs.getAttributeResourceValue(null, "normalBackground", 0);
if (0 != resId) {
normalBitmap = getResources().getDrawable(resId);
setBackgroundDrawable(normalBitmap);
}
resId = attrs.getAttributeResourceValue(null, "upBackground", 0);
if (0 != resId) {
upBitmap = getResources().getDrawable(resId);
}
resId = attrs.getAttributeResourceValue(null, "downBackground", 0);
if (0 != resId) {
downBitmap = getResources().getDrawable(resId);
}
} @SuppressWarnings("deprecation")
@Override
public void setEnabled(boolean enabled) {
if (enabled) {
if (null != normalBitmap)
setBackgroundDrawable(normalBitmap);
} else {
if (null != disBitmap)
setBackgroundDrawable(disBitmap);
}
super.setEnabled(enabled);
} @SuppressWarnings("deprecation")
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled())
return super.onTouchEvent(event);
float y = 0;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
Log.i(TAG, "ACTION_DOWN");
y = event.getY();
// 按钮的上半部分被按下
if (y < getHeight() / 2) {
if (null != keyTouchListener)
keyTouchListener.onUpKeyTouch(this);
Log.i(TAG, "BUTTON_UP");
if (null != upBitmap)
setBackgroundDrawable(upBitmap);
} else {
if (null != keyTouchListener)
keyTouchListener.onDownKeyTouch(this);
Log.i(TAG, "BUTTON_DOWN");
if (null != downBitmap)
setBackgroundDrawable(downBitmap);
}
break;
case MotionEvent.ACTION_UP:
if (null != keyTouchListener)
keyTouchListener.onKeyTouchEnd(this);
Log.i(TAG, "ACTION_DOWN");
if (null != normalBitmap)
setBackgroundDrawable(normalBitmap);
break;
default:
break;
}
return super.onTouchEvent(event);
} public interface onKeyTouchListener {
/**
* Up键被按下
*/
void onUpKeyTouch(TwoKeyButton send); /**
* Down键被按下
*/
void onDownKeyTouch(TwoKeyButton send); /**
* 触摸结束
*/
void onKeyTouchEnd(TwoKeyButton send);
}}
imageButtonb.setOnTouchListener(new OnTouchListener() { @Override
public boolean onTouch(View v, MotionEvent event) {
float y = imageButtonb.getHeight();//获取b按钮的高度
int action = event.getAction();
switch (action) { case MotionEvent.ACTION_DOWN:
//判断鼠标点下的纵坐标与按钮一般的大小
if (event.getY() < y / 2) {
imageButtonb.setImageResource(R.drawable.b_up);//设置按钮图片
} else {
imageButtonb.setImageResource(R.drawable.b_down);
}
break;
case MotionEvent.ACTION_UP:
imageButtonb.setImageResource(R.drawable.b);
break;
default:
break;
}
return false;
}
});