图像动态加载R.Drawable使用可变 [英] Dynamic loading of images R.Drawable using variable

查看:268
本文介绍了图像动态加载R.Drawable使用可变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得我越来越近,但仍然得到资源的错误与此有关。我有一个名为rock.png在所有3绘制文件夹的图片文件。

I think i'm getting closer but still getting resource errors with this. I have an image file named rock.png in all 3 drawable folders.

在我的布局main.xml中:

in my layout MAIN.XML:

 <ImageView android:id="@+id/rockId" android:src="@drawable/rock"></ImageView>

在我的code:

            int resID = getResources().getIdentifier("rockId" , "id", "com.testing");
            ImageView image = (ImageView) findViewById(resID);

我仍然看到在我的错误目录下载:

I'm still seeing this in my error catlog:

10-30 17:36:24.485: WARN/ResourceType(74): Resources don't contain package for resource number 0x7f020000

这是什么,我可能是做错了什么想法?任何提示欢迎

Any thoughts on what I might be doing wrong? Any tips welcome

推荐答案

要找到控制:

ImageView image = (ImageView) findViewById(R.id.rockId);

要动态地从绘制我用这个函数加载图像

To dynamicly load an image from drawable i use this function

    public static int getDrawable(Context context, String name)
    {
        Assert.assertNotNull(context);
        Assert.assertNotNull(name);

        return context.getResources().getIdentifier(name,
                "drawable", context.getPackageName());
    }

这将返回您可绘制的ID,现在你需要到设置在控制图像:

this will return the id of your drawable, now all you need to to is set the image in the control:

image.setImageResource(int Id);

这篇关于图像动态加载R.Drawable使用可变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