decodeApngAsyncInto can now take a scope param (default to GlobalScope)

This commit is contained in:
Oupson 2021-03-04 12:32:28 +01:00
parent 63198f6ce7
commit 35f70ecd88
1 changed files with 23 additions and 20 deletions

View File

@ -11,10 +11,7 @@ import android.os.Build
import android.util.Log import android.util.Log
import android.widget.ImageView import android.widget.ImageView
import androidx.annotation.RawRes import androidx.annotation.RawRes
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.*
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import oupson.apng.BuildConfig import oupson.apng.BuildConfig
import oupson.apng.decoder.ApngDecoder.Companion.decodeApng import oupson.apng.decoder.ApngDecoder.Companion.decodeApng
import oupson.apng.drawable.ApngDrawable import oupson.apng.drawable.ApngDrawable
@ -52,22 +49,23 @@ class ApngDecoder {
internal var bitmapConfig: Bitmap.Config = Bitmap.Config.ARGB_8888, internal var bitmapConfig: Bitmap.Config = Bitmap.Config.ARGB_8888,
internal var decodeCoverFrame: Boolean = false internal var decodeCoverFrame: Boolean = false
) { ) {
fun getSpeed() : Float = this.speed fun getSpeed(): Float = this.speed
fun setSpeed(speed : Float) : Config { fun setSpeed(speed: Float): Config {
this.speed = speed this.speed = speed
return this return this
} }
fun getBitmapConfig() : Bitmap.Config = this.bitmapConfig fun getBitmapConfig(): Bitmap.Config = this.bitmapConfig
fun setBitmapConfig(config : Bitmap.Config) : Config { fun setBitmapConfig(config: Bitmap.Config): Config {
this.bitmapConfig = config this.bitmapConfig = config
return this return this
} }
fun isDecodingCoverFrame() : Boolean { fun isDecodingCoverFrame(): Boolean {
return this.decodeCoverFrame return this.decodeCoverFrame
} }
fun setIsDecodingCoverFrame(decodeCoverFrame : Boolean) : Config {
fun setIsDecodingCoverFrame(decodeCoverFrame: Boolean): Config {
this.decodeCoverFrame = decodeCoverFrame this.decodeCoverFrame = decodeCoverFrame
return this return this
} }
@ -637,9 +635,10 @@ class ApngDecoder {
file: File, file: File,
imageView: ImageView, imageView: ImageView,
callback: Callback? = null, callback: Callback? = null,
config: Config = Config() config: Config = Config(),
scope: CoroutineScope = GlobalScope
) { ) {
GlobalScope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
try { try {
val drawable = val drawable =
decodeApng( decodeApng(
@ -679,10 +678,11 @@ class ApngDecoder {
uri: Uri, uri: Uri,
imageView: ImageView, imageView: ImageView,
callback: Callback? = null, callback: Callback? = null,
config: Config = Config() config: Config = Config(),
scope: CoroutineScope = GlobalScope
) { ) {
val inputStream = context.contentResolver.openInputStream(uri)!! val inputStream = context.contentResolver.openInputStream(uri)!!
GlobalScope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
try { try {
val drawable = val drawable =
decodeApng( decodeApng(
@ -721,9 +721,10 @@ class ApngDecoder {
context: Context, @RawRes res: Int, context: Context, @RawRes res: Int,
imageView: ImageView, imageView: ImageView,
callback: Callback? = null, callback: Callback? = null,
config: Config = Config() config: Config = Config(),
scope: CoroutineScope = GlobalScope
) { ) {
GlobalScope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
try { try {
val drawable = val drawable =
decodeApng( decodeApng(
@ -764,9 +765,10 @@ class ApngDecoder {
url: URL, url: URL,
imageView: ImageView, imageView: ImageView,
callback: Callback? = null, callback: Callback? = null,
config: Config = Config() config: Config = Config(),
scope: CoroutineScope = GlobalScope
) { ) {
GlobalScope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
try { try {
val drawable = decodeApng( val drawable = decodeApng(
context, context,
@ -809,9 +811,10 @@ class ApngDecoder {
string: String, string: String,
imageView: ImageView, imageView: ImageView,
callback: Callback? = null, callback: Callback? = null,
config: Config = Config() config: Config = Config(),
scope: CoroutineScope = GlobalScope
) { ) {
GlobalScope.launch(Dispatchers.IO) { scope.launch(Dispatchers.IO) {
try { try {
if (string.startsWith("http://") || string.startsWith("https://")) { if (string.startsWith("http://") || string.startsWith("https://")) {
decodeApngAsyncInto( decodeApngAsyncInto(