【问题标题】:How to add an ImageLoader in android如何在android中添加ImageLoader
【发布时间】:2014-09-22 12:26:32
【问题描述】:

我有一个 android 游戏应用程序,其中每个级别都有一个 250*250(像素)的图像,所有级别都在一个名为“game1.java”的 Activity 中。因为许多布局特征在关卡中发生了变化;当我想更改游戏级别时,我使用 Intent 和 finish() 调用同一类。 因为我的 ram 使用有问题,我尝试在 game1 类中使用它:

https://github.com/nostra13/Android-Universal-Image-Loader

但每次我想打开活动时,我都会很遗憾。

这是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();

每当我需要一个新图像时,我都会调用此函数并给出我在资产文件夹中的图像的 strName 名称和扩展名。例如“image1.jpg”

private Bitmap getBitmapFromAsset(String strName)
{
    String imageUri = "assets://" + strName;

    //Bitmap bmp = ImageLoader.getInstance().loadImageSync(imageUri);
    imageLoader.loadImage(imageUri,new SimpleImageLoadingListener(){
        @Override
        public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {

            super.onLoadingComplete(imageUri, view, loadedImage);
            bmp_loader=loadedImage;
        }
    });

    return bmp_loader;
}

然后我通过 setImageBitmap 函数将图像提供给 ImageView

问题出在哪里?

【问题讨论】:

  • 我的回答不能解决你的问题?
  • 我在帖子中添加了我的答案

标签: android github universal-image-loader


【解决方案1】:

利用我朋友的帮助;这就是我最终解决问题的方法:

第 1 步:我在游戏的第一个活动中设置加载程序;在 onCreate 中:

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();

我把需要的图片地址给了一个字符串

   ax_level = "level_01.jpg";

定义了我的图像视图

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;
        }
    });

终于成功了

【讨论】:

    【解决方案2】:

    这是一个非常简单的错误:) 当您尝试调用 loadImage(...) 时,您没有定义 imageLoader 并且 imageLoader 为空!

    将您的代码 ImageLoader.getInstance().init(config); 更改为此
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(config);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-09-29
      • 2021-12-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多