SurfaceView shows black screen - Android

会有一股神秘感。 提交于 2019-11-29 10:08:06

I had this same problem. I tested my APP on different emulators.

This was my MainActivity class

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private SurfaceViewTest test;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        test = new SurfaceViewTest(this);
        setContentView(test);
    }

    @Override
    protected void onResume() {
        super.onResume();
        test.resume();
    }
}

SurfaceViewTest.java

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.view.SurfaceView;


public class SurfaceViewTest extends SurfaceView implements Runnable {

    private Thread thread;
    volatile boolean running;


    public SurfaceViewTest(Context context) {
        super(context);
    }
    @Override
    public void run() {
        while(running) {
            draw();
        }
    }

    private void draw(){

        if(getHolder().getSurface().isValid()) {
            Canvas canvas = getHolder().lockCanvas();

            canvas.drawColor(Color.argb(255, 255, 0, 0));

            getHolder().unlockCanvasAndPost(canvas);
        }
    }

    public void resume() {
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public void pause() {
        running = false;

        while (thread.isAlive()) {
            try {
                thread.join(100);

                if(thread.isAlive()) {
                    thread.interrupt();
                    thread.join();
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:windowFullscreen">true</item>
    </style>

    <style name="Theme.AppCompat.Light.NoActionBar.FullScreen" parent="@style/Theme.AppCompat.Light">
        <item name="windowNoTitle">true</item>
        <item name="windowActionBar">false</item>
        <item name="android:windowFullscreen">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:background">#000000</item><!-- Remove it to fix the problem -->
    </style>
</resources>

I solved it removing the item background from styles.xml

<item name="android:background">#00000</item>

It looks a problem with the alpha channel.

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.

At your DrawStripFrame constructor set alpha to zero. Like this:

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

    setAlpha(0); // this is the secret
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!