Resuming SurfaceView displays black screen

Everything is fine when I started my application. But when I move on to the next action and return to SurfaceView, it looks like black. When I debug, I see the lines that need to be drawn on the canvas, but nothing is displayed.
Here is my code:

public class DrawView extends SurfaceView implements Runnable
{

    Thread thread = null;
    SurfaceHolder holder;
    Bitmap background;
    Canvas canvas;
    Matrix matrixUP = new Matrix();
    Matrix matrixDOWN = new Matrix();
    boolean ok = false;

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

    public void run() 
    {
        while(ok)
        {
            if(!holder.getSurface().isValid())
            {
                continue;
            }
            watch_hand = Bitmap.createScaledBitmap(watch_hand, width/4, height/6, false);
            watch_hand_down = Bitmap.createScaledBitmap(watch_hand_down, width/4, height/6, false);
            background = BitmapFactory.decodeResource(getResources(), R.drawable.needles);
            background = Bitmap.createScaledBitmap(background, width, height/2+20, false);
            canvas = holder.lockCanvas();
            matrixUP.setTranslate(CENTER_X - watch_hand.getWidth()/2, height/4 - watch_hand.getHeight() - 40);
            matrixUP.preRotate((float) degreUP , watch_hand.getWidth()/2 , watch_hand.getHeight()-10);

            matrixDOWN.setTranslate(CENTER_X - watch_hand_down.getWidth()/2, height/2 - watch_hand_down.getHeight() - 20);
            matrixDOWN.preRotate((float) degreDOWN , watch_hand_down.getWidth()/2 , 10);

            canvas.drawARGB(255, 255, 255, 255);
            canvas.drawBitmap(background, 0, 0, null);
            canvas.drawBitmap(watch_hand, matrixUP, null);
            canvas.drawBitmap(watch_hand_down, matrixDOWN, null);
            holder.unlockCanvasAndPost(canvas);
        }           
    }

    public void pause()
    {
        ok = false;
        while(true)
        {
            try
            {
                thread.join();
            }catch (InterruptedException e)
            {
                e.printStackTrace();
            }
            break;
        }
        thread = null;
    }

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

As you can see, I use the resume and pause function. They are called from onCreate. Why is this happening and how can I fix it?

Thank!

+3
source share
2 answers

Call the resume () and pause () functions to view the surface (in your case DrawView) from the onResume () and onPause () methods of the activity in which the surface view is present. U mentioned that you called from onCreate ().

This should solve the problem.

0

, .

, , , , .

, onSaveInstanceState , degreUP degreDOWN (, ). onRestoreInstanceState. . Android

+1

All Articles