我尝试过Romain Guy的TextureView示例代码(http://pastebin.com/J4uDgrZ8),效果很好。但是当我将lockCanvas(null)更改为lockCanvas(new Rect(x,y,x + 20,y + 20))时,示例开始闪烁。 似乎'lockCanvas(Rect)'不能很好地适用于TextureView或任何其他原因?
我在Android 4.0.3上使用摩托罗拉XOOM。 谢谢你的帮助!
我修改的代码如下:
public void run() {
float x = 0.0f;
float y = 0.0f;
float speedX = 5.0f;
float speedY = 3.0f;
Paint paint = new Paint();
paint.setColor(0xff00ff00);
while (mRunning && !Thread.interrupted()) {
//final Canvas canvas = mSurface.lockCanvas(null);
**final Canvas canvas = mSurface.lockCanvas(new Rect((int)x, (int)y,
(int)(x+20.0f), (int)(y+20.0f)));**
try {
canvas.drawColor(0x00000000, PorterDuff.Mode.CLEAR);
canvas.drawRect(x, y, x + 20.0f, y + 20.0f, paint);
} finally {
mSurface.unlockCanvasAndPost(canvas);
}
if (x + 20.0f + speedX >= mSurface.getWidth() || x + speedX <= 0.0f) {
speedX = -speedX;
}
if (y + 20.0f + speedY >= mSurface.getHeight() || y + speedY <= 0.0f) {
speedY = -speedY;
}
x += speedX;
y += speedY;
try {
//Thread.sleep(15);
**Thread.sleep(1);**
} catch (InterruptedException e) {
// Interrupted
}
}
我仔细检查过,发现它是“Thread.sleep(1)”和“lockCanvas(Rect)”导致闪烁。当使用lockCanvas(null)时,sleep(1)就可以了。所以lockCanvas(Rect)不能像lockCanvas(null)??
那样快速刷新