Skip to content

Commit cb33d6f

Browse files
committed
use clipPath instead of BitmapShader.
set SoftwareLayer while API < 18.
1 parent bedc91d commit cb33d6f

File tree

3 files changed

+24
-13
lines changed

3 files changed

+24
-13
lines changed

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

+3-6
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,11 @@ class SVGACanvasDrawer(videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
120120
if (sprite.frameEntity.maskPath != null) {
121121
val maskPath = sprite.frameEntity.maskPath ?: return@let
122122
canvas.save()
123-
canvas.concat(sharedContentTransform)
124-
canvas.clipRect(0, 0, it.width, it.height)
125-
val bitmapShader = BitmapShader(it, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT)
126-
sharedPaint.shader = bitmapShader
127-
sharedPaint.isAntiAlias = true
128123
sharedPath.reset()
129124
maskPath.buildPath(sharedPath)
130-
canvas.drawPath(sharedPath, sharedPaint)
125+
sharedPath.transform(sharedContentTransform)
126+
canvas.clipPath(sharedPath)
127+
canvas.drawBitmap(it, sharedContentTransform, sharedPaint)
131128
canvas.restore()
132129
}
133130
else {

library/src/main/java/com/opensource/svgaplayer/SVGAImageView.kt

+14-1
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ import android.graphics.Bitmap
77
import android.graphics.Canvas
88
import android.graphics.ColorFilter
99
import android.graphics.drawable.Drawable
10+
import android.os.Build
1011
import android.text.TextPaint
1112
import android.util.AttributeSet
1213
import android.view.Choreographer
14+
import android.view.View
1315
import android.view.ViewPropertyAnimator
1416
import android.view.animation.LinearInterpolator
1517
import android.widget.ImageView
@@ -83,20 +85,31 @@ open class SVGAImageView : ImageView {
8385

8486
private var animator: ValueAnimator? = null
8587

86-
constructor(context: Context?) : super(context) {}
88+
constructor(context: Context?) : super(context) {
89+
setSoftwareLayerType()
90+
}
8791

8892
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {
93+
setSoftwareLayerType()
8994
attrs?.let { loadAttrs(it) }
9095
}
9196

9297
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
98+
setSoftwareLayerType()
9399
attrs?.let { loadAttrs(it) }
94100
}
95101

96102
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int, defStyleRes: Int) : super(context, attrs, defStyleAttr, defStyleRes) {
103+
setSoftwareLayerType()
97104
attrs?.let { loadAttrs(it) }
98105
}
99106

107+
private fun setSoftwareLayerType() {
108+
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
109+
this.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
110+
}
111+
}
112+
100113
override fun onDetachedFromWindow() {
101114
super.onDetachedFromWindow()
102115
animator?.cancel()

library/src/main/java/com/opensource/svgaplayer/SVGAPath.kt

+7-6
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package com.opensource.svgaplayer
33
import android.graphics.Path
44
import java.util.*
55

6-
class SVGAPath(val strValue: String) {
6+
val VALID_METHODS: List<String> = listOf("M", "L", "H", "V", "C", "S", "Q", "R", "A", "Z", "m", "l", "h", "v", "c", "s", "q", "r", "a", "z")
77

8-
var cachedPath: Path? = null
9-
val VALID_METHODS: List<String> = listOf("M", "L", "H", "V", "C", "S", "Q", "R", "A", "Z", "m", "l", "h", "v", "c", "s", "q", "r", "a", "z")
8+
class SVGAPath(private val strValue: String) {
9+
10+
private var cachedPath: Path? = null
1011

1112
fun buildPath(toPath: Path) {
1213
cachedPath?.let {
@@ -32,15 +33,15 @@ class SVGAPath(val strValue: String) {
3233
currentMethod = firstLetter
3334
argLast = item.substring(1)
3435
} else {
35-
if (null != argLast && argLast.trim { it <= ' ' }.length > 0) {
36+
argLast = if (null != argLast && argLast.trim { it <= ' ' }.isNotEmpty()) {
3637
args.add(SVGAPoint(try {
3738
argLast.toFloat()
3839
} catch (e: Exception) {0.0f}, try {
3940
item.toFloat()
4041
} catch (e: Exception) {0.0f}, 0.0f))
41-
argLast = null
42+
null
4243
} else {
43-
argLast = item
44+
item
4445
}
4546
}
4647
}

0 commit comments

Comments
 (0)