跳转intent改变图片ImageView
时间: 2023-05-31 21:02:22 浏览: 53
要改变ImageView的图片,您可以使用以下代码:
```java
ImageView imageView = findViewById(R.id.my_image_view);
imageView.setImageResource(R.drawable.my_image);
```
这将使用名为`my_image`的图像更改`my_image_view` ImageView的图像。
如果您想在单击按钮时更改图像,则可以使用以下代码:
```java
Button button = findViewById(R.id.my_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
ImageView imageView = findViewById(R.id.my_image_view);
imageView.setImageResource(R.drawable.new_image);
}
});
```
这将在单击`my_button`按钮时将ImageView的图像更改为`new_image`。
如果您要在跳转Intent时更改图像,您可以在onCreate方法中添加以下代码:
```java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.my_image_view);
Intent intent = getIntent();
int imageRes = intent.getIntExtra("image_res", R.drawable.default_image);
imageView.setImageResource(imageRes);
}
```
在这个例子中,我们假设您的默认图像是`default_image`,并且您在Intent中使用了一个名为`image_res`的额外参数来传递图像资源ID。 您可以根据需要更改这些值。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)