实现效果:
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
}
}
网友评论