Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.karn.example;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.content.Context;
- import android.graphics.Bitmap;
- import android.graphics.Canvas;
- import android.graphics.Paint;
- import android.graphics.Path;
- import android.view.MotionEvent;
- import android.view.SurfaceHolder;
- import android.view.SurfaceView;
- public class MySurfaceView extends SurfaceView implements Runnable{
- private Thread drawingThread;
- private SurfaceHolder sHolder;
- private Bitmap bmp;
- private boolean isRunning = false;
- private Paint mPaint;
- private Canvas canvas;
- private Path mPath;
- private static final float MINP = 0.25f;
- private static final float MAXP = 0.75f;
- private float sX, sY;
- private static final float TOUCH_TOLERANCE = 4;
- public MySurfaceView (Context context)
- {
- super(context);
- //surface holder for controlling the surface view
- sHolder = getHolder();
- //setting the paint attribute
- setPaintAttr();
- //creating canvas for drawing on the bitmap
- canvas = new Canvas();
- mPath = new Path();
- }
- private void setPaintAttr()
- {
- mPaint = new Paint();
- mPaint.setAntiAlias(true);
- mPaint.setDither(true);
- mPaint.setStyle(Paint.Style.STROKE);
- mPaint.setColor(0xFF00FF00);
- mPaint.setStrokeJoin(Paint.Join.ROUND);
- mPaint.setStrokeCap(Paint.Cap.ROUND);
- mPaint.setStrokeWidth(12);
- }
- @Override
- public void run ( )
- {
- while(isRunning)
- {
- //check for the valid surface
- if(!sHolder.getSurface().isValid())
- continue;
- Canvas mCanvas = sHolder.lockCanvas();
- if(bmp != null)
- {
- mCanvas.drawBitmap(bmp, 0, 0, null);
- mCanvas.drawPath(mPath, mPaint);
- }
- sHolder.unlockCanvasAndPost(mCanvas);
- }
- }
- public void setBitmap(Bitmap _bmp)
- {
- //creating a mutable bitmap
- bmp = _bmp.copy(_bmp.getConfig(), true);
- //bitmap on which paths are drawn
- canvas.setBitmap(bmp);
- invalidate();
- }
- @Override
- public boolean onTouchEvent (MotionEvent event)
- {
- float x = event.getX();
- float y = event.getY();
- switch (event.getAction()) {
- case MotionEvent.ACTION_DOWN:
- touchStart(x, y);
- //invalidate();
- break;
- case MotionEvent.ACTION_MOVE:
- touchMove(x, y);
- //invalidate();
- break;
- case MotionEvent.ACTION_UP:
- touchUp();
- //invalidate();
- break;
- }
- return true;
- }
- private void touchStart(float x, float y) {
- mPath.reset();
- mPath.moveTo(x, y);
- sX = x;
- sY = y;
- }
- private void touchMove(float x, float y) {
- float dx = Math.abs(x - sX);
- float dy = Math.abs(y - sY);
- if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
- mPath.quadTo(sX, sY, (x + sX)/2, (y + sY)/2);
- sX = x;
- sY = y;
- }
- }
- private void touchUp() {
- mPath.lineTo(sX, sY);
- // commit the path to our offscreen
- canvas.drawPath(mPath, mPaint);
- // kill this so we don't double draw
- mPath.reset();
- }
- /**
- *
- * @brief pause method
- *
- *
- * @detail
- */
- public void pause()
- {
- //saving the image on pause
- saveBitmap();
- isRunning = false;
- while(true)
- {
- try
- {
- drawingThread.join();
- } catch (InterruptedException e)
- {
- e.printStackTrace();
- }
- break;
- }
- drawingThread = null;
- }
- public void resume()
- {
- isRunning = true;
- //creating new thread
- drawingThread = new Thread(this);
- //starting the thread
- drawingThread.start();
- }
- public void saveBitmap()
- {
- File resolveMeSDCard = new File("/mnt/sdcard/newImage.jpg");
- try {
- boolean fileCreated = resolveMeSDCard.createNewFile();
- } catch (IOException e) {
- e.printStackTrace();
- }
- File file = new File("/mnt/sdcard/newImage.jpg");
- if(file.exists()) {
- FileOutputStream out = null;
- try {
- out = new FileOutputStream(file);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- }
- bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement