在这一系列技巧中,我们一直在仔细研究今年Google I / O宣布的一些新的Android功能和工具。
在本文中,我们将重点介绍Android Wear。
Google已经通过专用的Wearable支持库提供Android Wear UI组件已有一段时间了,但这一切都将改变!
在今年的活动中,Google宣布构成可穿戴支持库的各种组件将被弃用,合并或迁移到Android支持库中。 在本文中,我们将研究将要合并,移动和删除的组件,以及今天如何开始使用Android支持库的新Wear模块。
我们还将研究一些新工具,这些工具旨在简化与Android Wear的Complications API的配合使用。
新的Android Wear用户界面库
在今年的Google I / O上,Android Wear团队宣布,大部分可穿戴支持库都将移至Android支持库。 与Wear相关的组件将构成新的支持穿戴模块的基础,类似于Android支持库中的其他模块,例如support-recylerview
和support-design
。
根据Google I / O上的Android Wear会议,我们可以期望这个新的Wear模块将在Android O正式发布的同时退出beta版。
但是,并非来自可穿戴支持库的所有组件都将转移到Android支持库。 Google还宣布,可穿戴支持库中的某些组件将是:
合并。 适用于可穿戴设备和手持设备的组件将合并到Android框架或更通用的支持模块中。 组件是由于合并包括
CircledImageView
,DelayedConfirmationView
和ActionButton
。不推荐使用。 Google将会淘汰与设计模式相关联的Android Wear UI组件,而这些设计模式尚未被Android Wear用户所认可。 具体来说,Google将删除允许Android Wear用户水平和垂直移动的二维空间模型,并将其替换为垂直
LinearLayout
。 与二维空间模型关联的所有类都将被弃用,包括GridViewPager
,操作按钮和操作布局。
尽管此迁移是一个持续的过程,但Google已将一些Android Wear组件集成到Android支持库的版本26.0.0 Beta1中。
BoxInsetLayout
:这是一个可BoxInsetLayout
屏幕形状的FrameLayout
,可以帮助您设计适用于方形和圆形表盘的单个布局。 当您的布局显示在圆形屏幕上时,BoxInsetLayout
会将其所有子项装箱到圆形屏幕中央的假想正方形中。 您可以使用layout_box
属性指定如何将UI元素放置在此中心正方形中。 当您的应用显示在方形屏幕上时,Android会忽略layout_box
属性,并使用零值的窗口插入,因此您的视图将被定位为就好像它们位于常规FrameLayout
。SwipeDismissFrameLayout
:这是一个布局,可用于实现View
和片段的自定义交互。 通常,您将使用SwipeDismissFrameLayout
来使用户能够通过在屏幕上滑动来消除视图和片段,实质上是在Android智能手机和平板电脑上复制“ 返回”按钮的功能。WearableRecyclerView
:这是RecyclerView
的特定于可穿戴设备的实现,可帮助您为圆形显示设计更有效的布局。WearableRecyclerView
更有效地利用了圆形屏幕的曲率,通常用于实现弯曲列表。WearableRecyclerView
的setCircularScrollingGestureEnabled()
方法,您还可以选择在应用程序中使用圆形滚动手势。
添加新的Android Wear模块
要开始使用新的Android Wear模块,您需要安装Android支持库26.0.0 Beta1,这将使我们转到另一个Google I / O公告。
在今年的活动中,Google宣布将仅通过Google Maven存储库分发所有即将发布的Android支持库版本(26.0.0 Beta1及更高版本)。
从此存储库下载Android支持库仅需要您将Google Maven存储库添加到build.gradle文件中:
repositories {
maven { url 'https://maven.google.com' }
jcenter()
}
然后,您可以照常设置编译依赖项,因此打开可穿戴模块的build.gradle文件,并将Wear库添加为项目依赖项:
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
//Add the following//
compile 'com.android.support:wear:26.0.0-beta1'
要将Android Wear用户界面库中的组件添加到用户界面中,只需打开布局资源文件,并确保使用新的完全合格的包名称即可。 本质上,这意味着用android.support.wearable.view
替换android.support.wear.widget
。 例如,这里我使用Android支持库中的BoxInsetLayout
类:
<?xml version="1.0" encoding="utf-8"?>
<android.support.wear.widget.BoxInsetLayout
https://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/dark_grey"
android:padding="@dimen/box_inset_layout_padding"
tools:context="com.jessicathornsby.myapplication.MainActivity"
tools:deviceIds="wear">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="@dimen/inner_frame_layout_padding"
app:layout_box="all">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi World" />
</FrameLayout>
</android.support.wear.widget.BoxInsetLayout>
要将此类导入到您的Java文件中,只需使用相同的名称,因此旧的名称:
import android.support.wearable.view.BoxInsetLayout;
成为新的:
import android.support.wear.widget.BoxInsetLayout;
与Complications API轻松集成
Android Wear用户可以从各种样式的表盘中进行选择,尽管Complications API确实可以完全控制表盘绘制数据的方式,但是这种灵活性可能很难为表盘添加复杂性支持。
在今年的Google I / O上,Android Wear团队引入了一些新功能,使使用Complication API更加容易。
复杂性
ComplicationDrawable
是一个新的解决方案,它有望为您处理所有并发症的样式和布局。
如果创建ComplicationDrawable
但未设置任何样式参数,则将获得默认外观,但也可以使用ComplicationDrawable
每个部分的样式,包括背景色,角半径和边框。
如果您的项目以API 24或更高版本为目标,则可以通过在项目的/ res / drawable文件夹中创建专用的布局资源文件来定义ComplicationDrawable
对象。
打开您的XML文件,然后使用以下标记创建一个ComplicationDrawable
:
<android.support.wearable.complications.rendering.ComplicationDrawable
...
...
...
<ambient
//Define the attributes you want to use when the device is in ambient mode//
/>
</android.support.wearable.complications.rendering.ComplicationDrawable>
请注意,除非您在文件的<ambient>
部分中专门覆盖了这些属性,否则在顶层定义的属性适用于标准模式和环境模式。
然后,您需要将并发症数据传递到可绘制对象:
@Override
public void onComplicationDataUpdate(int id, ComplicationData data) {
myComplicationDrawable.setComplicationData(data);
}
最后,通过在drawable上调用setBounds
来绘制复杂的代码:
@Override
public void onDraw(Canvas canvas, Rect bounds) {
if(haveChanged(bounds)) {
myComplicationDrawable.setBounds(
complicationBoundsWithin(bounds));
}
//Call draw on the ComplicationDrawable//
myComplicationDrawable.draw(canvas, currentTimeMillis);
...
...
...
}
TextRenderer
大多数并发症包括某种形式的文本,而TextRenderer
是一个新类, TextRenderer
在画布上绘制复杂文本的方式进行了一些小而有力的调整。
您可以使用TextRenderer
来指定复杂文本必须使用的边界,然后TextRenderer
将调整文本的大小或将其排列在几行上以适合此区域。 此外,当屏幕进入Android Wear的“始终开启”环境模式时, TextRenderer
通过隐藏不适合该模式的字符和样式来调整文本。
要利用这个新类,您需要在初始化表盘时创建一个TextRenderer
,然后将要使用的TextPaint
传递给它,它定义了样式属性,例如字体和文本颜色:
@Override
public void onCreate(SurfaceHolder holder) {
...
...
...
myTextRenderer = new TextRenderer();
myTextRenderer.setPaint(myTextPaint(TextPaint);
您需要为每个字段创建一个TextRenderer
,所以您还需要为标题文本创建一个TextRenderer
:
myTitleRenderer = new TextRenderer();
myTitleRenderer.setPaint(myTitlePaint);
...
...
...
}
在绘制时,您需要通过调用setText
在渲染器上设置文本,然后通过调用getText
检索文本:
public void onDraw(Canvas canvas, Rect bounds) {
...
...
...
myTextRenderer.setText(myComplicationText.getText(
Context, data.getShortText(), currentTimeMillis));
myTextRenderer.draw(canvas, bounds);
...
...
...
}
请注意,许多复杂性与时间有关,这就是为什么上述代码片段中包含currentTimeMillis
原因。
结论
在本文中,我们研究了如何将新的Android Wear UI库添加到您的项目中,以及如何从今天开始使用该库中的许多组件。 我们还检查了两个组件,这些组件有望使与Android Wear的Complications API的集成比以前更加轻松。
在下一期中,我们将通过探索最新的Android Studio Canary版本来预览Android 3.0中的新功能。