我使用Bitmap绘制菜单图片,当我尝试使用TouchEvent在我的MenuLoading Activity中加载另一个Activity时它不起作用。
请问有人能解决这个问题吗?提前谢谢你。
这是我使用的代码:
菜单视图:
public class MenuView extends View{
private Bitmap menu;
boolean result = false;
public MenuView(Context context) {
super(context);
menu = BitmapFactory.decodeResource(getResources(), R.drawable.menu2);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(menu, 0, 0, null);
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN: {
// Code on finger down
float posX = ev.getX();
float posY = ev.getY();
float x1 = 296, x2 = 528, y1 = 246, y2 = 304;
if ((posX >= x1 && posX <= x2) && (posY >= y1 && posY <= y2)) {
// we are in the square
result = true;
}
else {
// we are somewhere else
}
break;
}
case MotionEvent.ACTION_UP: {
// Code on finger up
break;
}
case MotionEvent.ACTION_MOVE: {
// Code on finger move
break;
}
}
invalidate();
return result;
}
}
和 MenuLoading 活动:
public class MenuLoading extends Activity {
MenuView View;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View = new MenuView(this);
setContentView(View);
if (View.result == true){
Intent i = new Intent (this, GameLoading.class);
startActivity(i);
}
}
}