Open top menu





In this tutorial draw a finger feedback paint line. in this code implement draw canvas view and show finger touch circle path.

create subclass DrawingView  and extends View Class ,the DrawingView  class add on MainActivity .java .


setContentView(drawingview);

try this code........................................

MainActivity .java
..............................................................................................................................................
package com.example.androidbeginnerpoint;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;

public class MainActivity extends Activity {
DrawingView drawingview;
private Paint mPaintcanvas;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
drawingview = new DrawingView(this);
setContentView(drawingview);

mPaintcanvas = new Paint();
mPaintcanvas.setAntiAlias(true);
mPaintcanvas.setDither(true);
mPaintcanvas.setColor(Color.GREEN);
mPaintcanvas.setStyle(Paint.Style.STROKE);
mPaintcanvas.setStrokeJoin(Paint.Join.ROUND);
mPaintcanvas.setStrokeCap(Paint.Cap.ROUND);
mPaintcanvas.setStrokeWidth(40);
}

public class DrawingView extends View {

public int width;
public int height;
private Bitmap mBitmap;
private Canvas mCanvaspaint;
private Path mPath;
private Paint mBitmapPaint;
Context context;
private Paint circlePaint;
private Path circlePath;

public DrawingView(Context c) {
super(c);
context = c;
mPath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
circlePaint = new Paint();
circlePath = new Path();
circlePaint.setAntiAlias(true);
circlePaint.setColor(Color.BLUE);
circlePaint.setStyle(Paint.Style.STROKE);
circlePaint.setStrokeJoin(Paint.Join.MITER);
circlePaint.setStrokeWidth(4f);


}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);

mBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
mCanvaspaint = new Canvas(mBitmap);


}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);

canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaintcanvas);
canvas.drawPath(circlePath, circlePaint);
}

private float mX, mY;
private static final float TOUCH_TOLERANCE = 4;

private void touch_start(float x, float y) {
mPath.reset();
mPath.moveTo(x, y);
mX = x;
mY = y;


}

private void touch_move(float x, float y) {
float dx = Math.abs(x - mX);
float dy = Math.abs(y - mY);
if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
mPath.quadTo(mX, mY, (x + mX) / 2, (y + mY) / 2);
mX = x;
mY = y;

circlePath.reset();
circlePath.addCircle(mX, mY, 40, Path.Direction.CW);

}
}

private void touch_up() {
mPath.lineTo(mX, mY);
circlePath.reset();

mCanvaspaint.drawPath(mPath, mPaintcanvas);

mPath.reset();

}

@Override
public boolean onTouchEvent(MotionEvent event) {
float x = event.getX();
float y = event.getY();

switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
touch_start(x, y);
invalidate();
break;
case MotionEvent.ACTION_MOVE:
touch_move(x, y);
invalidate();
break;
case MotionEvent.ACTION_UP:
touch_up();
invalidate();
break;
}
return true;
}

public void clear() {
if (mCanvaspaint != null) {
mPaintcanvas.setARGB(0xff, 0, 0, 0);
mCanvaspaint.drawPaint(mPaintcanvas);
invalidate();

}
}


}

}

// this is aniamtion Code
/*
 * this.requestWindowFeature(Window.FEATURE_NO_TITLE);
 * this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
 * WindowManager.LayoutParams.FLAG_FULLSCREEN ); DisplayMetrics dm = new
 * DisplayMetrics();
 *
 * this.getWindowManager().getDefaultDisplay().getMetrics(dm);
 * this.setContentView(new WaterEffectView(this, dm.widthPixels,
 * dm.heightPixels));
 */

0 comments