【问题标题】:Load images into a Gallery after Activity's view is shown显示 Activity 的视图后将图像加载到图库中
【发布时间】:2012-10-15 07:27:24
【问题描述】:

我正在开发一个 Android 3.1 平板电脑应用程序。

我需要加载并显示一些存储在本地磁盘中的图像。知道 UI 在图像加载并显示在画廊中时冻结,我想知道是否有另一种显示 ui 的方式,当显示 ui 时,显示ProgressDialog,然后开始从磁盘加载并显示每张图像。

我知道如何开始和Asyntask,但是在显示 ui 后我不知道该怎么做。

我将task.execute() 放在OnCreate 和OnResume 上,我看到UI 冻结。

显示 UI 后如何执行任务?

如果您需要查看代码,请查看Gallery images still load very slow 和ClassCastException: AbsListView$LayoutParams cannot be cast to Gallery$LayoutParams 问题。

【问题讨论】:

  • 为了更好地理解,您想要一个包含从内部存储器加载的图像的画廊吗?他们已经下载了吗?为什么需要异步任务?只是为了展示他们?顺便说一句,gallery 已弃用,为什么不使用 Horizo​​ntalScroolView 或 viewpager?
  • 不,我有一个带有图像路径的ArrayList<String>。我将从本地磁盘加载这些图像,它们在本地磁盘上。我是 Android 开发新手,但我不知道 Gallery 已被弃用。
  • 本地磁盘是什么意思?我以为你把它们放在手机的内存或卡上?
  • 哦,对不起。它们在 SD 卡上。

标签: android android-layout user-interface android-asynctask android-gallery


【解决方案1】:

首先,您应该有一个带有画廊(或您决定使用的任何东西)的 Activity 和一个画廊适配器。在适配器的getView() 中,您应该加载图像(getView() 是每个图库对象的图库)。 对于加载,您应该有这样的方法:

public static Bitmap loadFromPath(String file_path) {
   BitmapFactory.Options options = new BitmapFactory.Options();
   options.inDither = false; // disable dithering
   options.inScaled = false; // do not scale bitmap when loading; scale later
   options.inPreferredConfig = Bitmap.Config.ARGB_8888;
   Bitmap backgroundBitmap = null;
    try {
         backgroundBitmap = BitmapFactory.decodeFile(file_path, options);
     } catch (OutOfMemoryError e) {
          e.printStackTrace();
     }
     return backgroundBitmap;

}

这个返回的位图可以用来在getView() 中设置imageView 的源。 如果您有问题,请告诉我!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-23
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    • 2019-05-19
    • 2015-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多