1. Are you ready for the Galaxy S20? Here is everything we know so far!

Draw on SurfaceView(VideoPlayer) Canvas

Discussion in 'Android Development' started by sapegas, Nov 4, 2010.

  1. sapegas

    sapegas Lurker
    Thread Starter

    Hi,

    I'm trying to draw some primitives on SurfaceView Canvas, but I can't do this. I need to implement a VideoPlayer(SurfaceView) with ability to draw on it(and save user's drawing in future). Can I do this anyhow? Is it possible on Android?

    There is my code (only drawing on player):
    Code (Text):
    1.  
    2. package com.example.canvasdrawing;
    3.  
    4. import java.io.IOException;
    5. import java.util.ArrayList;
    6.  
    7. import android.app.Activity;
    8. import android.content.Context;
    9. import android.graphics.Canvas;
    10. import android.graphics.Color;
    11. import android.graphics.Paint;
    12. import android.graphics.Path;
    13. import android.media.MediaPlayer;
    14. import android.os.Bundle;
    15. import android.util.Log;
    16. import android.view.Gravity;
    17. import android.view.MotionEvent;
    18. import android.view.SurfaceHolder;
    19. import android.view.SurfaceView;
    20. import android.widget.Button;
    21. import android.widget.VideoView;
    22.  
    23. public class CanvasDrawing extends  Activity   {
    24.    
    25.     private ArrayList<Path> _graphics = new ArrayList<Path>();
    26.     private Paint mPaint;
    27.     MediaPlayer mp = new MediaPlayer();
    28.    
    29.     @Override
    30.     public void onCreate(Bundle savedInstanceState) {
    31.         super.onCreate(savedInstanceState);
    32.         //setContentView(R.layout.main);
    33.         setContentView(new DrawingPanel(this));
    34.         Log.d("mytag", "1");
    35.         mPaint = new Paint();
    36.         mPaint.setDither(true);
    37.         mPaint.setAntiAlias(true);
    38.         mPaint.setColor(0xFFFFFF00);
    39.         mPaint.setStyle(Paint.Style.STROKE);
    40.         mPaint.setStrokeJoin(Paint.Join.ROUND);
    41.         mPaint.setStrokeCap(Paint.Cap.ROUND);
    42.         mPaint.setStrokeWidth(3);
    43.         Log.d("mytag", "2");
    44.        
    45.     }
    46.    
    47.    
    48.     class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
    49.         private DrawingThread _thread;
    50.         private Path path;
    51.        
    52.         public DrawingPanel(Context context) {
    53.             super(context);
    54.             getHolder().addCallback(this);
    55.             //
    56.             getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
    57.             //getHolder().setFixedSize(100, 100);
    58.             //
    59.             Log.d("mytag", "3");
    60.             _thread = new DrawingThread(getHolder(), this);
    61.             Log.d("mytag", "4");
    62.         }
    63.        
    64.         public boolean onTouchEvent(MotionEvent event) {
    65.             synchronized (_thread.getSurfaceHolder()) {
    66.                 if(event.getAction() == MotionEvent.ACTION_DOWN){
    67.                     path = new Path();
    68.                     path.moveTo(event.getX(), event.getY());
    69.                     path.lineTo(event.getX(), event.getY());
    70.                 }else if(event.getAction() == MotionEvent.ACTION_MOVE){
    71.                     path.lineTo(event.getX(), event.getY());
    72.                 }else if(event.getAction() == MotionEvent.ACTION_UP){
    73.                     path.lineTo(event.getX(), event.getY());
    74.                     _graphics.add(path);
    75.                 }
    76.                
    77.                 return true;
    78.             }
    79.         }
    80.        
    81.         @Override
    82.         public void onDraw(Canvas canvas) {
    83.             for (Path path : _graphics) {
    84.                 //canvas.drawPoint(graphic.x, graphic.y, mPaint);
    85.                 canvas.drawPath(path, mPaint);
    86.             }
    87.         }
    88.        
    89.         public void surfaceChanged(SurfaceHolder holder, int format, int width,
    90.                                    int height) {
    91.             // TODO Auto-generated method stub
    92.            
    93.         }
    94.        
    95.         public void surfaceCreated(SurfaceHolder holder) {
    96.             // TODO Auto-generated method stub
    97.             mp = new MediaPlayer();
    98.             mp.setDisplay(holder);
    99.             try {
    100.                 mp.setDataSource("/data/test.3gp");
    101.                 mp.prepare();
    102.                 mp.start();
    103.                 Log.d("mytag", "5");
    104.             } catch (IllegalArgumentException e) {
    105.                 // TODO Auto-generated catch block
    106.                 e.printStackTrace();
    107.             } catch (IllegalStateException e) {
    108.                 // TODO Auto-generated catch block
    109.                 e.printStackTrace();
    110.             } catch (IOException e) {
    111.                 // TODO Auto-generated catch block
    112.                 e.printStackTrace();
    113.             }
    114.             //
    115.             _thread.setRunning(true);
    116.             _thread.start();
    117.             Log.d("mytag", "6");
    118.         }
    119.        
    120.         public void surfaceDestroyed(SurfaceHolder holder) {
    121.             // TODO Auto-generated method stub
    122.             mp.release();
    123.            
    124.             boolean retry = true;
    125.             _thread.setRunning(false);
    126.             while (retry) {
    127.                 try {
    128.                     _thread.join();
    129.                     retry = false;
    130.                 } catch (InterruptedException e) {
    131.                     // we will try it again and again...
    132.                 }
    133.             }
    134.         }
    135.     }
    136.    
    137.     class DrawingThread extends Thread {
    138.         private SurfaceHolder _surfaceHolder;
    139.         private DrawingPanel _panel;
    140.         private boolean _run = false;
    141.        
    142.         public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
    143.             _surfaceHolder = surfaceHolder;
    144.             _panel = panel;
    145.         }
    146.        
    147.         public void setRunning(boolean run) {
    148.             _run = run;
    149.         }
    150.        
    151.         public SurfaceHolder getSurfaceHolder() {
    152.             return _surfaceHolder;
    153.         }
    154.        
    155.         @Override
    156.         public void run() {
    157.             Canvas c;
    158.             while (_run) {
    159.                 c = null;
    160.                 try {
    161.                     c = _surfaceHolder.lockCanvas(null);
    162.                     synchronized (_surfaceHolder) {
    163.                         _panel.onDraw(c);
    164.                     }
    165.                 } finally {
    166.                     // do this in a finally so that if an exception is thrown
    167.                     // during the above, we don't leave the Surface in an
    168.                     // inconsistent state
    169.                     if (c != null) {
    170.                         _surfaceHolder.unlockCanvasAndPost(c);
    171.                     }
    172.                 }
    173.             }
    174.         }
    175.     }
    176.    
    177.    
    178.    
    179. }
    180.  
    Any ideas or help here?
    Thanks.
     



    1. Download the Forums for Android™ app!


      Download

       
Loading...
Similar Threads - Draw SurfaceView VideoPlayer
  1. dangerkittah_1117
    Replies:
    1
    Views:
    722
  2. App Update
    Replies:
    0
    Views:
    729
  3. App Update
    Replies:
    0
    Views:
    469
  4. Art Plays
    Replies:
    2
    Views:
    671
  5. Biffos
    Replies:
    6
    Views:
    687
  6. App Update
    Replies:
    0
    Views:
    614
  7. electricpunch
    Replies:
    1
    Views:
    499
  8. App Update
    Replies:
    1
    Views:
    640
  9. tekir90
    Replies:
    0
    Views:
    2,530
  10. tobotech
    Replies:
    1
    Views:
    1,390

Share This Page

Loading...