Android kotlin学习笔记(三)

今天继续学习。之前已经学了怎么把萤石云播放视频整合到kotlin代码里,接下来我们将对云台控制功能进行集成。

首先,在还没有操作萤石云SDK前,我们先将touch监听给弄好。

<SurfaceView
        android:id="@+id/remoteplayback_sv"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintDimensionRatio="H,16:9"
        app:layout_constraintBottom_toTopOf="@+id/button_up"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <Button
        android:id="@+id/button_up"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/move_up"
        app:layout_constraintTop_toBottomOf="@+id/remoteplayback_sv"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/button_left"/>
    <Button
        android:id="@+id/button_left"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/move_left"
        app:layout_constraintTop_toBottomOf="@+id/button_up"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button_right"
        app:layout_constraintBottom_toTopOf="@+id/button_down"/>
    <Button
        android:id="@+id/button_right"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/move_right"
        app:layout_constraintTop_toBottomOf="@+id/button_up"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/button_left"
        app:layout_constraintBottom_toTopOf="@+id/button_down"/>
    <Button
        android:id="@+id/button_down"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/move_down"
        app:layout_constraintTop_toBottomOf="@+id/button_left"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"/>

首先先在xml里添加上下左右四个按钮

然后在activity.kt里,用上touch接口

class MainActivity : AppCompatActivity() , SurfaceHolder.Callback,View.OnTouchListener

这是就会需要实onTouch方法。

override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_DOWN ->{
                when(v?.id){
                    R.id.button_up -> {
                        Log.d("ssssss","up+")
                    }
                    R.id.button_left -> {
                        Log.d("ssssss","left+")
                    }
                    R.id.button_right -> {
                        Log.d("ssssss","right+")
                    }
                    R.id.button_down -> {
                        Log.d("ssssss","down+")
                    }
                }
            }
            MotionEvent.ACTION_UP ->{
                when(v?.id){
                    R.id.button_up -> {
                        Log.d("ssssss","up-")
                    }
                    R.id.button_left -> {
                        Log.d("ssssss","left-")
                    }
                    R.id.button_right -> {
                        Log.d("ssssss","right-")
                    }
                    R.id.button_down -> {
                        Log.d("ssssss","down-")
                    }
                }
            }
        }
        return true
    }

接着,声明四个按钮并赋值,同时添加设置touch监听

    private lateinit var move_up:Button;
    private lateinit var move_left:Button
    private lateinit var move_right:Button
    private lateinit var move_down:Button



    
        move_up = findViewById(R.id.button_up) as Button
        move_left = findViewById(R.id.button_left) as Button
        move_right = findViewById(R.id.button_right) as Button
        move_down = findViewById(R.id.button_down) as Button

        move_up.setOnTouchListener( this)
        move_left.setOnTouchListener( this)
        move_right.setOnTouchListener( this)
        move_down.setOnTouchListener( this)

这时候,我们按下三四个按钮或者松开,都会有对应的响应了。

然后我们新建一个方法:

    private fun ptzOption(command: EZConstants.EZPTZCommand, action: EZConstants.EZPTZAction) {
        Thread(Runnable {
            var ptz_result = false
            try {
                ptz_result = EZOpenSDK.getInstance().controlPTZ(
                    cameraInfo00?.getDeviceSerial(), cameraInfo00?.getCameraNo()!!, command,
                    action, EZConstants.PTZ_SPEED_DEFAULT
                )
            } catch (e: BaseException) {
                e.printStackTrace()
            }
            LogUtil.i(
                MainActivity.TAG,
                "controlPTZ ptzCtrl result: $ptz_result"
            )
        }).start()
    }

我们可以看到,跟java不同的是,线程里的override不用再写了。

这里有个问题,因为cameraInfo00是有可能为空的,所以要在后面加上!!,这个的意思是如果非空就进行,为空就抛出异常。

然后将这个方法写入之前的touch监听里,就完成了。

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值