Skip to content

Commit 863af6b

Browse files
author
hyb1996
committed
修复 画布canvas默认白色改成默认透明
修复 画布canvas会覆盖其他View的问题
1 parent a0e9dbc commit 863af6b

File tree

5 files changed

+192
-189
lines changed

5 files changed

+192
-189
lines changed
0 Bytes
Binary file not shown.

app/release/output.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":203},"path":"inrt-release.apk","properties":{"packageId":"com.stardust.auojs.inrt","split":"","minSdkVersion":"17"}}]
1+
[{"outputType":{"type":"APK"},"apkInfo":{"type":"MAIN","splits":[],"versionCode":253,"versionName":"4.1.0 Alpha3","enabled":true,"outputFile":"inrt-release.apk","fullName":"release","baseName":"release"},"path":"inrt-release.apk","properties":{}}]

autojs/src/main/java/com/stardust/autojs/core/graphics/ScriptCanvasView.java

Lines changed: 0 additions & 188 deletions
This file was deleted.
Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
package com.stardust.autojs.core.graphics
2+
3+
import android.annotation.SuppressLint
4+
import android.content.Context
5+
import android.graphics.Canvas
6+
import android.graphics.SurfaceTexture
7+
import android.os.SystemClock
8+
import android.util.Log
9+
import android.view.TextureView
10+
import android.view.View
11+
import com.stardust.autojs.core.eventloop.EventEmitter
12+
import com.stardust.autojs.runtime.ScriptRuntime
13+
import com.stardust.autojs.runtime.exception.ScriptInterruptedException
14+
import com.stardust.ext.ifNull
15+
import java.util.concurrent.ExecutorService
16+
import java.util.concurrent.Executors
17+
18+
/**
19+
* Created by Stardust on 2018/3/16.
20+
*/
21+
22+
@SuppressLint("ViewConstructor")
23+
class ScriptCanvasView(context: Context, private val mScriptRuntime: ScriptRuntime) : TextureView(context), TextureView.SurfaceTextureListener {
24+
@Volatile
25+
private var mDrawing = true
26+
private val mEventEmitter: EventEmitter = EventEmitter(mScriptRuntime.bridges)
27+
private var mDrawingThreadPool: ExecutorService? = null
28+
@Volatile
29+
private var mTimePerDraw = (1000 / 30).toLong()
30+
31+
val maxListeners: Int
32+
get() = mEventEmitter.maxListeners
33+
34+
init {
35+
surfaceTextureListener = this
36+
}
37+
38+
fun setMaxFps(maxFps: Int) {
39+
mTimePerDraw = if (maxFps <= 0) {
40+
0
41+
} else {
42+
(100 / maxFps).toLong()
43+
}
44+
}
45+
46+
@Synchronized
47+
private fun performDraw() {
48+
::mDrawingThreadPool.ifNull {
49+
Executors.newCachedThreadPool()
50+
}.run {
51+
execute {
52+
var canvas: Canvas? = null
53+
var time = SystemClock.uptimeMillis()
54+
val scriptCanvas = ScriptCanvas()
55+
try {
56+
while (mDrawing) {
57+
canvas = lockCanvas()
58+
scriptCanvas.setCanvas(canvas)
59+
// scriptCanvas.drawColor(Color.WHITE);
60+
emit("draw", scriptCanvas, this@ScriptCanvasView)
61+
unlockCanvasAndPost(canvas)
62+
canvas = null
63+
val dt = mTimePerDraw - (SystemClock.uptimeMillis() - time)
64+
if (dt > 0) {
65+
sleep(dt)
66+
}
67+
time = SystemClock.uptimeMillis()
68+
}
69+
} catch (e: Exception) {
70+
mScriptRuntime.exit(e)
71+
mDrawing = false
72+
} finally {
73+
if (canvas != null) {
74+
unlockCanvasAndPost(canvas)
75+
}
76+
}
77+
}
78+
}
79+
}
80+
81+
private fun sleep(dt: Long) {
82+
try {
83+
Thread.sleep(dt)
84+
} catch (e: InterruptedException) {
85+
throw ScriptInterruptedException(e)
86+
}
87+
88+
}
89+
90+
override fun onWindowVisibilityChanged(visibility: Int) {
91+
Log.d(LOG_TAG, "onWindowVisibilityChanged: " + this + ": visibility=" + visibility + ", mDrawingThreadPool=" + mDrawingThreadPool)
92+
val oldDrawing = mDrawing
93+
mDrawing = visibility == View.VISIBLE
94+
if (!oldDrawing && mDrawing) {
95+
performDraw()
96+
}
97+
super.onWindowVisibilityChanged(visibility)
98+
}
99+
100+
fun once(eventName: String, listener: Any): EventEmitter {
101+
return mEventEmitter.once(eventName, listener)
102+
}
103+
104+
fun on(eventName: String, listener: Any): EventEmitter {
105+
return mEventEmitter.on(eventName, listener)
106+
}
107+
108+
fun addListener(eventName: String, listener: Any): EventEmitter {
109+
return mEventEmitter.addListener(eventName, listener)
110+
}
111+
112+
fun emit(eventName: String, vararg args: Any): Boolean {
113+
return mEventEmitter.emit(eventName, *args)
114+
}
115+
116+
fun eventNames(): Array<String> {
117+
return mEventEmitter.eventNames()
118+
}
119+
120+
fun listenerCount(eventName: String): Int {
121+
return mEventEmitter.listenerCount(eventName)
122+
}
123+
124+
fun listeners(eventName: String): Array<Any> {
125+
return mEventEmitter.listeners(eventName)
126+
}
127+
128+
fun prependListener(eventName: String, listener: Any): EventEmitter {
129+
return mEventEmitter.prependListener(eventName, listener)
130+
}
131+
132+
fun prependOnceListener(eventName: String, listener: Any): EventEmitter {
133+
return mEventEmitter.prependOnceListener(eventName, listener)
134+
}
135+
136+
fun removeAllListeners(): EventEmitter {
137+
return mEventEmitter.removeAllListeners()
138+
}
139+
140+
fun removeAllListeners(eventName: String): EventEmitter {
141+
return mEventEmitter.removeAllListeners(eventName)
142+
}
143+
144+
fun removeListener(eventName: String, listener: Any): EventEmitter {
145+
return mEventEmitter.removeListener(eventName, listener)
146+
}
147+
148+
fun setMaxListeners(n: Int): EventEmitter {
149+
return mEventEmitter.setMaxListeners(n)
150+
}
151+
152+
override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
153+
performDraw()
154+
Log.d(LOG_TAG, "onSurfaceTextureAvailable: ${this}, width = $width, height = $height")
155+
}
156+
157+
override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}
158+
159+
override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
160+
mDrawing = false
161+
mDrawingThreadPool?.shutdown()
162+
Log.d(LOG_TAG, "onSurfaceTextureDestroyed: ${this}")
163+
return true
164+
}
165+
166+
override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
167+
168+
}
169+
170+
companion object {
171+
172+
private const val LOG_TAG = "ScriptCanvasView"
173+
174+
fun defaultMaxListeners(): Int {
175+
return EventEmitter.defaultMaxListeners()
176+
}
177+
}
178+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.stardust.ext
2+
3+
import kotlin.reflect.KMutableProperty0
4+
5+
fun <R, T : KMutableProperty0<R?>> T.ifNull(provider: () -> R): R {
6+
val value = this.get()
7+
if (value != null) {
8+
return value
9+
}
10+
val newValue = provider()
11+
set(newValue)
12+
return newValue
13+
}

0 commit comments

Comments
 (0)