Kotlin协程运行阻止无法按预期方式运行,并且无法阻止for循环的执行

编码器

在我的应用程序中,我正在执行两个for循环,但是这些for循环需要按顺序安排,这里是用例:

有两个for循环:1- ImageStickerslist 2-TextStickerslist

我想做的是在imagestickerslist之后,如果正确定义,则将执行textstickerslist。

这里的imagesticker列表由URL路径组成,用于从滑行加载图像,但是,如果这些图像具有高分辨率,即使尚未从URL加载图像,它也会最终使线程继续。为了解决这个问题,尝试添加阻塞调用以在ready和full方法上滑动,但这不会带来任何帮助。我非常困惑阻塞呼叫如何工作,对此将有所帮助。

这是执行循环的我的代码:

 runBlocking {
                            launch {
                                imagestickers.forEach {
                                    runBlocking {
                                        var image = it.path
                                        var x = it.x
                                        var y = it.y
                                        image!!.log()
                                        setimagestickers(image!!, x!!, y!!, it.width!!, it.height!!)
                                    }
                                }
                            }.join()
                            textstickers.forEach {
                                runBlocking {
                                    var text = it.text.toString()
                                    var color = it.color
                                    var font = it.font
                                    var size = it.size
                                    var x = it.x
                                    var y = it.y
                                    setTextSticker(text, Color.parseColor(color), size!!, x!!, y!!)
                                }
                            }
                        }

这是我进行主要计算的两种方法:

 fun setimagestickers(path:String,x:Int,y:Int,w:Int,h:Int){

           Glide.with(this@NewStickerActivity).asBitmap().timeout(6000000).load(path).into(object : CustomTarget<Bitmap>() {
               override fun onLoadCleared(placeholder: Drawable?) {

               }

               override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                   var size: ViewGroup.LayoutParams
                   var bmp1 = resource
                   size = UiHelper.getHeightWidth(this@NewStickerActivity, (w).toInt(), (h).toInt())
                   var resizedBitmap = Bitmap.createScaledBitmap(bmp1, size.width, size.height, false)
                   var drawable = BitmapDrawable(resources, resizedBitmap)
                   var dsImageSticker = DrawableSticker(drawable)
                   dsImageSticker.setTag("ImageSticker")
                   var pm: List<Int>
                   if (density > 3.0) {
                       pm = UiHelper.getmargins(this@NewStickerActivity, (x).toInt(), (y).toInt())
                   } else {
                       pm = UiHelper.getmargins(this@NewStickerActivity, (x).toInt(), (y).toInt())
                   }
                   Log.i("Hmmm:", pm.get(0).toFloat().toString() + "::" + pm.get(1).toFloat().toString())

                   stickerView.addStickerAndSetMatrix1(
                           dsImageSticker,
                           pm.get(0).toFloat(),
                           pm.get(1).toFloat()
                   )

               }

           })
    }
   fun setTextSticker(text: String, color: Int,size: Int, x: Int, y: Int){
            val bmp1: Bitmap
            val drawable: Drawable
            var l: List<Int>
            if (density > 3.0) {
                l = UiHelper.getmargins(this@NewStickerActivity, (x).toInt(), (y * 1.07).toInt())
            } else {
                l = UiHelper.getmargins(this@NewStickerActivity, x.toInt(), y.toInt())
            }
            //var tf = Typeface.createFromFile(assets,"fonts/"+path)
            var tf = Typeface.createFromAsset(assets, "fonts/Myriad Pro Bold SemiExtended.ttf")
            bmp1 = createBitmapFromLayoutWithText(this@NewStickerActivity, size, text, color, 0f, tf, 0f, 0f, color, Gravity.LEFT)
            drawable = BitmapDrawable(resources, bmp1)
            var dsTextSticker = DrawableSticker(drawable)
            dsTextSticker.setTag("textSticker")
            Log.i("Hmmm:", l.get(0).toFloat().toString() + "::" + l.get(1).toFloat().toString())
            /*if (rotate) {
                stic.addStickerAndSetrotate(
                        dsTextSticker, rotationdegress,
                        l.get(0).toFloat(),
                        l.get(1).toFloat()
                )
            } else {*/
            stickerView.addStickerAndSetMatrix1(
                    dsTextSticker,
                    l.get(0).toFloat(),
                    l.get(1).toFloat())



    }

