android如何使用textureVide显示本地图片
时间: 2023-11-27 08:51:46 浏览: 157
要在 Android 中使用 TextureView 显示本地图片,可以按照以下步骤操作:
1. 将图片转换为 Bitmap 对象。
```java
Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/image.jpg");
```
2. 创建一个 SurfaceTexture 对象,并将其与 TextureView 关联。
```java
TextureView textureView = findViewById(R.id.texture_view);
SurfaceTexture surfaceTexture = textureView.getSurfaceTexture();
```
3. 将 Bitmap 对象绑定到 SurfaceTexture 上,并设置纹理大小。
```java
surfaceTexture.setDefaultBufferSize(bitmap.getWidth(), bitmap.getHeight());
Surface surface = new Surface(surfaceTexture);
Canvas canvas = surface.lockCanvas(null);
canvas.drawBitmap(bitmap, 0, 0, null);
surface.unlockCanvasAndPost(canvas);
```
4. 将 Surface 对象设置给 MediaPlayer 或其他播放器。
```java
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setSurface(surface);
mediaPlayer.setDataSource("/sdcard/video.mp4");
mediaPlayer.prepare();
mediaPlayer.start();
```
这样就可以在 TextureView 上显示本地图片了。
阅读全文
相关推荐


















