2017-02-25 119 views
0

我想从我的代码(而不是xml)更改ImageView源代码。根据我从其他活动收到的信息,我有一个活动,我想改变它中的图像。有没有什么方法可以从类代码中更改图像源而不是xml代码?图像源更改

例如:

if (message.equals("hello")){ 
     //change the ImageView to hello.png; 
    } 
+1

([ImageView的程序的Android的变化图]的可能的复制http://stackoverflow.com/questions/16906528/change-image -of-imageview-programmingmatically-android) –

+1

你的意思是'ImageView.setBackgroundResource(R.drawable.hello);'你可以在drawable文件夹中找到名为hello.png的图像^ _^ –

+0

它将新图像设置为只在当前图片,我希望它把图像更改为新图像 –

回答

0

您可以按照下面的步骤做:

  1. 保存与消息名称的图像(即hello.png等)。

  2. 现在在你的if条件下使用下面的代码来设置图像。更改mipmapdrawable如果你已经保存在文件夹绘制图像

    int imageId=context.getResources().getIdentifier(message,"mipmap",context.getPackageName()); 
    imageView.setImageResource(imageId); 
    
+0

我不明白我应该在消息 –

+0

消息中写什么消息将成为你的消息你得到的前例。 “你好”,同样使用这种方法你可以摆脱条件陈述 – Sanjeet

+0

你能给我一个如何写它的例子吗? String message =? –