9

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);
            }

        }
    }
}
Kara
  • 6,115
  • 16
  • 50
  • 57
Wallyfull
  • 181
  • 1
  • 2
  • 16

5 Answers5

3

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.

1

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.

Wallyfull
  • 181
  • 1
  • 2
  • 16
  • 10
    I don't get it. This code is functionally equivalent to that in your original post. You basically changed it from "if condition is true then run this code" to "if condition is false then don't run this code". Different ways to say the same thing. – Adrian Lopez Sep 03 '14 at 21:14
  • 1
    those two statements aren't equivalent. A implies B is not equivalent to not(A) implies not(B). – Fela Maslen Sep 11 '16 at 21:43
  • ```A => B``` is not equivalent to ```¬A => ¬B```, sure. But in this case, the second statement can't be formalized as ```¬A => ¬B```. When A (Surface is valid) is true, B (Draw in canvas) is true; and when A is false, B is false. It is ```A => B``` in both cases. If you can say that the code to draw can't be executed in another part of the code, that is another story. – JCarlosR Feb 26 '17 at 20:24
0

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

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

    setAlpha(0); // this is the secret
}
Derzu
  • 7,011
  • 3
  • 57
  • 60
0

Make sure you have given Camera Permission otherwise SurfaceView shows black screen.

AskQ
  • 4,215
  • 7
  • 34
  • 61
0

SurfaceView basically doesn't supporting animation, scrolling etc. So, you should use TextureView.

Sergei Maleev
  • 307
  • 3
  • 4
  • 2
    Welcome to stack overflow. Please consider backing your answers that don't include code with a reference to an official resource website and a quote with the relevant information. That way nobody can claim that your answer should have been a comment instead. – Nikos Hidalgo Jan 31 '20 at 17:08