Stu*_*Stu 1 android clear surfaceview
我创建了一个扩展SurfaceView的类,以便循环一系列ARGB位图.这主要起作用,除了基础位图的状态(通常但不总是)为每个新帧保留.
换句话说,如果第一帧I显示是不透明的,并且后续帧是透明的,则在绘制新帧时不会清除原始帧中的不透明像素.
这种行为让我感到困惑,因为SurfaceHolder.lockCanvas()的文档特别指出:
"在SurfaceCanvas()和lockCanvas()之间永远不会保留Surface的内容,因此,必须写入Surface区域中的每个像素."
如果我只有一个坚实的背景,那么调用canvas.drawARGB(255,0,0,0)成功将其清除为黑色...但我希望有一个透明的背景,我无法将其清除为透明color,因为canvas.drawARGB(0,0,0,0)没有效果.
import java.util.ArrayList;
import java.util.Random;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
/*
* Accepts a sequence of Bitmap buffers and cycles through them.
*/
class AnimatedBufferView extends SurfaceView implements Runnable
{
Thread thread = null;
SurfaceHolder surfaceHolder;
volatile boolean running = false;
ArrayList<Bitmap> frames;
int curIndex = 0;
public AnimatedBufferView(ArrayList<Bitmap> _frames, Context context)
{
super(context);
surfaceHolder = getHolder();
frames = _frames;
}
public void onResume(){
running = true;
thread = new Thread(this);
thread.start();
}
public void onPause(){
boolean retry = true;
running = false;
while(retry){
try {
thread.join();
retry = false;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
public void run()
{
// TODO Auto-generated method stub
while(running)
{
if(surfaceHolder.getSurface().isValid())
{
Canvas canvas = surfaceHolder.lockCanvas();
//clear the buffer?
//canvas.drawARGB(255, 0, 0, 0);
//display the saved frame-buffer..
Matrix identity = new Matrix();
Bitmap frame = frames.get(curIndex);
canvas.drawBitmap(frame, identity, null);
surfaceHolder.unlockCanvasAndPost(canvas);
curIndex = (curIndex + 1) % frames.size();
try {
thread.sleep( 100 );
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
好的,发现问题是默认的Porter-Duff绘图模式,这使得无法绘制透明颜色.只需改变模式.即
Canvas canvas surfaceView.lockCanvas();
canvas.drawColor(0, Mode.CLEAR);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4944 次 |
| 最近记录: |