如何在android中添加ImageLoader
我有一个android游戏应用程序,其中每个关卡都有一个250*250(像素)的图像,所有关卡都在一个名为“game1.java”的活动中。因为许多布局特征在级别中发生变化;当我想改变游戏的等级时,我使用Intent和finish()调用同一个类。 因为我在ram使用方面有问题,我尝试在game1类中使用:如何在android中添加ImageLoader,android,github,universal-image-loader,Android,Github,Universal Image Loader,我有一个android游戏应用程序,其中每个关卡都有一个250*250(像素)的图像,所有关卡都在一个名为“game1.java”的活动中。因为许多布局特征在级别中发生变化;当我想改变游戏的等级时,我使用Intent和finish()调用同一个类。 因为我在ram使用方面有问题,我尝试在game1类中使用: ImageLoader imageLoader; 但不幸的是,每次我想开始活动时,我都会感到沮丧 这是game1类的一个属性: ImageLoader imageLoader; 此部分
ImageLoader imageLoader;
但不幸的是,每次我想开始活动时,我都会感到沮丧
这是game1类的一个属性:
ImageLoader imageLoader;
此部分位于onCreate的开头:
super.onCreate(savedInstanceState);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
File cacheDir = StorageUtils.getCacheDirectory(getBaseContext());
DisplayImageOptions options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(0)
.cacheInMemory(false) // default
.cacheOnDisk(true) // default
.build();
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(getBaseContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.discCacheSize(50 * 1024 * 1024)
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.build();
每当我需要一个新的映像时,我调用这个函数,并给出位于assets文件夹中的映像的strName和扩展名。例如“image1.jpg”
然后我通过setImageBitmap函数将图像提供给ImageView
问题出在哪里?这是一个非常简单的错误:) 您没有定义imageLoader,当您尝试调用
loadImage(…)
时,imageLoader
为空
更改代码ImageLoader.getInstance().init(配置)代码>到这个
imageLoader = ImageLoader.getInstance();
imageLoader.init(config);
利用朋友的帮助;这就是我最终解决问题的方法:
步骤1:我在游戏的第一个活动中设置我的加载器;在onCreate中:
super.onCreate(savedInstanceState);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
File cacheDir = StorageUtils.getCacheDirectory(getBaseContext());
DisplayImageOptions options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(0)
.cacheInMemory(false) // default
.cacheOnDisk(true) // default
.build();
DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoaderConfiguration config = new ImageLoaderConfiguration
.Builder(getBaseContext())
.defaultDisplayImageOptions(defaultOptions)
.memoryCache(new LruMemoryCache(2 * 1024 * 1024))
.discCacheSize(50 * 1024 * 1024)
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.build();
步骤2:
在game1.class中;super.oncreate之后:
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this).build();
ImageLoader.getInstance().init(config);
File cacheDir = StorageUtils.getCacheDirectory(getBaseContext());
DisplayImageOptions options = new DisplayImageOptions.Builder()
.resetViewBeforeLoading(false) // default
.delayBeforeLoading(0)
.cacheInMemory(false) // default
.cacheOnDisk(true) // default
.build();
我给了一个字符串所需的imageaddress
ax_level = "level_01.jpg";
定义了我的imageview
ImageView posht_safhe = (ImageView) findViewById(R.id.rook_ax);
将图像提供给我的imageView
String pin_url = "assets://"+ax_level;
DisplayImageOptions option = new DisplayImageOptions.Builder()
.cacheOnDisc(true)
.cacheInMemory(false )
.bitmapConfig(Bitmap.Config.RGB_565)
.build();
ImageLoader.getInstance().displayImage(pin_url ,posht_safhe, option, new SimpleImageLoadingListener() {
@Override
public void onLoadingComplete(String imageUri, View view,
Bitmap loadedImage) {
// bitmap = loadedImage;
}
});
最后它成功了我的答案不能解决你的问题吗?我在帖子中添加了我的答案