Jetpack学习笔录-dataBinding

dataBinding

数据的双向绑定,使用dataBinding,在xml布局文件中·对指定的布局内容设置,当数据内容发生改变,自动更新到xml上,这配合LiveData使用。另外可以编写顶层函数,顶部增加@BindAdapter注解。

/**
 * A Binding Adapter that is called whenever the value of the attribute `android:progressTint`
 * changes. Depending on the value it determines the color of the progress bar.
 */
@BindingAdapter("app:progressTint")
fun tintPopularity(view: ProgressBar, popularity: Popularity) {

    val color = getAssociatedColor(popularity, view.context)

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.progressTintList = ColorStateList.valueOf(color)
    }
}

比如上面这样,表示监听progressBar,当progressBar设置了app:progressTint属性时启动,会根据popularity的内容改变时发生调用。这就实现了用户点击的响应。

代码实验室地址:https://developer.android.com/codelabs/android-databinding#0

大致步骤:

1、在build.gradle文件夹中启动databinding

android {
...
    dataBinding {
       enabled true
    }
}

2、将布局文件转换为layout做为根节点。同时在data节点中增加相关变量。

<layout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools">

    <data>

        <variable
            name="viewmodel"
            type="com.example.android.databinding.basicsample.data.SimpleViewModelSolution"/>
    </data>

    ....

</layout>

3、然后就可以对具体的控件设置内容,比如

android:text="@{String.valueOf(index + 1)}"
android:visibility="@{age < 13 ? View.GONE : View.VISIBLE}"
android:transitionName='@{"image_" + id}'


// Bind the name property of the viewmodel to the text attribute
android:text="@{viewmodel.name}"
// Bind the nameVisible property of the viewmodel to the visibility attribute
android:visibility="@{viewmodel.nameVisible}"
// Call the onLike() method on the viewmodel when the View is clicked.
android:onClick="@{() -> viewmodel.onLike()}"


//当然也可以自定义一些简单的值
 <data>
        <variable name="name" type="String"/>
        <variable name="lastName" type="String"/>
  </data>

 <TextView
                android:id="@+id/plain_name"
                android:text="@{name}" 
        ... />

4、修改完xml布局布局文件,现在开始修改kt文件。原先的setContentView方法换一种方式

val binding : PlainActivityBinding =
    DataBindingUtil.setContentView(this, R.layout.plain_activity)

 结合上面那段代码,利用获取到的binding对象设置在xml文件中申明的那边变量、对象的具体值

binding.viewModel = viewModel
//下面这个是因为viewmodel中使用了liveData,liveData是可观察的数据,会在对应的页面生命周期可见的情况下更新视图,这就需要明确的指定到底是谁的生命周期了
binding.lifecycleOwner = this

到这里初步的数据自动更新到ui上就ok了。

下面开始设置绑定适配器

//单个属性
@BindingAdapter("app:hideIfZero")
fun hideIfZero(view: View, number: Int) {
    view.visibility = if (number == 0) View.GONE else View.VISIBLE
}



//使用时就是下面这样
<ProgressBar
      android:id="@+id/progressBar"
      app:hideIfZero="@{viewmodel.likes}"
...

这个绑定适配器:应用于app:hideIfZero属性。可以应用到每个视图(因为第一个参数是一个视图;您可以通过更改此类型来限制为某些类)接受一个整数,该整数应该是布局表达式返回的结果。如果数字为0,则视图消失。否则可见。

 

多个属性

@BindingAdapter(value = ["app:progressScaled", "android:max"], requireAll = true)
fun setProgress(progressBar: ProgressBar, likes: Int, max: Int) {
    progressBar.progress = (likes * max / 5).coerceAtMost(max)
}

如果缺少任何属性,则不使用此绑定适配器。这发生在编译时。该方法现在接受3个参数(它所应用的视图加上注释中定义的属性数量)。requireAll参数定义了何时使用绑定适配器:当为true时,所有元素都必须出现在XML定义中。如果为false,则缺失的属性将为null,如果为布尔值则为false,如果为原语则为0。

 

### 如何在 Jetpack 4.4 上安装和配置 Python OpenCV #### 下载并安装必要的依赖项 为了确保系统的稳定性和兼容性,在安装特定版本的 OpenCV 前,建议先移除任何旧版可能存在的冲突库。对于已经存在多个不同版本的情况,可以考虑创建虚拟环境来隔离各个项目所需的软件栈[^2]。 ```bash sudo apt-get update && sudo apt-get upgrade -y sudo apt-get remove --purge libopencv* python-opencv -y ``` #### 创建 Python 虚拟环境 通过 `virtualenv` 或者 Anaconda 来管理独立的工作空间能够有效避免包之间的相互干扰: ```bash pip install virtualenv virtualenv opencv_env source opencv_env/bin/activate ``` #### 编译安装指定版本的 OpenCV 考虑到系统中已有的 CUDA 和 TensorRT 配置,编译时应启用 GPU 加速支持以充分利用硬件资源[^3]。针对 Jetson Nano 平台上的 JetPack 4.4 版本,推荐采用预构建二进制文件简化流程;然而若需自定义功能,则可按照如下步骤操作: 1. **获取源码** ```bash git clone https://github.com/opencv/opencv.git -b 3.4.6 cd opencv/ ``` 2. **设置 CMake 参数** 确认路径指向正确的开发工具链位置,并开启 GStreamer 支持以便于多媒体处理应用开发[^4]。 ```cmake cmake \ -D CMAKE_BUILD_TYPE=RELEASE \ -D CMAKE_INSTALL_PREFIX=/usr/local \ -D CUDA_ARCH_BIN="5.3" \ -D CUDA_ARCH_PTX="" \ -D OPENCV_EXTRA_MODULES_PATH=../../opencv_contrib/modules \ -D BUILD_EXAMPLES=OFF .. ``` 3. **执行多线程编译过程** 利用 `-j$(nproc)` 自动调整并发任务数加快速度。 ```bash make -j$(nproc) sudo make install ``` 完成上述步骤之后,重启终端使新加载的共享库生效,接着就可以利用 pip 工具快速部署对应的 Python 接口模块了。 ```bash deactivate cd ~ git clone https://github.com/skvark/opencv-python.git cd opencv-python python setup.py build_ext --inplace pip install . ``` 最后一步是验证安装成果,运行简单的测试脚本来确认一切正常工作。 ```python import cv2 print(cv2.__version__) cap = cv2.VideoCapture(0) while True: ret, frame = cap.read() if not ret: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.imshow('frame',gray) if cv2.waitKey(1) & 0xFF == ord('q'): break cap.release() cv2.destroyAllWindows() ```
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值