如何在Kotlin中调整图像文件的大小

时间:2019-06-03 00:48:54

标签: android image kotlin

我的应用程序拍摄照片并将其保存在手机中,但是当我的列表加载许多照片时,它变得非常慢,这就是为什么我需要降低照片的分辨率。我该怎么办?

这就是我创建图片的方式

fun createImage():File{
        val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
        val imageName = "JPEG_"+timeStamp+"_"
        var storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
        var image = File.createTempFile(imageName,".jpg",storageDir)

        currentPath=image.absolutePath
        return image
    }

这就是我加载图片的方式

  var beerPhoto: ImageView?=null
  ....
  viewHolder.beerPhoto?.setImageURI(Uri.parse(userDto.beerPhoto))

2 个答案:

答案 0 :(得分:1)

您可以使用滑行调整图像大小和缩放目的,这非常简单

GlideApp  
    .with(context)
    .load(userDto.beerPhoto)
    .override(600, 200)  // it don't maintain aspect ratio 
    .into(beerPhoto);

要保持宽高比,您可以使用显式缩放图像

GlideApp  
    .with(context)
    .load(userDto.beerPhoto)
    .override(600, 200)
    .centerCrop() // or use fitcenter
    .into(beerPhoto);

答案 1 :(得分:0)

请使用Glide从服务器加载图像并调整大小。我认为这对您来说非常容易且有用。

请检查以下链接以获取更多有关调整图像大小的信息

https://guides.codepath.com/android/Displaying-Images-with-the-Glide-Library

  

存储库    {

     

mavenCentral()

     

google()

     

}

     

依赖性

     

{    实施'com.github.bumptech.glide:glide:4.9.0'

     

annotationProcessor'com.github.bumptech.glide:compiler:4.9.0'   }