Wear OS基础介绍及基本创建Wear OS应用的方法


前言

2021年作者开始学习Android开发,一直以来对于谷歌手表及其他可穿戴只能设备非常热衷,偶然接触到一款作用于可穿戴设备上的全新操作系统,本着对于其技术水平的好奇,作者会带大家一起来认识与学习Wear OS。

一、Wear OS是什么?

在开发移动应用程序时,我们通常只关注两种类型的设备:平板电脑和智能手机。每次我们开始新的 Android Studio 项目时,当我们遇到全新的其他设备时,我们通常都会去寻找它已经存在的模板,在此模板的基础上进行代码构建。

既然如此,那你有没有考虑过为可穿戴设备构建一款属于它们的应用程序?

Wear OS(前称为Android Wear、Wear OS ByGoogle),是Android操作系统的一个分支版本,专为智能手表等可穿戴式电脑设备所设计,由Google主导开发。初始原型于2014年3月19日公布。

智能手表在设计和交互性方面与智能手机不同,因为不同的使用环境和较小的屏幕。布局更简单,更依赖于滑动操作来操作。

在本文中,我将教大家如何为 Wear OS 可穿戴设备创建简易的应用程序。

二、准备阶段

1. 你需要的

要读懂此教程,你至少应该具备:

  • 电脑已经安装Android Studio并为其成功配备环境。
  • Kotlin 编程语言的基础知识。

2. Wear OS与Android相比如何?

Wear OS是专为可穿戴设备设计的新平台。尽管它以Android为基础,但它是一种全新的模式并拥有一组独特的功能。

如果你已经比较熟悉Android移动应用程序开发,Wear OS也应该有迹可循。

3. 专用于 Wear OS 的软件包

  • android.webkit -它是一个开源的网页渲染引擎,现在已经成为PC浏览器的主流。使用KDE桌面环境的KHTML和KJS模块中的代码构建。
  • android.print - 包括用于在 Android 应用中实现打印功能的类。与打印相关的其他更专业的软件包也利用了这些基本类。
  • android.app.backup - 包括备份和恢复功能。因此,当重新安装启用了备份的应用时,可以还原旧的用户数据。
  • android.appwidget - 包含开发应用程序小部件所需的工具,这些小部件允许用户访问应用程序数据和服务,而无需创建解决方案本身。
  • android.hardware.usb - 允许基于 Android 的设备与 USB 外围设备进行通信。

三、开发

1. 在 Android Studio 上创建 Wear OS 项目

建立 Android Wear 应用时,请根据项目要求选择"Wear OS"选项卡、空的活动或任何其他可用选项。

你的应用程序包中会出现两个模块:用于智能手表的佩戴模块,以及用于平板电脑和手机的应用程序模块。

如果你希望将智能手表功能添加到现有应用程序中,打开它,然后从菜单中选择 Wear OS 模块,进行设置。之后会出现一个文件夹,其中包含所需模块的文件名。File -> New -> New Module

从两个模块创建两个不同的文件。但是,它们必须具有相同的包名称,并在发布时使用相同的认证进行验证。.apk.aab

这一点很重要,因为 Google 服务允许应用相互通信。当然你也可以在不使用智能手机平台的情况下制作Wear OS应用程序。

2.1 创建用户界面布局

第一步是为应用程序设计布局。

布局是对应用程序用户界面的直观描述。它是向用户显示的应用的图形插图。

Android应用程序开发中的主要问题之一是设备种类繁多。它们拥有各种不同的尺寸、形状和配置。即使是Wear OS,也有各种屏幕样式需要考虑,包括圆形,方形和带有切碎边缘的圆形。

Wear OS和Android OS具有相似的UI模式。所以你可以使用Google的Wear UI工具包,该工具包具有满足智能手表样式要求所需的一套全面功能。

如此,实施过程简化了,开发资源也减少了。

为此,我们应在应用级文件中包含以下依赖项。build.gradle

dependencies {
    // 对于Wear UI工具包
    implementation 'com.google.android.support:wearable:2.5.0'
    implementation 'com.android.support:percent:28.0.0'
    implementation 'com.android.support:wear:28.0.0'
    implementation 'androidx.wear:wear:1.0.0'
    compileOnly 'com.google.android.wearable:wearable:2.7.0'

    // 对于谷歌服务
    implementation 'com.google.android.gms:play-services-wearable:16.0.1'
    implementation 'com.android.support:support-v4:28.0.0'
}

2.2 设置清单

Wear OS Apps的清单与Android智能手机的清单略有不同。

在Wear OS中,必须定义它的功能与操作系统库以及其他元数据。

<manifest xmlns:android="https://schemas.android.com/apk/res/android"
    package="com.demo.wear">

    <uses-feature android:name="android.hardware.type.watch" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.DeviceDefault">

        <uses-library
            android:name="com.google.android.wearable"
            android:required="true" />

        <!-- If your software is Standalone, it means it doesn't need to be run on a mobile device. -->

        <meta-data
            android:name="com.google.android.wearable.standalone"
            android:value="true" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

3. 最常见的 Wear OS UI 组件

工具包中有四个主要的 UI 元件值得特别注意,因为它们在 Wear OS 开发中很有用:

  • WearableLinearLayoutManager -允许你使用手表的机械轮滚动浏览列表。此外,它将沿屏幕边框定位的对象移动到中心,这在使用圆形界面时非常有用。
  • BoxInsetLayout - 是一款AndroidFrameLayout,能够使子项目适应圆形显示器。它将它们定位在由屏幕圆圈定义的矩形区域中。对于方形显示,在系统级别中通常会忽略此类转换。因此,所有监视界面的布局都是基本相似的。
  • WearableRecyclerView -RecyclerView是一种有用的工具,广泛用于移动应用程序,但在Wear OS中,它是为手表量身定制的。由于显示器的弯曲边缘,顶部和底部视图可能会沿边缘截断。此模式就是用于解决此类问题的。
  • EdgeItemsCenteringEnabled -是一种设置,允许你为滚动项目创建弯曲布局并改进核心元素,使其更容易在相对较小的屏幕上查看。
  • SwipeDismissFrameLayout -是另一种流行的布局,可以从左向右滑动。

