Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development

Application Development Dev Lounge for the Coder Folks



Reply
 
LinkBack Thread Tools
Old November 4th, 2010, 01:03 AM   #1 (permalink)
New Member
 
Join Date: Nov 2010
Posts: 1
 
Device(s):
Thanks: 0
Thanked 0 Times in 0 Posts
Default Draw on SurfaceView(VideoPlayer) Canvas

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:
package com.example.canvasdrawing;

import java.io.IOException;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Button;
import android.widget.VideoView;

public class CanvasDrawing extends  Activity   {
	
	private ArrayList<Path> _graphics = new ArrayList<Path>();
	private Paint mPaint;
	MediaPlayer mp = new MediaPlayer();
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.main);
        setContentView(new DrawingPanel(this));
        Log.d("mytag", "1");
        mPaint = new Paint();
        mPaint.setDither(true);
        mPaint.setAntiAlias(true);
        mPaint.setColor(0xFFFFFF00);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(3);
        Log.d("mytag", "2");
        
	}
	
	
	class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
		private DrawingThread _thread;
		private Path path;
		
		public DrawingPanel(Context context) {
			super(context);
            getHolder().addCallback(this);
            //
            getHolder().setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
            //getHolder().setFixedSize(100, 100);
            //
            Log.d("mytag", "3");
            _thread = new DrawingThread(getHolder(), this);
            Log.d("mytag", "4");
		}
		
		public boolean onTouchEvent(MotionEvent event) {
            synchronized (_thread.getSurfaceHolder()) {
            	if(event.getAction() == MotionEvent.ACTION_DOWN){
            		path = new Path();
            		path.moveTo(event.getX(), event.getY());
            		path.lineTo(event.getX(), event.getY());
            	}else if(event.getAction() == MotionEvent.ACTION_MOVE){
            		path.lineTo(event.getX(), event.getY());
            	}else if(event.getAction() == MotionEvent.ACTION_UP){
            		path.lineTo(event.getX(), event.getY());
            		_graphics.add(path);
            	}
            	
            	return true;
            }
		}
		
		@Override
        public void onDraw(Canvas canvas) {
			for (Path path : _graphics) {
				//canvas.drawPoint(graphic.x, graphic.y, mPaint);
				canvas.drawPath(path, mPaint);
			}
		}
		
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
								   int height) {
			// TODO Auto-generated method stub
			
		}
		
		public void surfaceCreated(SurfaceHolder holder) {
			// TODO Auto-generated method stub
            mp = new MediaPlayer();
            mp.setDisplay(holder);
            try {
				mp.setDataSource("/data/test.3gp");
				mp.prepare();
				mp.start();
				Log.d("mytag", "5");
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			//
			_thread.setRunning(true);
            _thread.start();
            Log.d("mytag", "6");
        }
		
		public void surfaceDestroyed(SurfaceHolder holder) {
			// TODO Auto-generated method stub
			mp.release();
			
			boolean retry = true;
            _thread.setRunning(false);
            while (retry) {
                try {
                    _thread.join();
                    retry = false;
                } catch (InterruptedException e) {
                    // we will try it again and again...
                }
            }
		}
	}
	
	class DrawingThread extends Thread {
        private SurfaceHolder _surfaceHolder;
        private DrawingPanel _panel;
        private boolean _run = false;
		
        public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
            _surfaceHolder = surfaceHolder;
            _panel = panel;
        }
		
        public void setRunning(boolean run) {
            _run = run;
        }
		
        public SurfaceHolder getSurfaceHolder() {
            return _surfaceHolder;
        }
		
        @Override
        public void run() {
            Canvas c;
            while (_run) {
                c = null;
                try {
                    c = _surfaceHolder.lockCanvas(null);
                    synchronized (_surfaceHolder) {
                        _panel.onDraw(c);
                    }
                } finally {
                    // do this in a finally so that if an exception is thrown
                    // during the above, we don't leave the Surface in an
                    // inconsistent state
                    if (c != null) {
                        _surfaceHolder.unlockCanvasAndPost(c);
                    }
                }
            }
        }
    }
	
	
	
}
Any ideas or help here?
Thanks.

sapegas is offline  
Reply With Quote
Sponsors
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development User CP
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Draw on canvas (SurfaceView) hollowback Application Development 2 April 4th, 2012 09:24 PM
A Subtitle VideoPlayer Out There? MDesire2010 HTC Desire 1 August 31st, 2010 12:53 AM
Redrawing a portion of a surfaceView ophanin Application Development 1 July 20th, 2010 09:29 AM
Add Button to SurfaceView safibaba Application Development 0 June 17th, 2010 09:50 AM
surfaceview setbackgrounddrawable() mrqs Application Development 1 March 19th, 2010 11:22 AM



All times are GMT -5. The time now is 11:34 AM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo