0

I'm trying to make a simple game for Android and have a problem with animation rendering.
First of all, I started from drawing in View by calling of .invalidate() method.
But it gives only 30 FPS.

Than, I founded SurfaceView. I'm drawing in background Thread and I have 60 FPS.
But the problem starts when I hide and resume the application.
Here is my code:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.company.temp.MySurfaceView
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

package com.company.temp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

MySurfaceView.kt

package com.company.temp

import android.content.Context
import android.graphics.Color
import android.util.AttributeSet
import android.view.SurfaceHolder
import android.view.SurfaceView

class MySurfaceView : SurfaceView, SurfaceHolder.Callback {

    constructor(context: Context) : super(context)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) :
            super(context, attrs, defStyleAttr)

    init {
        holder.addCallback(this)
    }

    private val runnable = Runnable {
        while (true) {
            val canvas = holder?.lockCanvas()
            canvas?.drawColor(Color.BLACK)
            holder?.unlockCanvasAndPost(canvas)
        }
    }
    private val thread = Thread(runnable)


    override fun surfaceCreated(holder: SurfaceHolder) {
        thread.start()
    }

    override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
    }

    override fun surfaceDestroyed(holder: SurfaceHolder) {
    }
}

How to continue rendering after application resume?

0 Answers0