我剛開始使用觸摸事件。所以我搜索一個例子,發現這個http://www.vogella.com/articles/AndroidTouch/article.html ..教程是偉大的,但有一點是,當我運行的代碼。我將手指向各個方向移動,而不是在cavas中顯示..我的意思是它沒有繪製任何東西。安卓觸摸不顯示在畫布上
這裏是我的代碼:
public class SingleTouchEventView extends View {
private Paint paint = new Paint();
private Path path = new Path();
public SingleTouchEventView(Context context, AttributeSet attrs) {
super(context, attrs);
paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeJoin(Paint.Join.ROUND);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawPath(path,paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
float eventX = event.getX();
float eventY = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
path.moveTo(eventX, eventY);
return true;
case MotionEvent.ACTION_MOVE:
path.lineTo(eventX, eventY);
break;
case MotionEvent.ACTION_UP:
// nothing to do
break;
default:
return false;
}
// Schedules a repaint.
invalidate();
return true;
}
}
和我的主要activty
public class SingleTouchActivity extends Activity {
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new SingleTouchEventView(this,null));
}
}
我失去的是什麼?由於
這是一個總猜測,但不要在'ACTION_DOWN'上調用'return' – seaplain
Ahm does not work .. :( – thenewbie
代碼是否碰到了你的構造函數?在日誌中添加我唯一的原因是因爲你的構造函數被設置爲從'xml'中調用,你所做的調用看起來有效,但是一個簡單的檢查不會傷害 – seaplain