User interfaces for wearable apps differ significantly from those built for handheld devices. Apps for wearables should follow the Android Weardesign principles and implement the recommendedUI patterns, which ensure a consistent user experience across apps that is optimized for wearables.
This class teaches you how to create custom UIs for your wearable apps and custom notifications that look good on any Android Wear device by implementing these UI patterns:
The Wearable UI Library, which is part of the Google Repository in the Android SDK, provides classes that help you implement these patterns and create layouts that work on both round and square Android Wear devices.
Note: We recommend using Android Studio for Android Wear development, as it provides project setup, library inclusion, and packaging conveniences. This training assumes you are using Android Studio.
Confirmations in Android Wear apps use the whole screen or a larger portion of it than those in handheld apps. This ensures that users can see these confirmations by just glancing at the screen and that they have large enough touch targets to cancel an action.
<翻译>在android 手表应用的确认页使用整个屏幕或者一个比手机端应用大的份额( portion)。这个保证了用户可以看到确认也通过看手机屏幕,并且用户有足够大的目标去取消一个action。(就像我们说出了语音以后,他会有一个取消的按钮)
The Wearable UI Library helps you show confirmation animations and timers in your Android Wear apps:<翻译>手表UI库帮助你显示确认的动画和倒计时在你的android 手表应用中。
The following sections show you how to implement these patterns.下面的内容展示了如何实现这些模式
Automatic confirmation timers let users cancel an action they just performed. When the user performs the action, your app shows a button to cancel the action with a timer animation and starts the timer. The user has the option to cancel the action until the timer finishes. Your app gets notified if the user cancels the action and when the timer expires.
<翻译>自动确认的倒计时让用户可以取消一个刚才执行的操作。单用户执行了一个操作,你的应用显示了一个有取消操作的按钮,这个按钮还有一个倒计时的动画,然后启动这个倒计时。用户可以选择取消操作的知道倒计时完成之前。你的应用会得到用户取消了操作的通知或者倒计时超时的通知。
To show a confirmation timer when users complete an action in your app:
element to your layout.DelayedConfirmationListener
interface in your activity.Add the
element to your layout as follows:
<翻译>为了当用户在你的应用中完成了操作后显示一个确认的动画:
1:添加一个
项目到你的布局中
2:在你的activity 中实现a
的接口
3:设置倒计时并且在用户完成了操作后开始及时。
DelayedConfirmationView
android:id="@+id/delayed_confirm"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/cancel_circle"
app:circle_border_color="@color/lightblue"
app:circle_border_width="4dp"
app:circle_radius="16dp">
You can assign a drawable resource to display inside the circle with the android:src
attribute and configure the parameters of the circle directly on the layout definition.
To be notified when the timer finishes or when users tap on it, implement the corresponding listener methods in your activity:
<翻译>你可以通过android:src 属性来将一个drawable 的资源分配显示在圆圈中,并且配置圆圈的参数在布局中。
为了在倒计时完成后或者用户取消了之后收到通知,就要实现相应的监听方法。
public class WearActivity extends Activity implements DelayedConfirmationView.DelayedConfirmationListener { private DelayedConfirmationView mDelayedView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_wear_activity); mDelayedView = (DelayedConfirmationView) findViewById(R.id.delayed_confirm); mDelayedView.setListener(this); } @Override public void onTimerFinished(View view) { // User didn't cancel, perform the action } @Override public void onTimerSelected(View view) { // User canceled, abort the action } }
To start the timer, add the following code to the point in your activity where users select an action:
为了启动倒计时,添加下面的代码到你的actiivty的用户选择action的点。
// Two seconds to cancel the action mDelayedView.setTotalTimeMs(2000); // Start the timer mDelayedView.start();
To show a confirmation animation when users complete an action in your app, create an intent that starts ConfirmationActivity
from one of your activities. You can specify one of the these animations with theEXTRA_ANIMATION_TYPE
intent extra:
SUCCESS_ANIMATION
FAILURE_ANIMATION
OPEN_ON_PHONE_ANIMATION
You can also add a message that appears under the confirmation icon.
To use the ConfirmationActivity
in your app, first declare this activity in your manifest file:
<翻译>当用户完成了应用中的一个action的时候,为了显示确认的动画,在你的额activit中创建一个intent去启动 ConfirmationActivity
。
你可以通过EXTRA_ANIMATION_TYPE
intent extra指定下面的动画。
你也可以在确认的icon 下面添加信息。
为了使用ConfirmationActivity 在你的应用中,首先在清单文件中声明这个actiivty
... android:name="android.support.wearable.activity.ConfirmationActivity">
Then determine the result of the user action and start the activity with an intent:
然后确定用户的动作结果并且通过intent来启动activity
Intent intent = new Intent(this, ConfirmationActivity.class); intent.putExtra(ConfirmationActivity.EXTRA_ANIMATION_TYPE, ConfirmationActivity.SUCCESS_ANIMATION); intent.putExtra(ConfirmationActivity.EXTRA_MESSAGE, getString(R.string.msg_sent)); startActivity(intent);
After showing the confirmation animation, ConfirmationActivity
finishes and your activity resumes.
ConfirmationActivity
完成了,并且你的acitivyt 会resume。
By default, users exit Android Wear activities by swiping from left to right. If the app contains horizontally scrollable content, users first have to navigate to the edge of the content and then swipe again from left to right to exit the app.
For more immersive experiences, like an app that can scroll a map in any direction, you can disable the swipe to exit gesture in your app. However, if you disable it, you must implement the long-press-to-dismiss UI pattern to let users exit your app using the DismissOverlayView
class from the Wearable UI Library. You must also inform your users the first time they run your app that they can exit using a long press.
For design guidelines about exiting Android Wear activities, see Breaking out of the card.
<翻译> 默认的情况下,用户推出了android 的手机activity通过从左向右的滑动(swip)。如果应用包含了水平的可滑动的内容,用户首先要滑动到内容的边缘,然后在从左向右滑动。
为了真实的体验,例如一个应用可以在地图上面随便的滑动方向,你要在你的应用中禁用滑动退出的手势。然而,如果你禁用了这个,你必须要实现长按取消的UI 模式,这个模式让用户可以通过手表UI库中的 DismissOverlayView
类来退出你的应用。
关于推出android 手表actiivty的设计的向导,请查看Breaking out of the card.
If the user interaction model of your app interferes with the swipe-to-dismiss gesture, you can disable it for your app. To disable the swipe-to-dismiss gesture in your app, extend the default theme and set theandroid:windowSwipeToDismiss
attribute to false
:
<翻译>如果你应用的用户的交互模式和滑动取消的手势有冲去,你可以在你的app中禁用这个手势。为了在你的应用中禁用滑动取消的手势,集成默认的主题并且设置属性android:windowSwipeToDismiss为false
If you disable this gesture, you must implement the long-press-to-dismiss UI pattern to let users exit your app, as described in the next section.
<翻译>如果你禁用了这个手势,你必须实现长按取消的UI模式来用用户推出你的应用。
To use the DismissOverlayView
class in your activity, add this element to your layout definition such that it covers the whole screen and is placed above all other views.
The following example shows how to add the
element:
<翻译> 为了在你的activity 中使用 DismissOverlayView
类,添加这个类型到你的布局定义中。并且这个是覆盖整个界面同时被放在所有的view 的最上面。
下面的例子展示了怎么去添加
项目:
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent">
DismissOverlayView
android:id="@+id/dismiss_overlay"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
In your activity, obtain the
element and set some introductory text. This text is shown to users the first time they run your app to inform them that they can exit the app using a long press gesture. Then use a GestureDetector
to detect a long press:
<翻译>在你的activity 中,获取
项目然后设置一些介绍的文字。这个文字在用户第一次运行你的app的时候展示给用户通知他们他们可以通过长按的手势来推出。然后使用来检测手势。
public class WearActivity extends Activity { private DismissOverlayView mDismissOverlay; private GestureDetector mDetector; public void onCreate(Bundle savedState) { super.onCreate(savedState); setContentView(R.layout.wear_activity); // Obtain the DismissOverlayView element mDismissOverlay = (DismissOverlayView) findViewById(R.id.dismiss_overlay); mDismissOverlay.setIntroText(R.string.long_press_intro); mDismissOverlay.showIntroIfNecessary(); // Configure a gesture detector mDetector = new GestureDetector(this, new SimpleOnGestureListener() { public void onLongPress(MotionEvent ev) { mDismissOverlay.show(); } }); } // Capture long presses @Override public boolean onTouchEvent(MotionEvent ev) { return mDetector.onTouchEvent(ev) || super.onTouchEvent(ev); } }
When the system detects a long press gesture, the
element shows an Exit button, which terminates your activity if the user presses it.
<翻译>当系统检测到了一个长按的手势的时候,
会显示一个推出的按钮,如果用户按了这个按钮会将你的actiivty 结束