Skip to content

Commit 26bbb13

Browse files
committed
add fun setDynamicImage(url: String, forKey: String) method.
1 parent 96b9fa8 commit 26bbb13

File tree

5 files changed

+32
-6
lines changed

5 files changed

+32
-6
lines changed
-34.5 KB
Binary file not shown.

app/src/main/assets/angel.svga

-89.1 KB
Binary file not shown.

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
package com.opensource.svgaplayer
22

33
import android.graphics.Bitmap
4+
import android.graphics.BitmapFactory
45
import android.text.TextPaint
6+
import java.io.ByteArrayInputStream
7+
import java.io.ByteArrayOutputStream
8+
import java.net.HttpURLConnection
9+
import java.net.URL
10+
import java.util.logging.Handler
511

612
/**
713
* Created by cuiminghui on 2017/3/30.
@@ -18,6 +24,25 @@ class SVGADynamicEntity {
1824
this.dynamicImage.put(forKey, bitmap)
1925
}
2026

27+
fun setDynamicImage(url: String, forKey: String) {
28+
val handler = android.os.Handler()
29+
Thread({
30+
try {
31+
(URL(url).openConnection() as? HttpURLConnection)?.let {
32+
it.connectTimeout = 20 * 1000
33+
it.requestMethod = "GET"
34+
it.connect()
35+
BitmapFactory.decodeStream(it.inputStream)?.let {
36+
handler.post { setDynamicImage(it, forKey) }
37+
}
38+
it.inputStream.close()
39+
}
40+
} catch (e: Exception) {
41+
e.printStackTrace()
42+
}
43+
}).start()
44+
}
45+
2146
fun setDynamicText(text: String, textPaint: TextPaint, forKey: String) {
2247
this.dynamicText.put(forKey, text)
2348
this.dynamicTextPaint.put(forKey, textPaint)

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import android.content.Context
66
import android.graphics.Bitmap
77
import android.graphics.Canvas
88
import android.graphics.ColorFilter
9+
import android.graphics.PixelFormat
910
import android.graphics.drawable.Drawable
1011
import android.os.Build
1112
import android.text.TextPaint
@@ -46,7 +47,7 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
4647

4748
var scaleType: ImageView.ScaleType = ImageView.ScaleType.MATRIX
4849

49-
internal val drawer = SVGACanvasDrawer(videoItem, dynamicItem)
50+
private val drawer = SVGACanvasDrawer(videoItem, dynamicItem)
5051

5152
override fun draw(canvas: Canvas?) {
5253
if (cleared) {
@@ -61,7 +62,7 @@ class SVGADrawable(val videoItem: SVGAVideoEntity, val dynamicItem: SVGADynamicE
6162
override fun setAlpha(alpha: Int) { }
6263

6364
override fun getOpacity(): Int {
64-
return 255
65+
return PixelFormat.TRANSPARENT
6566
}
6667

6768
override fun setColorFilter(colorFilter: ColorFilter?) {
@@ -121,7 +122,7 @@ open class SVGAImageView : ImageView {
121122
animator?.removeAllUpdateListeners()
122123
}
123124

124-
fun loadAttrs(attrs: AttributeSet) {
125+
private fun loadAttrs(attrs: AttributeSet) {
125126
val typedArray = context.theme.obtainStyledAttributes(attrs, R.styleable.SVGAImageView, 0, 0)
126127
loops = typedArray.getInt(R.styleable.SVGAImageView_loopCount, 0)
127128
clearsAfterStop = typedArray.getBoolean(R.styleable.SVGAImageView_clearsAfterStop, true)
@@ -161,10 +162,10 @@ open class SVGAImageView : ImageView {
161162
}).start()
162163
}
163164
typedArray.getString(R.styleable.SVGAImageView_fillMode)?.let {
164-
if (it.equals("0")) {
165+
if (it == "0") {
165166
fillMode = FillMode.Backward
166167
}
167-
else if (it.equals("1")) {
168+
else if (it == "1") {
168169
fillMode = FillMode.Forward
169170
}
170171
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ class SVGAParser(private val context: Context) {
109109
}).start()
110110
}
111111

112-
private fun parse(inputStream: InputStream, cacheKey: String): SVGAVideoEntity? {
112+
fun parse(inputStream: InputStream, cacheKey: String): SVGAVideoEntity? {
113113
val bytes = readAsBytes(inputStream)
114114
if (bytes.size > 4 && bytes[0].toInt() == 80 && bytes[1].toInt() == 75 && bytes[2].toInt() == 3 && bytes[3].toInt() == 4) {
115115
synchronized(sharedLock, {

0 commit comments

Comments
 (0)