加载较小版本的图片以提高性能Android

问题描述 投票:0回答:1

我有一个图像(1.84MB)要加载,我想加载一个较小的版本以获得时间,我也不想存储一个较小的版本。对于信息,这些文件位于一个可移动的SD卡中,我需要加载很多,这就是为什么我想优化它,因为它需要太多的时间......

我红了 加载大型位图 文档。我之前用Glide来加载全图,经过测试,得到的数据是:Glide加载(1.84MB):244 361 667 nsBitmap加载(比例1):370 811 511 nsBitmap加载。

经过一些测试,我得到了这样的统计:Glide load (1.84MB): 244'361'667 nsBitmap load (scale 1): 370'811'511 nsBitmap load (scale 4): 324'403'386 nsBitmap load (scale 8): 269'223'073 nsThumbnail load: 245'250'729 ns

这意味着我在加载小8倍的图片时,使用Bitmap的时间稍长,我不明白为什么。

以下是我的代码。

load with glide:

private void loadGlide(Car c) {
DocumentFile makerDir = contentDirectory.findFile(getMakerById(c.getMakerId()).getName());
DocumentFile carPhoto = makerDir.findFile(c.getFilename());
if(carPhoto == null) {
    Log.d("test", c.getFilename() + " problem");
} else {
    Log.d("test", carPhoto.exists() + "");
}
ImageView img = new ImageView(this);
img.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        goToEdit(c.getId());
    }
});
// standard loading
Glide.with(img.getContext()).load(carPhoto.getUri()).into(img);
resultList.addView(img);

}

用位图加载。

private void scaledLoad(Car c, int scale) {
DocumentFile makerDir = contentDirectory.findFile(getMakerById(c.getMakerId()).getName());
DocumentFile carPhoto = makerDir.findFile(c.getFilename());
Bitmap b = null;
//Decode image size
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = scale;
// convert DocumentFile to File
File image = new File("image_path");
FileInputStream fis = null;
try {
    fis = new FileInputStream(image);
    b = BitmapFactory.decodeStream(fis, null, options);
    fis.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}
if(b != null) {
    ImageView img = new ImageView(this);
    img.setImageBitmap(b);
    resultList.addView(img);
}

}

我也试着加载 缩略图:

private void loadThumbnail() {
Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile("filepath"),
        816, 612);
ImageView img = new ImageView(this);
img.setImageBitmap(ThumbImage);
resultList.addView(img);

}

这里是测试代码,如果你想要的话。

long t0 = System.nanoTime();
scaledLoad(c, 1);
// or
loadGlide(c);
long t1 = System.nanoTime();
t1 = (t1-t0);
Log.d("test", "t1:" + t1);

如果你能回答我或给我任何其他的选择 从SD卡中快速加载下划线图像,你是欢迎的。先谢谢你。

java android android-glide bitmapfactory
1个回答
0
投票

如果你使用的是Glide,你可以使用 "override(width,height) "函数来加载调整大小的图片到imageView中。

这篇博客解释了如何做到这一点。 https:/futurestud.iotutorialsglid-image-resizing-scaling#imageresizingwithoverridexy。

© www.soinside.com 2019 - 2024. All rights reserved.