如何使位图加载速度在 Android 中更快? [英] How to make bitmap loading faster in Android?

查看:49
本文介绍了如何使位图加载速度在 Android 中更快?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个代码可以加载图像的缩略图并将其提供给滑动.然后滑行将加载到支架中.但是,当我加载缩略图时,它会返回一个位图.所以,我正在传递位图来滑动.但是,初始加载需要近 30 秒.我的意思是在画廊里有很多照片.因此,当图库打开时,它根本不会显示 30 秒的图像.然后它会显示位图.

I have a code that loads the thumbnail of the image and feeds it to glide. Then glide will load in the holder. but, As I am loading the thumbnail it returns a bitmap. SO, I am passing bitmap to glide. But, this takes nearly 30sec for initial loading. I mean in the gallery there are lots of photos. So, when the gallery is opened it won't show images for 30sec at all. And then it will show the bitmaps.

另一方面,如果我使用 URI 来提供滑行它会立即加载它.但是,图像正在回收站视图中加载.所以,此时只会加载一些图片.

on the other hand, if I use URI to feed to glide it will load it immediately. But, Images are being loaded in recycler view. So, only some images will be loaded at the time.

加载一些图像后.当我们向上滑动并查看其他照片的加载速度时,显然位图馈送图像的加载速度比 URI 馈送图像快得多.但是,位图需要 30 秒的初始加载时间.

After some images are loaded. When we swipe up and see other photos loading speed then obviously bitmap fed images are loaded way faster than URI fed images. But, bitmap takes 30sec of initial loading time.

有什么办法可以让这个位图加载得更快?

Is there any way to make this bitmap load faster?

...
while (cursor.moveToNext()) {
                Log.d("FetchImages(): ", " Started");
                Bitmap thumbBitmap = null;

                int _thumpId = cursor.getInt(column_index_data);
                Uri uri1 = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, _thumpId);

                Size thumbSize = new Size(150, 150);
                try {
                    thumbBitmap = getApplicationContext().getContentResolver().loadThumbnail(uri1, thumbSize, null);
                } catch (IOException e) {e.printStackTrace();}

                ImageModel ImageModel = new ImageModel();
                ImageModel.setBitmap(thumbBitmap);
                arrayList.add(ImageModel);
            }
            Log.d("FetchImages(): ", " Ended");
            runOnUiThread(() -> {
                Adapter Adapter = new Adapter(getApplicationContext(), arrayList, MainActivity.this);
                recyclerView.setAdapter(Adapter);
                cursor.close();
                Log.d("FetchImages(): ", " RecyclerView Adapter attached");
            });
...

推荐答案

Glide .with(context) .load(url).apply(new RequestOptions().override(150, 150))//用于调整大小.into(imageView);

无需创建位图,它需要更多的计算.Glide 更快更高效.而且如果你想使用位图,那么使用 AsycTask 这样它就不会阻塞主线程并且程序不会挂起.

No need to create bitmap it takes more computation . Glide is more faster and efficient. And still if you want to use bitmap then use AsycTask so it wont block main thread and program will not hang.

这篇关于如何使位图加载速度在 Android 中更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