2014-04-30 11 views
0

다음 응용 프로그램에서 화면은 항상 어떤 이유로 든 깜박이고 텍스트 바로 위에 드로잉 아티팩트가 있습니다. 이 문제를 해결할 수 있습니까? 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

캔버스를 잠그면 바로 지 웁니다. canvas.drawRGB(0,0,0). 그렇지 않으면 이전 프레임에서 이슈를 픽업 할 수 있습니다. 나는 TextureView 필요 내 경우

+0

이 일이, 감사합니다! – iggy

0

투명 삭제합니다 - 다음 코드는 나를 위해 그것을했다 :

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

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