Skip to content

Commit

Permalink
游戏适配生命周期
Browse files Browse the repository at this point in the history
  • Loading branch information
xzp committed Jan 24, 2022
1 parent 42550de commit 66ef6c9
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import com.lifefighter.widget.wallpaper.CanvasPainter
* @created on 2022/1/19.
*/
interface CanvasGame : CanvasPainter {
fun onStart(context: Context)
fun onStart(context: Context) {}

fun onEnd(context: Context)
fun onEnd(context: Context) {}

fun getName(): String
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,14 @@ class GameWallpaperService : CanvasWallpaperService() {
game?.onDraw(canvas)
}

override fun onResume() {
game?.onResume()
}

override fun onPause() {
game?.onPause()
}

override fun onOffset(xOffset: Float, yOffset: Float) {
game?.onOffset(xOffset, yOffset)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ class WallpaperActivity : BaseActivity<WallpaperBinding>() {
}))
}

override fun onResume() {
super.onResume()
adapter.data.forEach {
(it as? CanvasModel)?.game?.onResume()
}
}

override fun onPause() {
super.onPause()
adapter.data.forEach {
(it as? CanvasModel)?.game?.onPause()
}
}

private fun startWallpaper() {
WallpaperManager.getInstance(this).clear()
val intent = Intent(WallpaperManager.ACTION_CHANGE_LIVE_WALLPAPER).also {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ class Ball(private val game: BricksGame) : DrawAble, ResetAble {
}
}

fun resume() {
}

fun pause() {
lastMoveTime = null
}

fun move() {
if (isStart.not()) {
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class BricksGame : CanvasGame, DrawAble, ResetAble {
*/
private var messageHandler: Handler? = null


private var paused = false
override fun reset() {
messageHandler?.removeCallbacksAndMessages(null)
messageHandler?.post {
Expand Down Expand Up @@ -105,6 +105,9 @@ class BricksGame : CanvasGame, DrawAble, ResetAble {
*/
override fun onDraw(canvas: Canvas) {
synchronized(this@BricksGame) {
if (paused) {
return@synchronized
}
canvas.drawColor(Color.BLACK)
canvas.save()
if (canvas.width < width || canvas.height < height) {
Expand Down Expand Up @@ -165,6 +168,24 @@ class BricksGame : CanvasGame, DrawAble, ResetAble {
reset()
}

override fun onResume() {
messageHandler?.post {
synchronized(this@BricksGame) {
ball.resume()
paused = false
}
}
}

override fun onPause() {
messageHandler?.post {
synchronized(this@BricksGame) {
paused = true
ball.pause()
}
}
}

override fun onEnd(context: Context) {
messageHandler?.removeCallbacksAndMessages(null)
messageHandler = null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.lifefighter.overlord.action.wallpaper.clock

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
Expand All @@ -14,18 +13,16 @@ import kotlin.math.min
*/
class ClockGame : CanvasGame {
private val clockPaint = Paint(Paint.ANTI_ALIAS_FLAG)
override fun onStart(context: Context) {
}

override fun onEnd(context: Context) {
}

private var start = false
override fun getName(): String {
return "时钟壁纸"
}


override fun onDraw(canvas: Canvas) {
if (start.not()) {
return
}
val minSize = min(canvas.width, canvas.height)
if (minSize == 0) {
return
Expand Down Expand Up @@ -108,4 +105,12 @@ class ClockGame : CanvasGame {

override fun onOffset(xOffset: Float, yOffset: Float) {
}

override fun onResume() {
start = true
}

override fun onPause() {
start = false
}
}
2 changes: 1 addition & 1 deletion treasure-box

0 comments on commit 66ef6c9

Please sign in to comment.