SurfaceView显示黑屏 – Android

基本上我想使用SurfaceView进行动画制作.因此该类实现了Runnable.为了实验,我想绘制一个圆圈.但是,它只显示黑屏.

我已经好几天了.如果有人能提供帮助,真的很感激.

MainActivity类

public class MainActivity extends Activity {

private Bitmap Liquid;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature (Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    DrawStripFrame D1 = new DrawStripFrame(this);
    setContentView(D1);

DrawStripFrame类

public class DrawStripFrame extends SurfaceView implements Runnable{

private SurfaceHolder holder;
private boolean running = true;

public DrawStripFrame (Context context){
    super (context);
    holder = getHolder();
}

@Override
public void run(){

        while(running){         
            if(holder.getSurface().isValid()){
                Canvas c = holder.lockCanvas();
                c.drawARGB(0, 0, 0, 0);
                Paint redPaint = new Paint();
                redPaint.setColor(Color.RED);
                c.drawCircle(100, 100, 30, redPaint);
                holder.unlockCanvasAndPost(c);
            }

        }
    }
}
通过像这样更改while循环来解决问题:

while(running){         
            if(!holder.getSurface().isValid())
                continue;

            Canvas c = holder.lockCanvas();
            c.drawARGB(0, 0, 0, 0);
            Paint redPaint = new Paint();
            redPaint.setColor(Color.RED);
            c.drawCircle(100, 100, 30, redPaint);
            holder.unlockCanvasAndPost(c);
    }

当表面无效时,continue语句将控制权发送回循环的顶部.仅当曲面有效时,才会执行下面的块.

相关文章
相关标签/搜索