4. 使用"BoxInsetLayout"

<androidx.wear.widget.BoxInsetLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:padding="8dp"
    tools:deviceIds="wear"
    tools:context=".MainActivity">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="4dp"
        app:layout_boxedEdges="all">

        <ImageButton
            android:background="@android:color/transparent"
            android:layout_height="60dp"
            android:layout_width="60dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            android:src="@drawable/ic_ok" />

        <TextView
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:text="some text"
            android:textAlignment="center"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <!--In this example, this ImageButton requires an icon named ic_cancel-->
        <ImageButton
            android:background="@android:color/transparent"
            android:layout_height="60dp"
            android:layout_width="60dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            android:src="@drawable/ic_cancel" />
    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.wear.widget.BoxInsetLayout>

结果:

基本效果

5. 使用"SwipeDismissFrameLayout"

<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:app="https://schemas.android.com/apk/res-auto"
    xmlns:tools="https://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp"
    tools:context=".MainActivity"
    tools:deviceIds="wear">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="8dp"
        app:boxedEdges="none">

        <android.support.wear.widget.SwipeDismissFrameLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/swipe_dismiss_root" >

            <TextView
                android:id="@+id/test_content"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:gravity="bottom"
                android:text="Swipe to dismiss"/>
        </android.support.wear.widget.SwipeDismissFrameLayout>

        <TextView
            android:id="@+id/text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="Hello world" />

    </FrameLayout>
</android.support.wear.widget.BoxInsetLayout>

结果:

基本效果

6. 导航

导航是指允许用户在应用程序中的不同屏幕或目标中实现进入和返回的交互界面。

6.1 使用导航抽屉

手表应用程序通常没有设置标题栏,来节省大量的显示空间。使用 ViewPager 时,不会显示 TabLayout,因此,我们可能无法确定我们所在的页面。

让我们来看看导航栏的效果,它可以解决这个问题。WearableNavigationDrawerView

当你从屏幕顶部向下滑动时,它将显示一个导航栏,显示当前页面的图标和标题。如果有多个页面,请左右滑动页面以进行更改。

让我们看看它是如何使用的。我们更改布局文件并创建根布局,并添加导航栏控件。WearableDrawerLayout

<android.support.wear.widget.drawer.WearableNavigationDrawerView
        android:id="@+id/navigation_drawer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:navigationStyle="multiPage"/>

设置导航抽屉的适配器,以及每个页面的图标和标题,如下所示:

class DrawerAdapter(context: Context) : WearableNavigationDrawerView.WearableNavigationDrawerAdapter() {

    private val context = context

    override fun getItemText(pos: Int): CharSequence {
        return when (pos) {
            0 -> "First page"
            else -> "The second page"
        }
    }

    override fun getItemDrawable(pos: Int): Drawable {
        // 创建两个图标并命名它们
        return when (pos) {
            0 -> ContextCompat.getDrawable(context, R.drawable.icon_one)!!
            else -> ContextCompat.getDrawable(context, R.drawable.icon_two)!!
        }
    }

    override fun getCount(): Int {
        return 2
    }

}

在*.MainActivity*中设置抽屉。

navigation_drawer.apply{
    setAdapter(DrawerAdapter(this))
    controller.peekDrawer()
    addOnItemSelectedListener { pos ->
    // 切换页面
    }
}

结果:

基本效果

四、结束

在此教程中,我们学习了如何使用 ConstraintLayout、SwipeDismissFrameLayout 和 WearableNavigationDrawerView 创建基于 Android 的 Wear OS 应用程序。

但这只是基本的构建,希望大家能在这个教程的基础上能加入自己的想象,从而完成一个功能更全面更加优化的Wear OS app。

原文链接

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Wear OS是由Google开发的智能手表操作系统。它提供了许多功能和应用程序,让用户能够更方便地使用他们的智能手表。 首先,要使用Wear OS,您需要将其安装在您的智能手表上。许多智能手表默认都预装了此操作系统,但如果没有,您可以从Google Play商店中下载并安装它。 安装完成后,您需要将手表与您的手机进行配对。打开手表上的蓝牙设置,并在您的手机上搜索新设备。找到您的手表后,将其与手机进行配对。一旦配对成功,您的手表就可以与手机同步,并开始接收通知、查看日历、控制音乐等功能。 接下来,您可以开始个性化设置您的手表。通过访问手表的设置菜单,您可以更改手表的表盘、背景、字体大小等。您还可以调整手表的通知设置,选择您想要在手表上显示的通知类型。 Wear OS还支持许多应用程序,包括Google Fit健身追踪、Google Maps导航、Google Pay支付等。您可以在手表上打开Google Play商店,浏览并安装您感兴趣的应用程序。 最后,Wear OS还有一些手势和快捷方式,可以帮助您更方便地操作手表。例如,您可以通过在手表面板上向左滑动来访问快速设置,如亮度调整、飞行模式等。通过向上滑动,您可以查看手表的通知历史记录。 总体而言,Wear OS是一个功能强大的智能手表操作系统,提供了许多定制选项和方便功能。通过理解操作系统的不同功能和设置,您可以最大限度地发挥手表的潜力,并让它更好地满足您的需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值