在 Android 中显示图片有多种方式,主要包括以下几种:
1. 在 XML 布局文件中使用 `ImageView` 控件显示图片:
```xml
android:id="@+id/my_image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/my_image" /> ``` 其中 `@drawable/my_image` 是图片资源在 `res/drawable` 目录下的文件名。 2. 在代码中使用 `ImageView` 控件显示图片: ```kotlin val imageView = findViewById imageView.setImageResource(R.drawable.my_image) ``` 3. 使用 `Bitmap` 对象显示图片: ```kotlin val bitmap = BitmapFactory.decodeResource(resources, R.drawable.my_image) imageView.setImageBitmap(bitmap) ``` 4. 从网络加载图片并显示: ```kotlin Glide.with(this) .load("https://example.com/image.jpg") .into(imageView) ``` 这里使用了第三方库 Glide 来加载和显示网络图片。 5. 从 File 或 InputStream 显示图片: ```kotlin val file = File("path/to/image.jpg") val bitmap = BitmapFactory.decodeFile(file.path) imageView.setImageBitmap(bitmap) ``` 通过以上几种方式,你可以在 Android 应用中方便地显示各种图片资源。具体选择哪种方式取决于图片的来源和使用场景。