更新

通过按顺序递增和获取图像,我无需协程就可以工作:首先,我获取一个Int,然后不断递增,直到达到列表大小,这是我的代码:首先,我这样做了:

var i = 0 
setimagestickers(imagestickers.get(i).path!!, imagestickers.get(i).x!!, imagestickers.get(i).y!!, imagestickers.get(i).width!!, imagestickers.get(i).height!!)

之后,在资源上做好准备就可以了!

Glide.with(this@NewStickerActivity).asBitmap().timeout(6000000).load(path).into(object : CustomTarget<Bitmap>() {
               override fun onLoadCleared(placeholder: Drawable?) {

               }

               override fun onResourceReady(resource: Bitmap, transition: Transition<in Bitmap>?) {
                   var size: ViewGroup.LayoutParams
                   var bmp1 = resource
                   size = UiHelper.getHeightWidth(this@NewStickerActivity, (w).toInt(), (h).toInt())
                   var resizedBitmap = Bitmap.createScaledBitmap(bmp1, size.width, size.height, false)
                   var drawable = BitmapDrawable(resources, resizedBitmap)
                   var dsImageSticker = DrawableSticker(drawable)
                   dsImageSticker.setTag("ImageSticker")
                   var pm: List<Int>
                   if (density > 3.0) {
                       pm = UiHelper.getmargins(this@NewStickerActivity, (x).toInt(), (y).toInt())
                   } else {
                       pm = UiHelper.getmargins(this@NewStickerActivity, (x).toInt(), (y).toInt())
                   }
                   Log.i("Hmmm:", pm.get(0).toFloat().toString() + "::" + pm.get(1).toFloat().toString())

                   stickerView.addStickerAndSetMatrix1(
                           dsImageSticker,
                           pm.get(0).toFloat(),
                           pm.get(1).toFloat()
                   )
                i++
                if(i < imagestickers.size){
                    setimagestickers(imagestickers.get(i).path!!, imagestickers.get(i).x!!, imagestickers.get(i).y!!, imagestickers.get(i).width!!, imagestickers.get(i).height!!)
                }
                   else{
                    if(textstickers.isNullOrEmpty()){
                        loader!!.hide()
                    }
                    else {
                        setTextSticker(textstickers.get(j).text!!, Color.parseColor(textstickers.get(j).color), textstickers.get(j).size!!, textstickers.get(j).x!!, textstickers.get(j).y!!)
                    }
                    }
               }

           })

但是我仍然想知道如何用协程而不是这种方法来解决它!

马克·托波尼克(Marko Topolnik)

基本上,您要进行异步Glide调用,而不会暂停协程直到完成。您必须更改的第一件事是引入suspend fun getImageSticker()

suspend fun getSticker(path: String): Bitmap =
    suspendCancellableCoroutine { continuation -> Glide
        .with(this@NewStickerActivity)
        .asBitmap()
        .timeout(6000000)
        .load(path)
        .into(object : CustomTarget<Bitmap>() {
            override fun onResourceReady(resource: Bitmap, x: Transition<in Bitmap>?) {
                continuation.resume(resource)
            }
        })
    }

请注意,这将返回Bitmap现在,您可以像调用常规阻塞函数一样调用它,并从调用方使用它的结果:

suspend fun setimagestickers(path: String, x: Int, y: Int, w: Int, h: Int) {
    val resource = getSticker(path)
    var size: ViewGroup.LayoutParams
    var bmp1 = resource
    size = UiHelper.getHeightWidth(this@NewStickerActivity, (w).toInt(), (h).toInt())
    var resizedBitmap = Bitmap.createScaledBitmap(bmp1, size.width, size.height, false)

}

现在,如果要并行化setimagestickers调用,请循环启动它们:

launch {
    imagestickers.map {
        launch {
            var image = it.path
            var x = it.x
            var y = it.y
            image!!.log()
            setimagestickers(image!!, x!!, y!!, it.width!!, it.height!!)
        }
    }.joinAll()
    textstickers.forEach {
        ...
    }
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

C ++ while循环无法按预期/希望的方式运行

来自分类Dev

设备轮换通知无法按预期方式运行iOS Swift

来自分类Dev

嵌套公式无法按预期的方式运行Google表格

来自分类Dev

isKindOfClass无法按预期的方式运行IOS 7

来自分类Dev

数组无法在jQuery中按预期方式运行

来自分类Dev

在datagrid中添加行无法按预期的方式运行mvvm

来自分类Dev

Onclick无法按预期运行

来自分类Dev

Valueallowunset无法按预期运行

来自分类Dev

for循环中的条件表达式无法按预期运行

来自分类Dev

Kotlin协程会阻止Android中的主线程

来自分类Dev

析构函数无法按预期运行,并且出现分段错误(C)

来自分类Dev

WHERE子句无法按预期运行

来自分类Dev

RewriteRule无法按预期的.htaccess运行

来自分类Dev

isdigit()函数无法按预期运行?

来自分类Dev

which.max无法按预期运行

来自分类Dev

PHP strpos()无法按预期运行

来自分类Dev

函数无法按预期在PHP中运行

来自分类Dev

WHERE子句无法按预期运行

来自分类Dev

SQL命令无法按预期运行

来自分类Dev

模式匹配无法按预期运行python

来自分类Dev

RewriteRule无法按预期的.htaccess运行

来自分类Dev

isdigit()函数无法按预期运行?

来自分类Dev

不在,并且存在无法按我预期的方式工作

来自分类Dev

du -sh --exclude = {}命令中的cat / echo命令导致长时间运行,并且无法按预期运行

来自分类Dev

在后台运行命令无法按预期运行

来自分类Dev

store.filter()无法按预期的方式运行ember.js(尝试搜索模型)

来自分类Dev

CMake SET命令无法按预期方式运行(CMake未设置预定义缓存变量的值)

来自分类Dev

Java字符串拆分,无法按我预期的方式运行

来自分类Dev

无法使相对路径,重写规则和路由按我预期的方式运行

Related 相关文章

  1. 1

    C ++ while循环无法按预期/希望的方式运行

  2. 2

    设备轮换通知无法按预期方式运行iOS Swift

  3. 3

    嵌套公式无法按预期的方式运行Google表格

  4. 4

    isKindOfClass无法按预期的方式运行IOS 7

  5. 5

    数组无法在jQuery中按预期方式运行

  6. 6

    在datagrid中添加行无法按预期的方式运行mvvm

  7. 7

    Onclick无法按预期运行

  8. 8

    Valueallowunset无法按预期运行

  9. 9

    for循环中的条件表达式无法按预期运行

  10. 10

    Kotlin协程会阻止Android中的主线程

  11. 11

    析构函数无法按预期运行,并且出现分段错误(C)

  12. 12

    WHERE子句无法按预期运行

  13. 13

    RewriteRule无法按预期的.htaccess运行

  14. 14

    isdigit()函数无法按预期运行?

  15. 15

    which.max无法按预期运行

  16. 16

    PHP strpos()无法按预期运行

  17. 17

    函数无法按预期在PHP中运行

  18. 18

    WHERE子句无法按预期运行

  19. 19

    SQL命令无法按预期运行

  20. 20

    模式匹配无法按预期运行python

  21. 21

    RewriteRule无法按预期的.htaccess运行

  22. 22

    isdigit()函数无法按预期运行?

  23. 23

    不在,并且存在无法按我预期的方式工作

  24. 24

    du -sh --exclude = {}命令中的cat / echo命令导致长时间运行,并且无法按预期运行

  25. 25

    在后台运行命令无法按预期运行

  26. 26

    store.filter()无法按预期的方式运行ember.js(尝试搜索模型)

  27. 27

    CMake SET命令无法按预期方式运行(CMake未设置预定义缓存变量的值)

  28. 28

    Java字符串拆分,无法按我预期的方式运行

  29. 29

    无法使相对路径,重写规则和路由按我预期的方式运行

热门标签

归档