Android经典实战之TextureView原理和高级用法

本文首发于公众号“AntDream”,欢迎微信搜索“AntDream”或扫描文章底部二维码关注,和我一起每天进步一点点

1. TextureView 的原理

TextureView 是一个继承自 View 的类,其主要优势在于能够直接在硬件加速层进行渲染。它允许应用将内容绘制到一个 SurfaceTexture,并能够将这个 SurfaceTexture 的内容呈现在其视图层级中。与 SurfaceView 不同,TextureView 支持复杂的视图层次并且可以与其他视图时序混用。这意味着,TextureView 能真正像普通的 View 一样参与到视图的动画和变换中。

2. TextureView 的特点
  • 灵活性高:可以与其他 View 叠加使用,非常适合在复杂的视图层次结构中使用。
  • 硬件加速支持:由于它在硬件加速层进行渲染,其性能也较优。
  • 支持绘制操作:可以从其他线程更新内容,适合用于播放视频、显示实时特效等。
3. 具体的使用举例

下面是 TextureView 的基本使用示例,包括如何使用 SurfaceTextureListener 接口来管理其生命周期事件。

import android.graphics.SurfaceTexture
import android.media.MediaPlayer
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.view.TextureView
import android.view.TextureView.SurfaceTextureListener

class MainActivity : AppCompatActivity() {

    private lateinit var textureView: TextureView
    private var mediaPlayer: MediaPlayer? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        textureView = findViewById(R.id.textureView)
        textureView.surfaceTextureListener = object : SurfaceTextureListener {
            override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
                initializeMediaPlayer(surface)
            }

            override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {
                // Handle size change if needed
            }

            override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
                mediaPlayer?.release()
                return true
            }

            override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {
                // Update as needed
            }
        }
    }

    private fun initializeMediaPlayer(surface: SurfaceTexture) {
        mediaPlayer = MediaPlayer().apply {
            setDataSource("path/to/your/video/file") // Replace with actual video path
            setSurface(android.view.Surface(surface))
            prepare()
            start()
        }
    }
}

在上述示例中,我们在 Activity 中初始化了一个 TextureView,并通过 SurfaceTextureListener 管理其生命周期。当 SurfaceTexture 可用时,我们初始化并启动了一个 MediaPlayer,将其内容渲染到 TextureView 上。

4. 注意事项
  • SurfaceTexture的销毁与回收:在 onSurfaceTextureDestroyed 方法中应正确释放相关资源,以避免内存泄露。
  • 性能考虑:尽管 TextureView 功能强大,但由于其工作于 View 层次中,在复杂操作下可能导致性能瓶颈。
  • 清晰度与分辨率TextureView 的内容分辨率及其与设备屏幕的适配需特别注意,特别是在播放高清视频时。
  • 测试设备和环境:在不同设备上测试,确保不同分辨率和硬件规格下的兼容性,尤其是在处理高帧率视频和动画时。
5. 高级使用

TextureView 还支持 Canvas 绘制,可以用于实现自定义绘制的效果。例如:

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.TextureView

class CustomTextureView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : TextureView(context, attrs, defStyleAttr), TextureView.SurfaceTextureListener {

    private val paint = Paint().apply {
        color = Color.RED
        style = Paint.Style.FILL
    }

    init {
        surfaceTextureListener = this
    }

    override fun onSurfaceTextureAvailable(surface: SurfaceTexture, width: Int, height: Int) {
        // Custom drawing can start here
        val canvas = lockCanvas()
        drawCustomContent(canvas)
        unlockCanvasAndPost(canvas)
    }

    override fun onSurfaceTextureSizeChanged(surface: SurfaceTexture, width: Int, height: Int) {}

    override fun onSurfaceTextureDestroyed(surface: SurfaceTexture): Boolean {
        return true
    }

    override fun onSurfaceTextureUpdated(surface: SurfaceTexture) {}

    private fun drawCustomContent(canvas: Canvas?) {
        canvas?.drawCircle(width / 2f, height / 2f, 100f, paint)
    }
}

这个自定义的 TextureView 在可用时会绘制一个红色的圆形,可以进一步使用 CanvasTextureView 的内容上进行更多自定义绘制。

总的来说,TextureView 提供了一个功能强大且灵活的方式来处理复杂的图形和视频渲染任务,适合在需要与其他视图紧密集成的多种场景下使用。通过合理管理它的生命周期和资源,能够实现高效、流畅的用户体验。


欢迎关注我的公众号AntDream查看更多精彩文章!

AntDream

要将 TextureView 全屏,可以按照以下步骤进行操作: 1. 将 TextureView 添加到布局文件中,并设置其宽高。 ``` <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextureView android:id="@+id/texture_view" android:layout_width="match_parent" android:layout_height="match_parent" /> </FrameLayout> ``` 2. 在 Activity 的 onCreate 方法中获取 TextureView,并设置其监听器。 ``` private TextureView textureView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textureView = findViewById(R.id.texture_view); textureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() { @Override public void onSurfaceTextureAvailable(SurfaceTexture surfaceTexture, int width, int height) { // TextureView 可用时,将其设置为全屏 textureView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); } @Override public void onSurfaceTextureSizeChanged(SurfaceTexture surfaceTexture, int width, int height) { // TextureView 大小变化时,重新设置布局参数 textureView.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); } @Override public boolean onSurfaceTextureDestroyed(SurfaceTexture surfaceTexture) { return true; } @Override public void onSurfaceTextureUpdated(SurfaceTexture surfaceTexture) { } }); } ``` 在 TextureView 的监听器中,通过设置布局参数,将 TextureView 设置为全屏。当 TextureView 的大小发生变化时,也需要重新设置布局参数。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值