Canvas overlay, Flash

In my XML file, I have a VideoView and a VideoOverlay class. The video version is transparent and has drawings on it, so it turns out that the drawings are on video video.

    <VideoView
       android:id="@+id/videoplayer"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content">

       </VideoView>


    <com.example.example.VideoOverlay
        android:id="@+id/videooverlay"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" />

Here is the VideoOverlay class

package com.example.example;

import java.util.ArrayList;
import java.util.Random;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.widget.VideoView;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.NavUtils;
import android.support.v4.widget.DrawerLayout;

public class VideoOverlay extends SurfaceView implements Runnable {

    Thread thread = null;
    SurfaceHolder surfaceHolder;
    volatile boolean running = false;

    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Random random;

    public void onResumeMySurfaceView(){
           running = true;
           thread = new Thread(this);
           thread.start();
          }

          public void onPauseMySurfaceView(){
           boolean retry = true;
           running = false;
           while(retry){
            try {
             thread.join();
             retry = false;
            } catch (InterruptedException e) {
             // TODO Auto-generated catch block
             e.printStackTrace();
            }
           }
          }

    public VideoOverlay(Context context)
    {
        super(context);
        surfaceHolder = getHolder();
        random = new Random();
        this.setZOrderMediaOverlay(true);
        this.setZOrderOnTop(true);
        surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);

    }
    public VideoOverlay(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        surfaceHolder = getHolder();
        random = new Random();
        this.setZOrderMediaOverlay(true);
        this.setZOrderOnTop(true);
        surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);

    }
    public VideoOverlay(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        surfaceHolder = getHolder();
        random = new Random();
        this.setZOrderMediaOverlay(true);
        this.setZOrderOnTop(true);
        surfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
    }
// Other stuff

      @Override
      public void run() {
       // TODO Auto-generated method stub
       while(running){
        if(surfaceHolder.getSurface().isValid()){
         Canvas canvas = surfaceHolder.lockCanvas();
         //... actual drawing on canvas

         paint.setStyle(Paint.Style.STROKE);
            paint.setStrokeWidth(3);

         int w = canvas.getWidth();
         int h = canvas.getHeight();
         int x = random.nextInt(w-1); 
         int y = random.nextInt(h-1);
         int r = random.nextInt(255);
         int g = random.nextInt(255);
         int b = random.nextInt(255);
         paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
         canvas.drawPoint(x, y, paint);

         surfaceHolder.unlockCanvasAndPost(canvas);
        }
      }
      }

}

I have two questions.

  • The dots that are drawn flash on the device. (They blink)

  • I also use the navigation box in my application. When I open the navigation box, the picture canvas is superimposed on the navigation box!

Any help or advice on any of these issues would really be appreciated!

+4
source share

All Articles