如何在android中动态设置drawable图像?

时间:2012-07-31 10:00:47

标签: android

我将项目相关图像存储在drawable文件夹中。另外,我将图像名称存储在字符串变量中,并且我正在尝试将这些图像设置为imageview。但图像没有显示。请帮助我这方面。

我的代码:

int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);

在上面的代码中,“imagename”是包含图像名称的字符串变量。

提前致谢

17 个答案:

答案 0 :(得分:110)

试试这个:

String uri = "@drawable/myresource";  // where myresource (without the extension) is the file

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
imageView.setImageDrawable(res);

答案 1 :(得分:34)

此处我将frnd_inactive图片从drawable设置为图片

 imageview= (ImageView)findViewById(R.id.imageView);
 imageview.setImageDrawable(getResources().getDrawable(R.drawable.frnd_inactive));

答案 2 :(得分:22)

imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(R.drawable.mydrawable) 

答案 3 :(得分:18)

试试动态代码

String fnm = "cat"; //  this is image file name
String PACKAGE_NAME = getApplicationContext().getPackageName();
int imgId = getResources().getIdentifier(PACKAGE_NAME+":drawable/"+fnm , null, null);
System.out.println("IMG ID :: "+imgId);
System.out.println("PACKAGE_NAME :: "+PACKAGE_NAME);
//    Bitmap bitmap = BitmapFactory.decodeResource(getResources(),imgId);
your_image_view.setImageBitmap(BitmapFactory.decodeResource(getResources(),imgId));

在上面的代码中,您需要具有的Image-file-Name和Image-View对象

答案 4 :(得分:8)

见以下代码,这对我有用

iv.setImageResource(getResources().getIdentifier(
                "imagename", "drawable", "com.package.application"));

答案 5 :(得分:5)

这对我有用(不要使用图像的扩展名,只是名称):

String imagename = "myImage";
int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);
imageview.setImageResource(res);

答案 6 :(得分:5)

以下是今天在每部手机中都有效的最佳方式。大部分答案都已弃用或需要API> = 19或22。 您可以根据需要使用片段代码或活动代码。

<强>片段

//setting the bitmap from the drawable folder
Bitmap bitmap= BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.my_image);

//set the image to the imageView
imageView.setImageBitmap(bitmap);

<强>活动

//setting the bitmap from the drawable folder
Bitmap bitmap= BitmapFactory.decodeResource(MyActivity.this.getResources(), R.drawable.my_image);

//set the image to the imageView
imageView.setImageBitmap(bitmap);

干杯!

答案 7 :(得分:3)

像这样设置ImageViewimageView.setImageResource(R.drawable.image)

答案 8 :(得分:3)

getDrawable()已弃用,请尝试使用

imageView.setImageResource(R.drawable.fileName)

答案 9 :(得分:3)

imageView.setImageResource(R.drawable.picname);

答案 10 :(得分:2)

自API 22起,getResources().getDrawable()已弃用(另请参阅Android getResources().getDrawable() deprecated API 22)。以下是一种动态设置图像资源的新方法:

String resourceId = "@drawable/myResourceName"; // where myResourceName is the name of your resource file, minus the file extension
int imageResource = getResources().getIdentifier(resourceId, null, getPackageName());
Drawable drawable = ContextCompat.getDrawable(this, imageResource); // For API 21+, gets a drawable styled for theme of passed Context
imageview = (ImageView) findViewById(R.id.imageView);
imageview.setImageDrawable(drawable);

答案 11 :(得分:2)

首先,您的图片名称为 myimage 。所以你需要做的是转到Drawable并保存图像名称 myimage

现在假设您只知道图像名称,并且需要访问它。使用以下代码段来访问它,

你所做的是正确的,确保你保存了你将在编码中使用的图像名称。

public static int getResourceId(Context context, String name, String resourceType) {
    return context.getResources().getIdentifier(toResourceString(name), resourceType, context.getPackageName());
}

private static String toResourceString(String name) {
    return name.replace("(", "")
               .replace(")", "")
               .replace(" ", "_")
               .replace("-", "_")
               .replace("'", "")
               .replace("&", "")
               .toLowerCase();
}

除此之外,您应该确保没有空格和区分大小写

答案 12 :(得分:0)

不推荐使用getDrawable()。 现在你可以使用

imageView.setImageDrawable(ContextCompat.getDrawable(this,R.drawable.msg_status_client_read)) 

答案 13 :(得分:0)

这对我来说很好。

final R.drawable drawableResources = new R.drawable(); 
final Class<R.drawable> c = R.drawable.class;
final Field[] fields = c.getDeclaredFields();
for (int i=0; i < fields.length;i++) 
{
     resourceId[i] = fields[i].getInt(drawableResources);  
    /* till here you get all the images into the int array resourceId.*/
}
imageview= (ImageView)findViewById(R.id.imageView);
if(your condition)
{
   imageview.setImageResource(resourceId[any]);
}

答案 14 :(得分:0)

int[] phptr=new int[5];

将图像指针添加到数组

for(int lv=0;lv<3;lv++)
    {
        String id="a"+String.valueOf(lv+1);
        phptr[lv]= getResources().getIdentifier(id, "drawable", getPackageName());
    }

for(int loopvariable=0;loopvariable<3;loopvariable++) 
    {
        et[loopvariable] = findViewById(p[loopvariable]);
    }



for(int lv=0;lv<3;lv++)
    {
        et[k].setImageResource(phptr[k]);
    }

答案 15 :(得分:0)

ImageView imageView=findViewById(R.id.imageView)

如果可绘制文件夹中有图像,请使用

imageView.setImageResource(R.drawable.imageView)

如果您有uri并想在imageView中显示它,请使用

imageView.setImageUri("uri")

如果您有位图并想在imageView中显示它,请使用

imageView.setImageBitmap(bitmap)

注意:-1. imageView.setImageDrawable()现在在Java中已被弃用 2.如果图像uri来自Firebase或任何其他在线链接,请使用

    Picasso.get()
    .load("uri")
    .into(imageView)

https://github.com/square/picasso

或使用

Glide.with(context)
            .load("uri")
            .into(imageView)

https://github.com/bumptech/glide

答案 16 :(得分:0)

以下是在 Kotlin 中如何在循环 Lib.csproj 中执行此操作:

rfsbraz 的答案中重用的代码

i