美文网首页
安卓加载动画文件

安卓加载动画文件

作者: hjm1fb | 来源:发表于2018-04-18 11:58 被阅读17次

    Fresco加载Gif动画

    首先在gradle文件添加依赖

    compile 'com.facebook.fresco:fresco:1.8.1'
    compile 'com.facebook.fresco:animated-gif:1.8.1'
    

    封装的接口如下(Kotlin):

    /**
     * 加载本地Gif图片,宽度固定,高度等比缩放
     */
    fun SimpleDraweeView.loadLocalGifByResizeLength(resId: Int) {
        resizeLengthByResource(resId)
        var uri = UriUtil.getUriForResourceId(resId)
        controller = Fresco.newDraweeControllerBuilder().setAutoPlayAnimations(true).setUri(uri).build();
    }
    
    /**
     * 宽度固定,高度等比缩放
     */
    fun ImageView.resizeLengthByResource(resId: Int) {
        try {
            val options = BitmapFactory.Options()
            options.inJustDecodeBounds = true
            BitmapFactory.decodeResource(resources, resId, options)
            // 需要post才能获取高度,不然options.outWidth为0
            post {
                val ratio = width.toFloat() / options.outWidth
                val newHeight = ratio * options.outHeight
                val newLayoutParams = layoutParams
                newLayoutParams.height = newHeight.toInt()
                layoutParams = newLayoutParams
                Log.dMulti("loadLocalImage", ratio.toString(), options.outWidth.toString(), options.outHeight.toString(), width.toString(), newHeight.toString())
            }
        } catch (e: Exception) {
            e?.printStackTrace()
            Log.dMulti("loadLocalImage exception", e?.message)
        }
    }
    

    相关文章

      网友评论

          本文标题:安卓加载动画文件

          本文链接:https://www.haomeiwen.com/subject/kndvkftx.html