Draw with fingers in android
here I have written the sample coding for draw picture using finger in android. You can draw any shapes with your finger in the screen.1)Create class with extends View .
2)Create a view for draw.
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
anvas.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); // TODO Auto-generated constructor stub 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); } @Overrideprotected 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); } @Overridepublic void draw(Canvas canvas) {// TODO Auto-generated method stub 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.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; } }private void touch_up() { mPath.lineTo(mX, mY); // commit the path to our offscreen mCanvas.drawPath(mPath, mPaint); //mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SCREEN)); // kill this so we don't double drawmPath.reset(); // mPath= new Path(); } @Overridepublic 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:
No comments:
Post a Comment