我想要在觸摸視圖時執行操作。但是,觸動沒有迴應。該應用程序不會崩潰,它似乎忽略它。Android:OnTouch沒有響應。
public class CustomDrawableView extends View implements OnTouchListener
{
static final int width = 100;
static final int height = 50;
public CustomDrawableView(Context context)
{
super(context);
setFocusable(true);
setOnTouchListener(mCustomDrawableView);
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction()==MotionEvent.ACTION_DOWN)
{
x = 400;
return true;
}
else {
x = 300;
return false;
}
}
protected void onDraw(Canvas canvas)
{
int mCanvasHeight = canvas.getHeight();
int mCanvasWidth = canvas.getWidth();
canvas.save();
canvas.rotate(R,x,y);
if (y >= mCanvasHeight-100) {
y = 0;
}
RectF oval = new RectF(x, y, x + width, y
+ height); // set bounds of rectangle
Paint p = new Paint(); // set some paint options
p.setColor(Color.BLUE);
canvas.drawOval(oval, p);
canvas.restore();
invalidate();
}
}
我試了一堆不同的代碼來修復它。除了將setOnTouchListener(mCustomDrawable)更改爲mCustomDrawableView.setOnTouchListener(this)應用程序崩潰之外,它沒有任何功能。活動中還有更多的代碼,我沒有提出。
什麼說logcat? – poitroae 2012-02-14 19:29:50