本文介绍了Android的web视图与ImageView的表现来显示图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻觅了很多,但没有找到正确的答案。

I have searched a lot, but never found the right answer.

哪种方式比较好/更快地显示大或小的图像?

Which way is better/faster to display small or big images?

推荐答案

这可能不会直接回答你的问题,但你尝试过用乱射的 NetworkImageView 类?我最近使用的凌空抽射一个大项目来显示图像,这是比任何我以前见过更快,更稳定。凌空是在谷歌好人开发Android的一个网络图书馆,它包含下载图像作为工具箱的一部分。有没有官方文件的权利,但你可以找到任何数量的在网络上的例子。它处理或大或小的图像提供方便。所有你需要做的是凌空库 com.android.volley.toolbox.NetworkImageView 的ImageView 在你的XML布局/ code>。

This may not directly answer your question, but have you tried using Volley's NetworkImageView class ? I recently used Volley in a large project to display images and it was faster and more stable than anything I've seen before. Volley is a networking library for Android developed by the good guys at Google, and it includes image downloading as part of its toolbox. There's no official documentation right now, but you can find any number of examples on the net. It handles small or large images with ease. All you need to do is add the Volley library to your project and replace ImageView in your XML layout with com.android.volley.toolbox.NetworkImageView.

以下变量添加到您的活动类:

Add the following variables to your Activity class:

private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;

创建您的活动的onCreate()方法的对象:

mRequestQueue = Volley.newRequestQueue(context);
mImageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() {

    private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);

    public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
    }

    public Bitmap getBitmap(String url) {
        return mCache.get(url);
    }
});

然后下载图像的适配器类的 getView()方法:

NetworkImageView image = (NetworkImageView)view.findViewById(R.id.image);
image.setImageUrl("http://somerandomurl.com/somerandomimage.png",mImageLoader);

在生产code,你会同时使用请求队列 ImageLoader的类的全局实例,和你的的onCreate()方法不会混乱,因为它是在这个玩具的例子。

In production code, you would use a global instance of both the RequestQueue and ImageLoader classes, and your onCreate() method wouldn't be cluttered as it is in this toy example.

我不会用网页视图来显示图像,但当然是可以做到的。如果你真的想看看哪些方法是更快,你可以尝试的ImageView NetworkImageView 的WebView 加载一个大的图像,并得到一个粗略的估算时间与 System.nanoTime()方法。

I wouldn't use a webview to display images, though of course it can be done. If you really want to see which way is "faster", you can try out ImageView, NetworkImageView and WebView to load a large image and get a rough time estimate with the System.nanoTime() method.

这篇关于Android的web视图与ImageView的表现来显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 10:05
查看更多