Skip to content

Commit 628f7f5

Browse files
committed
优化性能,更新1.0.10
1 parent e422716 commit 628f7f5

File tree

3 files changed

+58
-20
lines changed

3 files changed

+58
-20
lines changed

maven-upload.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
apply plugin: 'maven'
22

33
group "per.goweii.visualeffect"
4-
version "1.0.9"
4+
version "1.0.10"
55

66
uploadArchives {
77
repositories {

visualeffect-view/src/main/java/per/goweii/visualeffect/view/BackdropVisualEffectHelper.kt

+43-16
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,16 @@ import java.text.NumberFormat
1313
import kotlin.math.max
1414

1515
class BackdropVisualEffectHelper(private val view: View) {
16-
private val bitmapCanvas = Canvas()
16+
private var bitmapCanvas: Canvas? = null
1717
private var cacheBitmap: Bitmap? = null
1818
private var activityDecorView: View? = null
1919
private var isDifferentRoot = false
2020

21+
private val renderingListener = RenderingListener()
22+
private val locations = IntArray(2)
2123
private val srcRect = Rect()
2224
private val dstRect = Rect()
2325

24-
private val locations = IntArray(2)
25-
private val onPreDrawListener = ViewTreeObserver.OnPreDrawListener {
26-
renderOnce()
27-
true
28-
}
29-
3026
private val paint = Paint().apply {
3127
isAntiAlias = true
3228
typeface = Typeface.MONOSPACE
@@ -108,6 +104,7 @@ class BackdropVisualEffectHelper(private val view: View) {
108104
cacheBitmap?.let {
109105
onDrawEffectedBitmap(canvas, it)
110106
}
107+
canvas.drawColor(overlayColor)
111108
if (isShowDebugInfo) {
112109
onDrawDebugInfo(canvas)
113110
}
@@ -140,10 +137,8 @@ class BackdropVisualEffectHelper(private val view: View) {
140137
view.context.getActivity()?.let {
141138
activityDecorView = it.window?.decorView
142139
}
140+
registerRenderingListener()
143141
activityDecorView?.let {
144-
if (it.viewTreeObserver.isAlive) {
145-
it.viewTreeObserver.addOnPreDrawListener(onPreDrawListener)
146-
}
147142
isDifferentRoot = it.rootView !== view.rootView
148143
if (isDifferentRoot) {
149144
it.postInvalidate()
@@ -154,25 +149,51 @@ class BackdropVisualEffectHelper(private val view: View) {
154149
}
155150

156151
private fun onDetachedFromWindow() {
152+
unregisterRenderingListener()
153+
visualEffect?.recycle()
154+
}
155+
156+
private fun registerRenderingListener() {
157+
activityDecorView?.let {
158+
if (it.viewTreeObserver.isAlive) {
159+
it.viewTreeObserver.addOnPreDrawListener(renderingListener)
160+
}
161+
isDifferentRoot = it.rootView !== view.rootView
162+
if (isDifferentRoot) {
163+
it.postInvalidate()
164+
}
165+
}
166+
}
167+
168+
private fun unregisterRenderingListener() {
157169
activityDecorView?.let {
158170
if (it.viewTreeObserver.isAlive) {
159-
it.viewTreeObserver.removeOnPreDrawListener(onPreDrawListener)
171+
it.viewTreeObserver.removeOnPreDrawListener(renderingListener)
160172
}
161173
}
162-
visualEffect?.recycle()
163174
}
164175

165176
private fun prepare() {
166177
val simpledWidth = (view.width / simpleSize).toInt()
167178
val simpledHeight = (view.height / simpleSize).toInt()
168-
if (cacheBitmap == null || cacheBitmap!!.width != simpledWidth || cacheBitmap!!.height != simpledHeight) {
179+
if (simpledWidth <= 0 || simpledHeight <= 0) {
180+
bitmapCanvas = null
181+
cacheBitmap = null
182+
} else if (cacheBitmap == null || cacheBitmap!!.width != simpledWidth || cacheBitmap!!.height != simpledHeight) {
169183
cacheBitmap = try {
170184
Bitmap.createBitmap(simpledWidth, simpledHeight, Bitmap.Config.ARGB_8888)
171185
} catch (e: OutOfMemoryError) {
172186
Runtime.getRuntime().gc()
173187
null
174188
}
175-
bitmapCanvas.setBitmap(cacheBitmap)
189+
if (cacheBitmap != null) {
190+
if (bitmapCanvas == null) {
191+
bitmapCanvas = Canvas()
192+
}
193+
bitmapCanvas!!.setBitmap(cacheBitmap)
194+
} else {
195+
bitmapCanvas = null
196+
}
176197
}
177198
}
178199

@@ -182,8 +203,8 @@ class BackdropVisualEffectHelper(private val view: View) {
182203
val decor = activityDecorView ?: return
183204
if (!decor.isDirty) return
184205
prepare()
206+
val canvas = bitmapCanvas ?: return
185207
val bitmap = cacheBitmap ?: return
186-
val canvas = bitmapCanvas
187208
renderStartTime = System.nanoTime()
188209
bitmap.eraseColor(Color.TRANSPARENT)
189210
captureToBitmap(decor, canvas, bitmap)
@@ -224,7 +245,6 @@ class BackdropVisualEffectHelper(private val view: View) {
224245
dstRect.right = view.width
225246
dstRect.bottom = view.height
226247
canvas.drawBitmap(bitmap, srcRect, dstRect, paint)
227-
canvas.drawColor(overlayColor)
228248
}
229249

230250
private fun onDrawDebugInfo(canvas: Canvas) {
@@ -273,5 +293,12 @@ class BackdropVisualEffectHelper(private val view: View) {
273293
}
274294
}
275295

296+
private inner class RenderingListener : ViewTreeObserver.OnPreDrawListener {
297+
override fun onPreDraw(): Boolean {
298+
renderOnce()
299+
return true
300+
}
301+
}
302+
276303
private object StopException : RuntimeException()
277304
}

visualeffect-view/src/main/java/per/goweii/visualeffect/view/ChildrenVisualEffectHelper.kt

+14-3
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import java.text.NumberFormat
1111
import kotlin.math.max
1212

1313
class ChildrenVisualEffectHelper(private val view: View) {
14+
private var bitmapCanvas: Canvas? = null
1415
private var cacheBitmap: Bitmap? = null
15-
private val bitmapCanvas = Canvas()
1616
private val paint = Paint().apply {
1717
isAntiAlias = true
1818
typeface = Typeface.MONOSPACE
@@ -72,6 +72,7 @@ class ChildrenVisualEffectHelper(private val view: View) {
7272
return
7373
}
7474
prepare()
75+
val bitmapCanvas = bitmapCanvas ?: return
7576
val cacheBitmap = cacheBitmap ?: return
7677
renderStartTime = System.nanoTime()
7778
val restoreCount = bitmapCanvas.save()
@@ -114,14 +115,24 @@ class ChildrenVisualEffectHelper(private val view: View) {
114115
private fun prepare() {
115116
val simpledWidth = (view.width / simpleSize).toInt()
116117
val simpledHeight = (view.height / simpleSize).toInt()
117-
if (cacheBitmap == null || cacheBitmap!!.width != simpledWidth || cacheBitmap!!.height != simpledHeight) {
118+
if (simpledWidth <= 0 || simpledHeight <= 0) {
119+
bitmapCanvas = null
120+
cacheBitmap = null
121+
} else if (cacheBitmap == null || cacheBitmap!!.width != simpledWidth || cacheBitmap!!.height != simpledHeight) {
118122
cacheBitmap = try {
119123
Bitmap.createBitmap(simpledWidth, simpledHeight, Bitmap.Config.ARGB_8888)
120124
} catch (e: OutOfMemoryError) {
121125
Runtime.getRuntime().gc()
122126
null
123127
}
124-
bitmapCanvas.setBitmap(cacheBitmap)
128+
if (cacheBitmap != null) {
129+
if (bitmapCanvas == null) {
130+
bitmapCanvas = Canvas()
131+
}
132+
bitmapCanvas!!.setBitmap(cacheBitmap)
133+
} else {
134+
bitmapCanvas = null
135+
}
125136
}
126137
}
127138

0 commit comments

Comments
 (0)