简体   繁体   English

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

[英]How can I resize a image file in Kotlin

My app takes photos and saves them inside the cellphone, however when my list loads many photos it becomes very slow, that is why I need to lower the resolution to the photos. 我的应用程序会拍摄照片并将其保存在手机中,但是当我的列表加载很多照片时,它会变得非常慢,这就是为什么我需要降低照片的分辨率。 How can I do that? 我怎样才能做到这一点?

This is how I create the picture 这就是我创建图片的方式

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
    }

This is how I load the picture 这就是我加载图片的方式

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

You can use glide to resize your image and scaling purposes it's pretty much easier 您可以使用滑行调整图像大小和缩放目的,这非常容易

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

To maintain aspect ratio you can use explicitly scale images 要保持宽高比,您可以使用显式缩放图像

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

Please use Glide for load images from server and resize it . 请使用Glide从服务器加载图像并调整其大小。 It's very easy and useful for you I think . 我认为这对您来说非常容易且有用。

Please check below link for more detail for resize image 请检查以下链接以获取更多详细信息,以调整图像大小

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

repositories { 储存库{

mavenCentral() mavenCentral()

google() 谷歌()

} }

dependencies 依赖

{ implementation 'com.github.bumptech.glide:glide:4.9.0' {实现'com.github.bumptech.glide:glide:4.9.0'

annotationProcessor 'com.github.bumptech.glide:compiler:4.9.0' } 注解处理器'com.github.bumptech.glide:compiler:4.9.0'}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM