1.用到的库:
glide SubsamplingScaleImageView
2.使用方法:
先使用Glide异步下载图片,再用SubsamplingScaleImageView无损显示大图
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).asFile().load(res).into(object : CustomTarget<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 point = Point()
wm!!.defaultDisplay.getSize(point)
val height = point.y//测量不包括标题栏和状态栏的高度
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 onLoadCleared(placeholder: Drawable?) {
}
})
}
|