美文网首页
android imageview加载长图Glide+Subsa

android imageview加载长图Glide+Subsa

作者: hao_developer | 来源:发表于2020-09-29 19:02 被阅读0次
    image.png
    158763676174928364815980349689.jpg

    长图图片地址:
    https://yd-imgs.380star.com/upload/uploadfile/2020/4/23/158763676174928364815980349689.jpg

    使用第三方加载图片插件:

    implementation 'com.davemorrissey.labs:subsampling-scale-image-view-androidx:3.10.0'
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    

    第三方imageview的github地址:
    https://github.com/davemorrissey/subsampling-scale-image-view

    布局:

    <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
                android:id="@+id/imageview"
                android:layout_width="match_content"
                android:layout_height="wrap_content"
                android:adjustViewBounds="true" />
    

    加载使用方法

    package com.dsy.test
    
    import android.content.Context
    import android.graphics.BitmapFactory
    import android.graphics.PointF
    import android.graphics.drawable.Drawable
    import android.net.Uri
    import android.os.Bundle
    import android.view.WindowManager
    import androidx.appcompat.app.AppCompatActivity
    import androidx.core.content.ContextCompat
    import com.bumptech.glide.Glide
    import com.bumptech.glide.request.target.SimpleTarget
    import com.bumptech.glide.request.transition.Transition
    import com.davemorrissey.labs.subscaleview.ImageSource
    import com.davemorrissey.labs.subscaleview.ImageViewState
    import com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
    import kotlinx.android.synthetic.main.activity_two_image_view.*
    import java.io.File
    
    /** 
     *@Created by wrs on 2020/8/13,9:34
     *@packageName: com.dsy.test
     *@Description: 测试
     */
    class ThreeActivity : AppCompatActivity() {
    
    
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_two_image_view)
    
            val url = "https://yd-imgs.380star.com/upload/uploadfile/2020/4/23/158763676174928364815980349689.jpg";
    
            loadLargeImage(this,url,imageview)
    
        }
    
      //加载图片的方法
       fun loadLargeImage(context: Context, res: String, imageView: SubsamplingScaleImageView) {
            imageView.isQuickScaleEnabled = true
            imageView.maxScale = 15F;
            imageView.isZoomEnabled = true;
            imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM)
    
            Glide.with(context).load(res).downloadOnly(object : SimpleTarget<File?>() {
                override fun onResourceReady(resource: File, glideAnimation: Transition<in File?>?) {
                    val sWidth = BitmapFactory.decodeFile(resource.absolutePath).width
                    val sHeight = BitmapFactory.decodeFile(resource.absolutePath).height
                    val wm = ContextCompat.getSystemService(context, WindowManager::class.java)
                    val width = wm?.defaultDisplay?.width ?: 0
                    val height = wm?.defaultDisplay?.height ?: 0
                    if (sHeight >= height
                            && sHeight / sWidth >= 3) {
                        imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CENTER_CROP)
                        imageView.setImage(ImageSource.uri(Uri.fromFile(resource)), ImageViewState(0.5f, PointF(0f, 0f), 0))
                    } else {
                        imageView.setMinimumScaleType(SubsamplingScaleImageView.SCALE_TYPE_CUSTOM)
                        imageView.setImage(ImageSource.uri(Uri.fromFile(resource)))
                        imageView.setDoubleTapZoomStyle(SubsamplingScaleImageView.ZOOM_FOCUS_CENTER_IMMEDIATE)
                    }
                }
                override fun onLoadFailed(errorDrawable: Drawable?) {
                    super.onLoadFailed(errorDrawable)
                }
            })
       }
    
    
    }
    

    相关文章

      网友评论

          本文标题:android imageview加载长图Glide+Subsa

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