Advertisement
Guest User

Untitled

a guest
Sep 21st, 2012
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.91 KB | None | 0 0
  1. package org.karn.example;
  2.  
  3. import java.io.File;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7.  
  8. import android.content.Context;
  9. import android.graphics.Bitmap;
  10. import android.graphics.Canvas;
  11. import android.graphics.Paint;
  12. import android.graphics.Path;
  13. import android.view.MotionEvent;
  14. import android.view.SurfaceHolder;
  15. import android.view.SurfaceView;
  16.  
  17.  
  18. public class MySurfaceView extends SurfaceView implements Runnable{
  19.  
  20.     private Thread drawingThread;
  21.     private SurfaceHolder sHolder;
  22.     private Bitmap bmp;
  23.     private boolean isRunning = false;
  24.     private Paint mPaint;
  25.     private Canvas  canvas;
  26.     private Path    mPath;
  27.  
  28.     private static final float MINP = 0.25f;
  29.     private static final float MAXP = 0.75f;
  30.  
  31.     private float sX, sY;
  32.     private static final float TOUCH_TOLERANCE = 4;
  33.  
  34.     public MySurfaceView (Context context)
  35.     {
  36.         super(context);
  37.         //surface holder for controlling the surface view
  38.         sHolder = getHolder();
  39.         //setting the paint attribute
  40.         setPaintAttr();
  41.         //creating canvas for drawing on the bitmap
  42.         canvas = new Canvas();
  43.  
  44.         mPath = new Path();
  45.     }
  46.  
  47.     private void setPaintAttr()
  48.     {
  49.         mPaint = new Paint();
  50.         mPaint.setAntiAlias(true);
  51.         mPaint.setDither(true);
  52.         mPaint.setStyle(Paint.Style.STROKE);
  53.         mPaint.setColor(0xFF00FF00);
  54.         mPaint.setStrokeJoin(Paint.Join.ROUND);
  55.         mPaint.setStrokeCap(Paint.Cap.ROUND);
  56.         mPaint.setStrokeWidth(12);    
  57.     }
  58.  
  59.     @Override
  60.     public void run ( )
  61.     {
  62.         while(isRunning)
  63.         {
  64.             //check for the valid surface
  65.             if(!sHolder.getSurface().isValid())
  66.                 continue;
  67.  
  68.             Canvas mCanvas = sHolder.lockCanvas();
  69.  
  70.             if(bmp != null)
  71.             {
  72.                 mCanvas.drawBitmap(bmp, 0, 0, null);
  73.                 mCanvas.drawPath(mPath, mPaint);
  74.             }
  75.             sHolder.unlockCanvasAndPost(mCanvas);
  76.         }
  77.     }
  78.  
  79.  
  80.     public void setBitmap(Bitmap _bmp)
  81.     {
  82.         //creating a mutable bitmap
  83.         bmp = _bmp.copy(_bmp.getConfig(), true);
  84.         //bitmap on which paths are drawn
  85.         canvas.setBitmap(bmp);
  86.         invalidate();
  87.     }
  88.  
  89.  
  90.  
  91.     @Override
  92.     public boolean onTouchEvent (MotionEvent event)
  93.     {        
  94.         float x = event.getX();
  95.         float y = event.getY();
  96.  
  97.         switch (event.getAction()) {
  98.             case MotionEvent.ACTION_DOWN:
  99.                 touchStart(x, y);
  100.                 //invalidate();
  101.                 break;
  102.             case MotionEvent.ACTION_MOVE:
  103.                 touchMove(x, y);
  104.                 //invalidate();
  105.                 break;
  106.             case MotionEvent.ACTION_UP:
  107.                 touchUp();
  108.                 //invalidate();
  109.                 break;
  110.         }
  111.         return true;
  112.     }
  113.  
  114.     private void touchStart(float x, float y) {
  115.         mPath.reset();
  116.         mPath.moveTo(x, y);
  117.         sX = x;
  118.         sY = y;
  119.     }
  120.  
  121.     private void touchMove(float x, float y) {
  122.         float dx = Math.abs(x - sX);
  123.         float dy = Math.abs(y - sY);
  124.         if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
  125.             mPath.quadTo(sX, sY, (x + sX)/2, (y + sY)/2);
  126.             sX = x;
  127.             sY = y;
  128.         }
  129.     }
  130.     private void touchUp() {
  131.         mPath.lineTo(sX, sY);
  132.         // commit the path to our offscreen
  133.         canvas.drawPath(mPath, mPaint);
  134.         // kill this so we don't double draw
  135.         mPath.reset();
  136.     }
  137.  
  138.     /**
  139.      *
  140.      * @brief pause method
  141.      *
  142.      *
  143.      * @detail
  144.      */
  145.     public void pause()
  146.     {
  147.         //saving the image on pause
  148.         saveBitmap();
  149.         isRunning = false;
  150.         while(true)
  151.         {
  152.             try
  153.             {
  154.                 drawingThread.join();
  155.             } catch (InterruptedException e)
  156.             {
  157.                 e.printStackTrace();
  158.             }
  159.             break;
  160.         }
  161.         drawingThread = null;
  162.     }
  163.  
  164.  
  165.     public void resume()
  166.     {
  167.         isRunning = true;
  168.         //creating new thread
  169.         drawingThread = new Thread(this);
  170.         //starting the thread
  171.         drawingThread.start();
  172.     }
  173.  
  174.     public void saveBitmap()
  175.     {
  176.         File resolveMeSDCard = new File("/mnt/sdcard/newImage.jpg");
  177.         try {
  178.             boolean fileCreated = resolveMeSDCard.createNewFile();
  179.         } catch (IOException e) {
  180.             e.printStackTrace();
  181.         }
  182.         File file = new File("/mnt/sdcard/newImage.jpg");
  183.  
  184.         if(file.exists()) {
  185.             FileOutputStream out = null;
  186.             try {
  187.                 out = new FileOutputStream(file);
  188.             } catch (FileNotFoundException e) {
  189.                 e.printStackTrace();
  190.             }
  191.             bmp.compress(Bitmap.CompressFormat.JPEG, 100, out);
  192.         }
  193.     }
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement