SurfaceView显示黑屏 - Android电子 [英] SurfaceView shows black screen - Android
问题描述
基本上我想使用SurfaceView动画。因此,该类实现Runnable。做实验,我想画一个圆。然而,只显示黑屏。
我一直在尝试了好几天。真的AP preciate,如果有人可以提供帮助。
MainActivity类别
公共类MainActivity延伸活动{私人位图液体;@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow()。setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN); DrawStripFrame D1 =新DrawStripFrame(本);
的setContentView(D1);
DrawStripFrame类
公共类DrawStripFrame扩展SurfaceView实现Runnable {私人SurfaceHolder持有人;
私人布尔运行= TRUE;公共DrawStripFrame(上下文的背景下){
超级(上下文);
支架= getHolder();
}@覆盖
公共无效的run(){ 而(运行){
如果(holder.getSurface()。的isValid()){
帆布C = holder.lockCanvas();
c.drawARGB(0,0,0,0);
涂料redPaint =新的油漆();
redPaint.setColor(Color.RED);
c.drawCircle(100,100,30,redPaint);
holder.unlockCanvasAndPost(C);
} }
}
}
问题是通过改变while循环是这样解决的:
而(运行){
如果(!holder.getSurface()。isValid()的)
继续; 帆布C = holder.lockCanvas();
c.drawARGB(0,0,0,0);
涂料redPaint =新的油漆();
redPaint.setColor(Color.RED);
c.drawCircle(100,100,30,redPaint);
holder.unlockCanvasAndPost(C);
}
continue语句发送控制回到循环的顶部时,表面是无效的。只有当表面是有效的,该块下面将被执行。
Basically I want to use SurfaceView for animation. Therefore the class implements Runnable. To experiment, I want to draw a circle. However, it shows only a black screen.
I have been trying for days. Really appreciate if someone can help.
MainActivity class
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 class
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);
}
}
}
}
The problem is solved by changing the while loop like this:
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);
}
The continue statement sends control back to the top of the loop when surface is invalid. Only when surface is valid, the block below will be executed.
这篇关于SurfaceView显示黑屏 - Android电子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!