我試圖做一個愚蠢的應用程序,其中使用Canvas
繪製的「HelloWorld!!
」字符串移動。該字符串的位置(x_new, y_new)
僅僅是其舊位置(x_old, y_old)
加(x, y)
由ACTION_UP
事件返回。問題在於事件ACTION_UP
它不被識別。功能Log.d()
功能總是打印motionEvent(28254): MotionEvent{4050df70 action=4 x=354.0 y=415.0 pressure=0.20000002 size=0.26666668}
,其中action=4
永不改變。你有什麼建議嗎?謝謝!!Android視圖畫布onTouchEvent
class myView extends View {
private Canvas canvas;
private int x;
private int y;
public HUDView(Context context) {
super(context);
this.x=5;
this.y=5;
}
protected void onDraw(Canvas canvas) {
this.canvas=canvas;
this.canvas.drawText("HelloWorld!!", x, y, mLoadPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
Log.d("motionEvent", event.toString());
if (event.getAction()==MotionEvent.ACTION_UP){
Log.d("motionEvent", "action_up");
this.x+=(int)event.getX();
this.y+=(int)event.getY();
return true;
}
this.postInvalidate();
return true;
}
}
構造函數的名稱有誤 - 尼斯發現 – Blackbelt
感謝您的答案和錯誤對不起!這個代碼對我來說不起作用,即使你的建議也不行。我仍然只有'動作= 4'動作事件 – displayName
'動作= 4'代表ACTION_OUTSIDE這意味着你已經犯了一個錯誤,部署這個視圖和行動發生在它之外。 – cjds