问题描述
我寻觅了很多,但没有找到正确的答案。
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的表现来显示图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!