Android纹理视图闪烁

时间:2014-04-30 16:26:06

标签: android drawing textureview

在以下应用程序中,屏幕也会因某种原因而闪烁 在文本上方有绘图工件。有可能解决这个问题吗?我在Nexus 7设备上运行Android 4.4.2。

package com.example.TestSurfaceView;

import android.app.Activity;
import android.graphics.*;
import android.os.Bundle;
import java.util.Random;
import android.os.SystemClock;
import android.view.TextureView;

public class MyActivity extends Activity implements TextureView.SurfaceTextureListener, Runnable {

    TextureView view_;
    Thread thread_;
    long prev_ = -1;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        view_ = new TextureView(this);
        view_.setSurfaceTextureListener(this);
        thread_ = new Thread(this);
        setContentView(view_);
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
    }

    @Override
    public void run() {
        while(true) {
            Canvas canvas = view_.lockCanvas();
            Paint myPaint = new Paint();
            myPaint.setColor(Color.WHITE);
            myPaint.setStrokeWidth(10);
            canvas.drawRect(100, 100, 300, 300, myPaint);

            Random random = new Random();
            Paint paint = new Paint();
            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);

            if(prev_ == -1)
                prev_ = SystemClock.elapsedRealtime();
            else {
                long curr = SystemClock.elapsedRealtime();
                long diff = curr-prev_;
                Paint text = new Paint(Color.WHITE);
                text.setTextSize(15);
                canvas.drawText("FPS: " + String.valueOf(1000.0 / diff), 100, 100, text);
                prev_ = curr;
            }

            view_.unlockCanvasAndPost(canvas);
        }
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
    }

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int i, int i2) {
        thread_.start();
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int i, int i2) {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) {
        return false;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) {
        //To change body of implemented methods use File | Settings | File Templates.
    }
}

2 个答案:

答案 0 :(得分:2)

锁定后立即清除画布,例如canvas.drawRGB(0,0,0)。否则,您可能会从之前的帧中拾取工件。

答案 1 :(得分:0)

在我的情况下,我需要将TextureView清除透明 - 以下代码为我做了:

  Paint clearPaint = new Paint();
  clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));

  while(true) {
      Canvas canvas = surface.lockCanvas(null);
      canvas.drawPaint(clearPaint);
      ...
      surface.unlockCanvasAndPost(canvas);
  }