postheadericon onTouchEvent Example in Android



Definition : Create an application to draw line on the screen as user drag his finger.


File Name : E20.java


package bsr.exa;

import android.app.Activity;
import android.os.Bundle;

public class E20 extends Activity {
/**  @author Bipin S Rupadiya , www.bipinrupadiya.blogspot.com
*
*
*   20) Create an application to draw line on the screen as user drag his finger.
     *
     *  */



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(new ExploreTouchEvent(this, null));
}
}


File Name : ExploreTouchEvent.java 


package bsr.exa;


import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
/**  @author Bipin S Rupadiya , www.bipinrupadiya.com

 *  */
public class ExploreTouchEvent extends View {
private Paint paint = new Paint();
private Path path = new Path();

public ExploreTouchEvent(Context context, AttributeSet attrs) {
super(context, attrs);

paint.setAntiAlias(true);
paint.setStrokeWidth(6f);
paint.setColor(Color.WHITE);
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;
}
}



Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.