美文网首页
Android ImageView+Glide实现加载长图片(适

Android ImageView+Glide实现加载长图片(适

作者: hao_developer | 来源:发表于2020-11-09 09:48 被阅读0次

    实现效果:


    image.png
    image.png

    布局实现:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.core.widget.NestedScrollView
        xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <ImageView
            android:id="@+id/ivImg"
            android:adjustViewBounds="true"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>
    
    </androidx.core.widget.NestedScrollView>
    

    activity实现

    import android.annotation.SuppressLint
    import android.graphics.BitmapFactory
    import android.os.Bundle
    import androidx.appcompat.app.AppCompatActivity
    import com.bumptech.glide.Glide
    import com.bumptech.glide.request.target.SimpleTarget
    import com.bumptech.glide.request.transition.Transition
    import kotlinx.android.synthetic.main.activity_glide_view.*
    import java.io.File
    
    
    /** 
     *@Created by wrs on 2020/11/7,15:46
     *@packageName: com.dsy.tui
     *@Description: 图片加载
     */
    class GlideImageActivity : AppCompatActivity() {
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_glide_view)
    
            val imgPath = "https://yd-imgs.380star.com/upload/uploadfile/2020/4/23/158763676174928364815980349689.jpg"
    
            Glide.with(this).load(imgPath)
                .downloadOnly(object : SimpleTarget<File?>() {
                    override fun onResourceReady(resource: File, transition: Transition<in File?>?) {
                        // 将保存的图片地址给SubsamplingScaleImageView,这里注意设置ImageViewState设置初始显示比例
                        val bitmap =
                            BitmapFactory.decodeFile(resource.getAbsolutePath(), getBitmapOption(1))
                        // 显示处理好的Bitmap图片
                        ivImg.setImageBitmap(bitmap)
                    }
    
                })
    
        }
    
        /**
         * 设置Bitmap的Options
         */
        private fun getBitmapOption(inSampleSize: Int): BitmapFactory.Options? {
            System.gc()
            val options = BitmapFactory.Options()
            options.inPurgeable = true
            options.inSampleSize = inSampleSize
            return options
        }
    }
    

    相关文章

      网友评论

          本文标题:Android ImageView+Glide实现加载长图片(适

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