package com.TestSingleTouch;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
public class TestSingleTouch extends Activity {
public class TouchView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float x, y;
boolean touching = false;
public TouchView(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
if(touching){
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(1);
paint.setColor(Color.WHITE);
canvas.drawCircle(x, y, 50f, paint);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// TODO Auto-generated method stub
setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),
MeasureSpec.getSize(heightMeasureSpec));
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
int action = event.getAction();
switch(action){
case MotionEvent.ACTION_MOVE:
x = event.getX();
y = event.getY();
touching = true;
break;
case MotionEvent.ACTION_DOWN:
x = event.getX();
y = event.getY();
touching = true;
break;
case MotionEvent.ACTION_UP:
touching = false;
break;
default:
touching = false;
}
invalidate();
return true;
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new TouchView(this));
}
}
Related Post:
- Detect Touch Event, test on Custom View; part II
- Detect Touch Event
- Detect Multi-Touch Event
- Detect Multi-Touch Event, test on Custom View
- Touch to move Bitmap inside a custom View
Hi Eric,
ReplyDeleteGreat Tutorials ... Hats off.
I have one doubt , I tried with your program, its working great, but when i tried to debug/execute, the control never enters in to actions (UP, MOVE)..., only the DOWN action is called all time.
Can you Please say me why is this happening ?
Thank you
With Regards,
Nandagopal T
Nandagopal T,
ReplyDeletePlease check the part II. I modified it for easier tracing event.
how can i move an image by using touch?
ReplyDeletehello,
ReplyDeleteIs it Touch to move Bitmap inside a custom View?