Reputation: 1743
In the following application the screen is always blinking for some reason, also there are drawing artifacts just above the text. Is it possible to fix this? I am running Android 4.4.2 on a Nexus 7 device.
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.
}
}
Upvotes: 0
Views: 2410
Reputation: 4367
In my case I need the TextureView to be cleared transparent - the following code did it for me:
Paint clearPaint = new Paint();
clearPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
while(true) {
Canvas canvas = surface.lockCanvas(null);
canvas.drawPaint(clearPaint);
...
surface.unlockCanvasAndPost(canvas);
}
Upvotes: 0
Reputation: 52313
Clear the Canvas right after you lock it, e.g. canvas.drawRGB(0,0,0)
. Otherwise you might pick up artifacts from previous frames.
Upvotes: 2