在imageView中设置图像

时间:2015-07-19 08:40:39

标签: android imageview

我是Android的新手,我正在尝试制作Mancala(这是一款非洲游戏)应用。我正在寻找一种方法来将图像视图从一个图像更新为另一个图像(对于那些熟悉游戏的人 - 我希望每次玩家玩时,空洞的图像都会更新为带有球的图像)。

我试图寻找答案,但我找不到答案。这是我尝试过的:

imgHome2=(ImageView)findViewById(R.drawable.home0);

2 个答案:

答案 0 :(得分:1)

您可以使用 Drawable

执行此操作
private ImageView imageView;

onCreate()

imageView = (ImageView) findViewById(imageView);

在ImageView上创建set drawable的新方法。

private void setDrawableOnImageView(Drawable drawable){
    imageView.setImageDrawable(drawable);
}

如何调用上述方法:

Drawable drawable = getResources().getDrawable(R.drawable.ic_launcher);    
setDrawableOnImageView(drawable);

您可以根据需要更改drawable。

答案 1 :(得分:0)

在findViewById中你应该传递一个id而不是一个可绘制的

 imgHome2=(ImageView)findViewById(R.id.myimageviewid)

并且您可以通过

更改图像
 imgHome2.setBackground(R.drawable.home0);
相关问题