Skip to content
This repository was archived by the owner on Feb 6, 2023. It is now read-only.

Commit 629d06c

Browse files
author
caiyuwei
committed
fix: 修复 svga alpha 和 color 重复设置导致画面显示异常
1 parent 908cd21 commit 629d06c

File tree

2 files changed

+57
-30
lines changed

2 files changed

+57
-30
lines changed

library/src/main/java/com/opensource/svgaplayer/drawer/SVGACanvasDrawer.kt

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@ package com.opensource.svgaplayer.drawer
22

33
import android.graphics.*
44
import android.os.Build
5-
import android.text.*
5+
import android.text.StaticLayout
6+
import android.text.TextUtils
67
import android.widget.ImageView
78
import com.opensource.svgaplayer.SVGADynamicEntity
89
import com.opensource.svgaplayer.SVGASoundManager
910
import com.opensource.svgaplayer.SVGAVideoEntity
1011
import com.opensource.svgaplayer.entities.SVGAVideoShapeEntity
11-
import java.lang.Exception
1212

1313
/**
1414
* Created by cuiminghui on 2017/3/29.
@@ -172,9 +172,9 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
172172
}
173173
if (audio.endFrame <= frameIndex) {
174174
audio.playID?.let {
175-
if (SVGASoundManager.isInit()){
175+
if (SVGASoundManager.isInit()) {
176176
SVGASoundManager.stop(it)
177-
}else{
177+
} else {
178178
this.videoItem.soundPool?.stop(it)
179179
}
180180
}
@@ -356,7 +356,10 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
356356
if (it != 0x00000000) {
357357
paint.style = Paint.Style.FILL
358358
paint.color = it
359-
paint.alpha = Math.min(255, Math.max(0, (sprite.frameEntity.alpha * 255).toInt()))
359+
val alpha = Math.min(255, Math.max(0, (sprite.frameEntity.alpha * 255).toInt()))
360+
if (alpha != 255) {
361+
paint.alpha = alpha
362+
}
360363
if (sprite.frameEntity.maskPath !== null) canvas.save()
361364
sprite.frameEntity.maskPath?.let { maskPath ->
362365
val path2 = this.sharedValues.sharedPath2()
@@ -370,10 +373,14 @@ internal class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVG
370373
}
371374
shape.styles?.strokeWidth?.let {
372375
if (it > 0) {
376+
paint.alpha = (sprite.frameEntity.alpha * 255).toInt()
373377
paint.style = Paint.Style.STROKE
374378
shape.styles?.stroke?.let {
375379
paint.color = it
376-
paint.alpha = Math.min(255, Math.max(0, (sprite.frameEntity.alpha * 255).toInt()))
380+
val alpha = Math.min(255, Math.max(0, (sprite.frameEntity.alpha * 255).toInt()))
381+
if (alpha != 255) {
382+
paint.alpha = alpha
383+
}
377384
}
378385
val scale = matrixScale(frameMatrix)
379386
shape.styles?.strokeWidth?.let {

library/src/main/java/com/opensource/svgaplayer/entities/SVGAVideoShapeEntity.kt

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,37 @@ internal class SVGAVideoShapeEntity {
135135
this.args = args
136136
}
137137

138+
// 检查色域范围是否是 [0f, 1f],或者是 [0f, 255f]
139+
private fun checkValueRange(obj: JSONArray): Float {
140+
return if (
141+
obj.optDouble(0) <= 1 &&
142+
obj.optDouble(1) <= 1 &&
143+
obj.optDouble(2) <= 1
144+
) {
145+
255f
146+
} else {
147+
1f
148+
}
149+
}
150+
151+
// 检查 alpha 的范围是否是 [0f, 1f],或者是 [0f, 255f]
152+
private fun checkAlphaValueRange(obj: JSONArray): Float {
153+
return if (obj.optDouble(3) <= 1) {
154+
255f
155+
} else {
156+
1f
157+
}
158+
}
159+
138160
private fun parseStyles(obj: JSONObject) {
139161
obj.optJSONObject("styles")?.let {
140162
val styles = Styles()
141163
it.optJSONArray("fill")?.let {
142164
if (it.length() == 4) {
143165
val mulValue = checkValueRange(it)
166+
val alphaRangeValue = checkAlphaValueRange(it)
144167
styles.fill = Color.argb(
145-
(it.optDouble(3) * mulValue).toInt(),
168+
(it.optDouble(3) * alphaRangeValue).toInt(),
146169
(it.optDouble(0) * mulValue).toInt(),
147170
(it.optDouble(1) * mulValue).toInt(),
148171
(it.optDouble(2) * mulValue).toInt()
@@ -152,8 +175,9 @@ internal class SVGAVideoShapeEntity {
152175
it.optJSONArray("stroke")?.let {
153176
if (it.length() == 4) {
154177
val mulValue = checkValueRange(it)
178+
val alphaRangeValue = checkAlphaValueRange(it)
155179
styles.stroke = Color.argb(
156-
(it.optDouble(3) * mulValue).toInt(),
180+
(it.optDouble(3) * alphaRangeValue).toInt(),
157181
(it.optDouble(0) * mulValue).toInt(),
158182
(it.optDouble(1) * mulValue).toInt(),
159183
(it.optDouble(2) * mulValue).toInt()
@@ -174,36 +198,46 @@ internal class SVGAVideoShapeEntity {
174198
}
175199
}
176200

177-
// 检查色域范围是否是 0-1
178-
private fun checkValueRange(obj: JSONArray): Float {
201+
// 检查色域范围是否是 [0f, 1f],或者是 [0f, 255f]
202+
private fun checkValueRange(color: ShapeEntity.ShapeStyle.RGBAColor): Float {
179203
return if (
180-
obj.optDouble(3) <= 1 &&
181-
obj.optDouble(0) <= 1 &&
182-
obj.optDouble(1) <= 1 &&
183-
obj.optDouble(2) <= 1
204+
(color.r ?: 0f) <= 1 &&
205+
(color.g ?: 0f) <= 1 &&
206+
(color.b ?: 0f) <= 1
184207
) {
185208
255f
186209
} else {
187210
1f
188211
}
189212
}
190213

214+
// 检查 alpha 范围是否是 [0f, 1f],有可能是 [0f, 255f]
215+
private fun checkAlphaValueRange(color: ShapeEntity.ShapeStyle.RGBAColor): Float {
216+
return if (color.a <= 1f) {
217+
255f
218+
} else {
219+
1f
220+
}
221+
}
222+
191223
private fun parseStyles(obj: ShapeEntity) {
192224
obj.styles?.let {
193225
val styles = Styles()
194226
it.fill?.let {
195227
val mulValue = checkValueRange(it)
228+
val alphaRangeValue = checkAlphaValueRange(it)
196229
styles.fill = Color.argb(
197-
((it.a ?: 0f) * mulValue).toInt(),
230+
((it.a ?: 0f) * alphaRangeValue).toInt(),
198231
((it.r ?: 0f) * mulValue).toInt(),
199232
((it.g ?: 0f) * mulValue).toInt(),
200233
((it.b ?: 0f) * mulValue).toInt()
201234
)
202235
}
203236
it.stroke?.let {
204237
val mulValue = checkValueRange(it)
238+
val alphaRangeValue = checkAlphaValueRange(it)
205239
styles.stroke = Color.argb(
206-
((it.a ?: 0f) * mulValue).toInt(),
240+
((it.a ?: 0f) * alphaRangeValue).toInt(),
207241
((it.r ?: 0f) * mulValue).toInt(),
208242
((it.g ?: 0f) * mulValue).toInt(),
209243
((it.b ?: 0f) * mulValue).toInt()
@@ -234,20 +268,6 @@ internal class SVGAVideoShapeEntity {
234268
}
235269
}
236270

237-
// 检查色域范围是否是 0-1
238-
private fun checkValueRange(color: ShapeEntity.ShapeStyle.RGBAColor): Float {
239-
return if (
240-
(color.a ?: 0f) <= 1 &&
241-
(color.r ?: 0f) <= 1 &&
242-
(color.g ?: 0f) <= 1 &&
243-
(color.b ?: 0f) <= 1
244-
) {
245-
255f
246-
} else {
247-
1f
248-
}
249-
}
250-
251271
private fun parseTransform(obj: JSONObject) {
252272
obj.optJSONObject("transform")?.let {
253273
val transform = Matrix()

0 commit comments

Comments
 (0)