在表面视图顶部覆盖画布,闪烁

时间:2013-12-17 16:25:37

标签: java android android-canvas

在我的XML文件中,我有一个VideoView和一个VideoOverlay类。 videoOverlay是透明的,并且上面有图纸,因此图片显示在videoView上。

    <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" />

这是VideoOverlay类

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);
        }
      }
      }

}

我现在有两个问题。

  1. 绘制的点,在设备上闪烁。 (他们眨眼)

  2. 我还在我的应用程序中使用了导航抽屉。当我打开导航抽屉时,绘图画布覆盖在导航抽屉上!

  3. 任何有关这些问题的帮助或建议都会非常感激!

0 个答案:

没有答案