Draw with fingers in android

Draw with fingers in android

Here I have written the sample coding for draw pictures using the finger in android. You can draw any shapes with your finger on the screen.

steps to create View

1. Create a class with extends View.
2. Create a view for drawing.

super.onSizeChanged(w,h, oldw, oldh);
mBitmap = Bitmap.createBitmap(w,h, Bitmap.Config.ARGB_8888);
mCanvas = new Canvas(mBitmap);
}

3.Override onTouch Event to get the drawing path.

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

4. final draw the path in the view using

canvas.drawPath(mPath,mPaint);

Example Coding 
CustomTextview.Java

package com.example.extendsviewinandroid;

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.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

public class CustomTextview extends View {
    Paint mPaint;
//MaskFilter
    mEmboss;
//MaskFilter
    mBlur;
    Bitmap mBitmap;
    Canvas mCanvas;
    Path mPath;
    Paint mBitmapPaint;
    Context mContext;

    public CustomTextview(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(20);
        mPath = new Path();
        mBitmapPaint = new Paint();
        mBitmapPaint.setColor(Color.RED);
    }

    @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);
        mCanvas = new Canvas(mBitmap);
    }

    @Override
    public void draw(Canvas canvas) 
    {
        super.draw(canvas);
        canvas.drawBitmap(mBitmap,0, 0, mBitmapPaint);
        canvas.drawPath(mPath,mPaint);
    }

    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    private void touch_start(float x, float y) {
        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;
        }
    }

    private void touch_up() {
        mPath.lineTo(mX, mY);
        mCanvas.drawPath(mPath,mPaint);
        mPath = new Path();
    }

    @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;
    }
}

MainActivity.class

package com.example.extendsviewinandroid;
import android.app.Activity;
import android.os.Bundle;
public
class MainActivity extends Activity { 
@Override protected
void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 
 setContentView(R.layout.activity_main);
}
}

activity_main.xml

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
><com.example.extendsviewinandroid.CustomTextview
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>

Screenshot

Download




 

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to Top