public class FingureCanvas extends View {
private Bitmap bitmap;
private Path path;
private float current_x;
private float current_y;
private Paint paint;
private static final int TOUCH_DISTANCE = 10;
public FingureCanvas(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public FingureCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
private void init(){
bitmap = Bitmap.createBitmap(320, 480, Config.RGB_565);
paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeJoin(Join.ROUND);
paint.setStrokeCap(Cap.ROUND);
paint.setStrokeWidth(5);
}
public FingureCanvas(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if(path != null){
canvas.drawPath(path, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
onStart(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
onMove(event.getX(), event.getY());
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(current_x, current_y);
invalidate();
break;
}
return super.onTouchEvent(event);
}
private void onMove(float x,float y){
int distance_x = Math.abs((int)(x - current_x));
int distance_y = Math.abs((int)(y - current_y));
if(distance_x < TOUCH_DISTANCE && distance_y < TOUCH_DISTANCE){
return;
}
path.quadTo(current_x, current_y, (x + current_x) / 2, (y + current_y) / 2);
current_x = x;
current_y = y;
}
private void onStart(float x,float y){
path = new Path();
current_x = x;
current_y = y;
path.moveTo(current_x, current_y);
}
}
private Bitmap bitmap;
private Path path;
private float current_x;
private float current_y;
private Paint paint;
private static final int TOUCH_DISTANCE = 10;
public FingureCanvas(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}
public FingureCanvas(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
private void init(){
bitmap = Bitmap.createBitmap(320, 480, Config.RGB_565);
paint = new Paint();
paint.setStyle(Style.STROKE);
paint.setColor(Color.RED);
paint.setStrokeJoin(Join.ROUND);
paint.setStrokeCap(Cap.ROUND);
paint.setStrokeWidth(5);
}
public FingureCanvas(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if(path != null){
canvas.drawPath(path, paint);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
onStart(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
onMove(event.getX(), event.getY());
invalidate();
break;
case MotionEvent.ACTION_UP:
path.lineTo(current_x, current_y);
invalidate();
break;
}
return super.onTouchEvent(event);
}
private void onMove(float x,float y){
int distance_x = Math.abs((int)(x - current_x));
int distance_y = Math.abs((int)(y - current_y));
if(distance_x < TOUCH_DISTANCE && distance_y < TOUCH_DISTANCE){
return;
}
path.quadTo(current_x, current_y, (x + current_x) / 2, (y + current_y) / 2);
current_x = x;
current_y = y;
}
private void onStart(float x,float y){
path = new Path();
current_x = x;
current_y = y;
path.moveTo(current_x, current_y);
}
}