应用在android中的图片显示速度较慢 [英] app is slow with images in android

查看:125
本文介绍了应用在android中的图片显示速度较慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对图像执行一个应用程序以在GridView中显示它们,我正在从服务器中获取20张图像.每个图像的分辨率为720 * 540.我使用JSON解析来获取url,并使用下面的代码将其转换为Bitmap以设置图像.

I am doing a app on images to show them in GridView, i am fetching 20 images from server. Resolution of the each image is 720*540.I used JSON parsing to fetch url and used below code to convert into Bitmap in order to set images.

public static Bitmap loadImageFromUrl(String url) {
    InputStream inputStream;Bitmap b;
    try {
        inputStream = (InputStream) new URL(url).getContent();
        BitmapFactory.Options bpo=  new BitmapFactory.Options();
        if(bpo.outWidth>500) {
            bpo.inSampleSize=8;
            b=BitmapFactory.decodeStream(inputStream, null,bpo );
        } else {
            bpo.inSampleSize=2;
            b=BitmapFactory.decodeStream(inputStream, null,bpo );
        }
        return  b;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

我的应用程序运行正常,但加载图像所需的时间过多.这样我的应用程序变慢了.我应该降低图像的分辨率吗?

my app is working fine but it is taking too much time to load the images. So that my app became slow. Should i decrease the resolution of images?

如何解决这个问题?

推荐答案

如果您正在执行网格视图以加载20张具有这种分辨率的图像,则建议以下操作:

If you are doing a grid view to load 20 images of such resolution, I would suggest the following:

  1. 绝对减小图像的尺寸.除非您将平板电脑作为目标,否则就可以了,因为大多数智能手机无法通过20张图像达到该分辨率.

  1. Definitely reduce the size of the images. Unless you are targeting a tablet, you will be fine as most smartphones cannot achieve that resolution with 20 images.

如果可以,则缓存图像.

Cache images if you can.

在其他线程上下载图像.存储HashMap会让您很容易,只需将所有带有图像文件名或其他形式ID的图像视图作为键.在下载图像后将消息发送给您的处理程序,并在解码后更新视图.您可以直接检索视图.只要记住检查它们是否仍在窗口中即可.这样一来,图像将迅速显示出来.我认为对图像进行多线程处理不会有所帮助,只需确保使用另一个线程来推送图像"并更新主UI线程即可.然后,用户体验将得到极大改善.

Download the images on a different thread. Store a HashMap would make it easy for you, just put all the imageviews with the image file names or other form of IDs as keys. send message to your Handler when images are downloaded and update the view after it's decoded. You can retrieve your views directly. Just remember to check if they are still in the window. This way the images will show up one after another quickly. I don't think multithreading the images will help, just make sure to use another thread to "push the images" and the main UI thread updates. User experience will be greatly improved then.

希望这会有所帮助.

---一些实现,我现在没有完整的代码---

---some implementations, I don't have the complete code with me right now---

具有一个数据结构,以将视图与传入的数据进行匹配.在这里非常方便.

Have a data structure to match the views with data that comes in. very handy here.

private HashMap<String,ImageView> pictures;

当您获得图像网址列表时,请对其进行遍历:

When you get the list of image urls, iterate through them:

 pictures.put(id,view);
        try{
            FileInputStream in = openFileInput(id);
            Bitmap bitmap = null;
            bitmap = BitmapFactory.decodeStream(in, null, null);
        view.setImageBitmap(bitmap);
        }catch(Exception e){
            new Thread(new PictureGetter(this,mHandler,id)).start();
        }

(如果图片尚未被缓存,则图片获取器将仅获取该图片并对其进行缓存)

(Here the picture getter will simply fetch the image if it is not cached already and cache it)

更新图像视图的代码:

 if(id!=null){
        ImageView iv = pictures.get(id);
        if(iv!=null){
            try{
                FileInputStream in = openFileInput(id);
                Bitmap bitmap = null;
                bitmap = BitmapFactory.decodeStream(in, null, null);
                iv.setImageBitmap(bitmap);
            }catch(Exception e){
        }
    }

这篇关于应用在android中的图片显示速度较慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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