【问题标题】:Get a dynamic list of Images获取图片的动态列表
【发布时间】:2011-05-27 05:59:37
【问题描述】:

我正在关注 Android Hello-GridView tutorial,并希望使用我的 res/drawable 文件夹中的动态图像列表,而不是建议的硬编码数组:

// references to our images
private Integer[] mThumbIds = {
        R.drawable.sample_2, R.drawable.sample_3,
        R.drawable.sample_4, R.drawable.sample_5,
        R.drawable.sample_6, R.drawable.sample_7,
        R.drawable.sample_0, R.drawable.sample_1
};

如何动态循环和加载这些图像?

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    尝试为此使用assets 文件夹,而不是将它们定义为资源。只需将图像转储到 res/assets 下的文件夹中(在本例中为 res/assets/images)。然后,以下代码应获取该文件夹中的文件列表:

    AssetManager assets = getAssets();
    String[] drawables = assets.list("images");
    

    然后,只需使用上面发布的代码@Jack Smartie 设置可绘制对象:

    imageView.setBackgroundDrawable(Drawable.createFromPath(drawables[i]))
    

    其中 'i' 是您想要的可绘制对象的索引。

    【讨论】:

      【解决方案2】:

      更新:糟糕,我没有认真阅读您的问题。我不确定除了将资源硬编码到数组中之外,您将如何动态加载可绘制文件夹中的资源。

      你好,

      我在我的应用中做了类似的事情。

      查看ImageAdapter 类的getView 方法。在代码的 if (view == null) 部分中,在 else 之前,使用 ImageView 的设置器之一(例如 setImageBitmap、setImageDrawable、setBackground 等)。

      我忘了提到您需要创建一个字符串数组来保存图像的文件路径。

      在我的代码中,这是我的行:

      imageView.setBackgroundDrawable(Drawable.createFromPath(MainActivity.imageStringArray[position]));

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-01-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-12-06
        • 2018-04-18
        • 2013-06-09
        相关资源
        最近更新 更多