在线观看不卡亚洲电影_亚洲妓女99综合网_91青青青亚洲娱乐在线观看_日韩无码高清综合久久

鍍金池/ 教程/ Android/ 使用安卓Wear API創(chuàng)建watchface—第2部分
原文鏈接
Issue #185
Issue #181
Issue #161
Issue #192
Issue #174
Issue #190
RecyclerView FastScroll – Part 2
僅作為Android 調(diào)試模式工具的Stetho
Issue #150
Issue #167
Issue #180
Issue #151
Issue #188
Issue #159
Issue #189
Issue #160
Issue #168
Issue #146
Issue #173
Issue #198
Issue #179
延期的共享元素轉(zhuǎn)換(3b)
Yahnac:RxJava Firebase&內(nèi)容提供
Issue #162
游戲性能:規(guī)劃限定條件
分析清單:測量和尋找哪些方面
Issue #148
Issue #166
Issue #158
Issue #178
Issue #193
Issue #145
Issue #170
Issue #169
Issue #196
Issue #186
Issue #172
Issue #171
附加Android工件和Gradle的檔案
Issue #147
自定義顏色范圍
根據(jù) Material 設(shè)計(jì)導(dǎo)航制圖工具樣式
Issue #187
Issue #184
Issue #175
在Android Lollipop上使用JobScheduler API
Android性能案例追蹤研究
使用安卓Wear API創(chuàng)建watchface—第2部分
在谷歌市場上創(chuàng)造更好的用戶體驗(yàn)
映射與包的神秘關(guān)系
Issue #165
用Robolectric進(jìn)行參數(shù)化測試
Issue #155
Issue #149
MVC / MVP中的M -模型
歡迎為 Android 和 iOS 嵌入 API
Issue #164
Android UI 自動(dòng)化測試
Issue #182
Issue #191
Issue #183
Issue #163
Issue #157
響應(yīng)式編程(Reactive Programming)介紹
Issue #197
原文鏈接
Issue #153
Issue #152
Issue #176
原文地址
Android Material 支持庫:Electric Boogaloo的提示與技巧
Issue #156
Issue #154
Android的模糊視圖
Issue #194
Issue #177
Issue #195
針對(duì)Jenkins的谷歌商店安卓出版插件

使用安卓Wear API創(chuàng)建watchface—第2部分

在該教程的第一部分,已經(jīng)涉及到創(chuàng)建一個(gè)數(shù)字的Android Wear watchface的基礎(chǔ)知識(shí)。在這一部分中,將看到如何在Android Wear的移動(dòng)應(yīng)用中為其watchface添加一個(gè)面板設(shè)置。在學(xué)習(xí)完本文后,你將學(xué)會(huì)在你的移動(dòng)設(shè)備上從Android Wear應(yīng)用程序內(nèi)部控制背景顏色、時(shí)間、日期和你的watchface的顏色。

為了建立腕表和移動(dòng)設(shè)備之間的通信通道,你必須使用Google Play services中的Wearable Data Layer API。官方文件說明如下:

因?yàn)檫@些API是專為手持設(shè)備和可穿戴設(shè)備之間的通信而設(shè)計(jì)的,這是僅有的一些API,你應(yīng)該使用它們?cè)O(shè)置這些設(shè)備之間的通信。例如,不要嘗試打開底層套接字創(chuàng)建一個(gè)通信信道。

在可穿戴數(shù)據(jù)層API的幫助下,你可以將將多種對(duì)象類型發(fā)送到你的設(shè)備上:

  • DataItems ——提供數(shù)據(jù)存儲(chǔ)和自動(dòng)同步

  • [Messages]——主要用于遠(yuǎn)程過程調(diào)用和單向請(qǐng)求

  • [Assets] ——用于發(fā)送二進(jìn)制數(shù)據(jù)

對(duì)于通信渠道,我們將使用DataApi 來發(fā)送從移動(dòng)設(shè)備到數(shù)據(jù)層的DataItems ,而watchface將接受任何修改。

創(chuàng)建移動(dòng)設(shè)置界面

前面已經(jīng)看到,我們將要使用的API是Google Play services的一部分,所以你必須在你的mobile/src/main/AndroidManifest.xml文件中 <application>標(biāo)簽之下指定一個(gè)meta-data實(shí)體:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  package="com.catinean.simpleandroidwatchface">

  ...

  <application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme">

    <meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />

  </application>

</manifest>  

下一步,在 mobile/src/main/java/your_package 中創(chuàng)建一個(gè)空的activity組件,這將作為你的watchface的activity組件設(shè)置。我們調(diào)用它的SimpleWatchFaceConfigurationActivity。為了讓你的watchface認(rèn)為該activity組件為設(shè)置activity組件,你必須在你的 mobile AndroidManifest.xml文件中指定一個(gè)<intent-filter>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  package="com.catinean.simpleandroidwatchface">

  ...

  <application
    android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@mipmap/ic_launcher"
    android:theme="@style/AppTheme">

    <activity
      android:name="com.catinean.simpleandroidwatchface.SimpleWatchFaceConfigurationActivity"
      android:label="@string/app_name"
      android:theme="@style/AppTheme">
      <intent-filter>
        <action android:name="com.catinean.simpleandroidwatchface.CONFIG_DIGITAL" />
        <category android:name="com.google.android.wearable.watchface.category.COMPANION_CONFIGURATION" />
        <category android:name="android.intent.category.DEFAULT" />
      </intent-filter>
    </activity>

    <meta-data
      android:name="com.google.android.gms.version"
      android:value="@integer/google_play_services_version" />
  </application>
</manifest>  

正如你看到的,你必須為你的intent filter和類別指定一個(gè)自定義的 < action >。在我們的例子中,動(dòng)作名稱是由封裝名稱和配置字符串:com.catinean.simpleandroidwatchface.CONFIG_DIGITAL組成的。 在穿戴模塊方面,你必須在 AndroidManifest.xml 文件中為你之前創(chuàng)建的<service>實(shí)體增加一個(gè)額外的<meta-data>字段:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  package="com.catinean.simpleandroidwatchface">

  ...

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

    <service
      android:name=".SimpleWatchFaceService"
      android:label="@string/app_name"
      android:permission="android.permission.BIND_WALLPAPER">

      ...

      <meta-data
        android:name="com.google.android.wearable.watchface.companionConfigurationAction"
        android:value="com.catinean.simpleandroidwatchface.CONFIG_DIGITAL" />

      <intent-filter>
        <action android:name="android.service.wallpaper.WallpaperService" />
        <category android:name="com.google.android.wearable.watchface.category.WATCH_FACE" />
      </intent-filter>
    </service>
  </application>
</manifest>  

讓我們看一下,元數(shù)據(jù)如何對(duì)應(yīng)于activity組件的動(dòng)作名稱。

現(xiàn)在,activity組件是空的。為了用戶能夠配置watchface背景顏色、日期和時(shí)間的顏色,我們用兩個(gè)實(shí)體填充它。它的布局很簡單,形成一個(gè)帶有兩個(gè)元素的LinearLayout (一個(gè)用于背景顏色,另一個(gè)用于日期和時(shí)間的顏色)。該 mobile/src/main/res/layout/activity_configuration.xml 文件將會(huì)是如下的形式:

   <?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  android:layout_height="match_parent"
  android:layout_width="match_parent">

  <android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    style="@style/MyActionBarStyle" />

  <LinearLayout
    android:layout_below="@+id/toolbar"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:divider="@drawable/delimiter"
    android:showDividers="middle"
    android:paddingStart="16dp"
    android:paddingEnd="16dp">

    <RelativeLayout
      android:id="@+id/configuration_background_colour"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/selector_preference_background"
      android:paddingTop="16dp"
      android:paddingBottom="16dp">

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:textColor="@android:color/black"
        android:textSize="18sp"
        android:text="@string/background_colour" />

      <View
        android:id="@+id/configuration_background_colour_preview"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true" />

    </RelativeLayout>

    <RelativeLayout
      android:id="@+id/configuration_time_colour"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:background="@drawable/selector_preference_background"
      android:paddingTop="16dp"
      android:paddingBottom="16dp">

      <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:text="@string/date_and_time_colour" />

      <View
        android:id="@+id/configuration_date_and_time_colour_preview"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:layout_alignParentEnd="true" />

    </RelativeLayout>
  </LinearLayout>
</RelativeLayout>  

再回到activity組件,我們希望在對(duì)話框中顯示各元素顏色的名稱。為此,我們將創(chuàng)建一個(gè)簡單的ColourChooserDialog ,其將包含一個(gè)簡單的顏色列表,當(dāng)用戶點(diǎn)擊一個(gè)activity組件的元素時(shí),它將進(jìn)行顯示。

ColourChooserDialog將會(huì)是這樣的:

package com.catinean.simpleandroidwatchface;

import android.app.Activity;  
import android.app.AlertDialog;  
import android.app.Dialog;  
import android.app.DialogFragment;  
import android.content.DialogInterface;  
import android.os.Bundle;

public class ColourChooserDialog extends DialogFragment {

    private static final String ARG_TITLE = "ARG_TITLE";
    private Listener colourSelectedListener;

    public static ColourChooserDialog newInstance(String dialogTitle) {
        Bundle arguments = new Bundle();
        arguments.putString(ARG_TITLE, dialogTitle);
        ColourChooserDialog dialog = new ColourChooserDialog();
        dialog.setArguments(arguments);
        return dialog;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        colourSelectedListener = (Listener) activity;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        String title = getArguments().getString(ARG_TITLE);
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setTitle(title)
                .setItems(R.array.colors_array, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        String[] colours = getResources().getStringArray(R.array.colors_array);
                        colourSelectedListener.onColourSelected(colours[which], getTag());
                    }
                });
        return builder.create();
    }

    interface Listener {
        void onColourSelected(String colour, String tag);
    }
}

你可以看到,這是一個(gè)簡單的DialogFragment ,其通過一個(gè)字符串?dāng)?shù)組R.array.colors_array顯示AlertDialog列表(獲取更多關(guān)于對(duì)話框的一般信息,你可以在這里閱讀官方文檔)。

該字符串?dāng)?shù)組只是mobile/src/main/res/values/arrays.xml的內(nèi)部資源:

<?xml version="1.0" encoding="utf-8"?>  
<resources>  
  <string-array name="colors_array">
    <item name="black">Black</item>
    <item name="white">White</item>
    <item name="red">Red</item>
    <item name="green">Green</item>
    <item name="cyan">Cyan</item>
    <item name="magenta">Magenta</item>
  </string-array>
</resources>   

為了通知所選擇的顏色的活動(dòng),該對(duì)話框提供了一個(gè)接口。因?yàn)閷⒂卸鄠€(gè)對(duì)話框的創(chuàng)建(一個(gè)用于背景顏色和一個(gè)用于日期和時(shí)間的顏色),而且必須要通過它們的tag來區(qū)分。

SimpleWatchFaceConfigurationActivity 會(huì)是這樣的:

public class SimpleWatchFaceConfigurationActivity extends ActionBarActivity implements ColourChooserDialog.Listener {

    private static final String TAG_BACKGROUND_COLOUR_CHOOSER = "background_chooser";
    private static final String TAG_DATE_AND_TIME_COLOUR_CHOOSER = "date_time_chooser";

    private View backgroundColourImagePreview;
    private View dateAndTimeColourImagePreview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_configuration);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        findViewById(R.id.configuration_background_colour).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColourChooserDialog.newInstance(getString(R.string.pick_background_colour))
                        .show(getFragmentManager(), TAG_BACKGROUND_COLOUR_CHOOSER);
            }
        });

        findViewById(R.id.configuration_time_colour).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ColourChooserDialog.newInstance(getString(R.string.pick_date_time_colour))
                        .show(getFragmentManager(), TAG_DATE_AND_TIME_COLOUR_CHOOSER);
            }
        });

        backgroundColourImagePreview = findViewById(R.id.configuration_background_colour_preview);
        dateAndTimeColourImagePreview = findViewById(R.id.configuration_date_and_time_colour_preview);

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onColourSelected(String colour, String tag) {

        if (TAG_BACKGROUND_COLOUR_CHOOSER.equals(tag)) {
            backgroundColourImagePreview.setBackgroundColor(Color.parseColor(colour));
        } else {
            dateAndTimeColourImagePreview.setBackgroundColor(Color.parseColor(colour));
        }
    }
}

如果現(xiàn)在你就要在穿戴設(shè)備運(yùn)行wear (穿戴)模塊,然后是移動(dòng)設(shè)備上的mobile(移動(dòng)) 模塊,導(dǎo)航到 Android Wear應(yīng)用程序,你將能夠訪問activity組件的設(shè)置。

http://wiki.jikexueyuan.com/project/android-weekly/images/issue-146/8.1.gif" alt="alt text" />

發(fā)送數(shù)據(jù)到API的數(shù)據(jù)層

現(xiàn)在,我們能夠捕捉到選定的顏色,現(xiàn)在該去了解我們是如何將它發(fā)送到API的數(shù)據(jù)層的。為了連接和發(fā)送數(shù)據(jù),你將不得不使用GoogleApiClient 類。在activity組件中的onCreate(),我們創(chuàng)建了對(duì)象,使用 onStart()我們連接到了數(shù)據(jù)層,使用onStop()斷開連接。我們?cè)鰪?qiáng)的activity組件將會(huì)是這樣的:

public class SimpleWatchFaceConfigurationActivity extends ActionBarActivity implements ColourChooserDialog.Listener,  
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    ... 
    private static final String TAG = "SimpleWatchface";

    private GoogleApiClient googleApiClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

       ...

        googleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(Wearable.API)
                .build();

        ...
    }

    @Override
    protected void onStart() {
        super.onStart();
        googleApiClient.connect();
    }

   ...

    @Override
    public void onConnected(Bundle bundle) {
        Log.d(TAG, "onConnected");
    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.e(TAG, "onConnectionSuspended");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.e(TAG, "onConnectionFailed");
    }

    @Override
    protected void onStop() {
        if (googleApiClient != null && googleApiClient.isConnected()) {
            googleApiClient.disconnect();
        }
        super.onStop();
    }
}

為了確保連接,我加了一些相應(yīng)的回調(diào)日志。一個(gè)同步數(shù)據(jù)層由兩元素組成:

  • payload: 實(shí)際要發(fā)送的數(shù)據(jù)。

  • path:你要發(fā)送數(shù)據(jù)項(xiàng)的一個(gè)唯一的標(biāo)識(shí)符(path必須以正斜杠開頭,例如"/simple_watch_face_config")。 我們將在 PutDataMapRequest的幫助下發(fā)送數(shù)據(jù),這為我們提供了一個(gè)類似于行為的關(guān)鍵值。onColourSelected(String colour, String tag)方法將會(huì)是這樣的:
@Override
public void onColourSelected(String colour, String tag) {  
     PutDataMapRequest putDataMapReq = PutDataMapRequest.create("/simple_watch_face_config");

     if (TAG_BACKGROUND_COLOUR_CHOOSER.equals(tag)) {
         backgroundColourImagePreview.setBackgroundColor(Color.parseColor(colour));
         putDataMapReq.getDataMap().putString("KEY_BACKGROUND_COLOUR", colour);
     } else {
         dateAndTimeColourImagePreview.setBackgroundColor(Color.parseColor(colour));
         putDataMapReq.getDataMap().putString("KEY_DATE_TIME_COLOUR", colour);
     }

     PutDataRequest putDataReq = putDataMapReq.asPutDataRequest();
     Wearable.DataApi.putDataItem(googleApiClient, putDataReq);
}

你會(huì)看到在指定的path 上如何創(chuàng)建一個(gè)PutDataRequest ,每次收到一個(gè)顏色,就用一個(gè)特定的key填充映射。最后,在Wearable.DataApi.putDataItem(googleApiClient, putDataReq)方法的幫助下發(fā)送請(qǐng)求。

現(xiàn)在,我們能夠發(fā)送顏色,必須在可穿戴模型中對(duì)它們進(jìn)行處理,具體是在先前創(chuàng)建的SimpleWatchFaceService里處理。

在SIMPLEWATCHFACESERVICE中處理接收到的配置

回到可穿戴模型,我們必須處理SimpleWatchFaceService中之前通過SimpleWatchFaceConfigurationActivity 發(fā)來的配置信息。

正如在配置activity組件中所做的那樣,為了與數(shù)據(jù)層API同步,必須首先通過一個(gè)GoogleApiClient對(duì)象連接到它。當(dāng)watchface是可見時(shí),我們將開始連接到API;當(dāng)watchface是不可見的時(shí),將端口與API的連接。你的SimpleWatchFaceService 將會(huì)是這樣:

public class SimpleWatchFaceService extends CanvasWatchFaceService {

    ...

    private class SimpleEngine extends CanvasWatchFaceService.Engine implements
            GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

        ...

        private GoogleApiClient googleApiClient;

        @Override
        public void onCreate(SurfaceHolder holder) {
            ...
            googleApiClient = new GoogleApiClient.Builder(SimpleWatchFaceService.this)
                    .addApi(Wearable.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }

        ...

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            if (visible) {
                ...
                googleApiClient.connect();
            } else {
                ...
                releaseGoogleApiClient();
            }

            ...
        }

        private void releaseGoogleApiClient() {
            if (googleApiClient != null && googleApiClient.isConnected()) {
                googleApiClient.disconnect();
            }
        }

        ...

        @Override
        public void onConnected(Bundle bundle) {
            Log.d(TAG, "connected GoogleAPI");
        }

        @Override
        public void onConnectionSuspended(int i) {
            Log.e(TAG, "suspended GoogleAPI");
        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.e(TAG, "connectionFailed GoogleAPI");
        }

        @Override
        public void onDestroy() {
            ...
            releaseGoogleApiClient();

            super.onDestroy();
        }
    }
}

我們?cè)?code>SimpleEngine 的 onCreate()方法中創(chuàng)建了GoogleApiClient對(duì)象,當(dāng)watchface是可見的時(shí)進(jìn)行連接,當(dāng)watchface是不可見的時(shí)釋放客戶端。

下一步,當(dāng)移動(dòng)activity組件的背景顏色和日期、時(shí)間顏色的值改變以及每次watch face連接到數(shù)據(jù)層時(shí),我們想要得到通知。為了實(shí)現(xiàn)這個(gè)功能,不得不使用:

  • DataApi.DataListener——每次改變數(shù)據(jù)層的某些數(shù)據(jù)時(shí),將會(huì)獲得通知。

  • ResultCallback——每次googleApiClient 連接時(shí),它將會(huì)通知我們。
public class SimpleWatchFaceService extends CanvasWatchFaceService {

    ...

    private class SimpleEngine extends CanvasWatchFaceService.Engine implements
            GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {

        ...

        private GoogleApiClient googleApiClient;

        @Override
        public void onCreate(SurfaceHolder holder) {
            ...
            googleApiClient = new GoogleApiClient.Builder(SimpleWatchFaceService.this)
                    .addApi(Wearable.API)
                    .addConnectionCallbacks(this)
                    .addOnConnectionFailedListener(this)
                    .build();
        }

        ...

        @Override
        public void onVisibilityChanged(boolean visible) {
            super.onVisibilityChanged(visible);
            if (visible) {
                ...
                googleApiClient.connect();
            } else {
                ...
                releaseGoogleApiClient();
            }

            ...
        }

        private void releaseGoogleApiClient() {
            if (googleApiClient != null && googleApiClient.isConnected()) {
                Wearable.DataApi.removeListener(googleApiClient, onDataChangedListener);
                googleApiClient.disconnect();
            }
        }

        ...

        @Override
        public void onConnected(Bundle bundle) {
            Log.d(TAG, "connected GoogleAPI");
            Wearable.DataApi.addListener(googleApiClient, onDataChangedListener);
            Wearable.DataApi.getDataItems(googleApiClient).setResultCallback(onConnectedResultCallback);
        }

        private final DataApi.DataListener onDataChangedListener = new DataApi.DataListener() {
            @Override
            public void onDataChanged(DataEventBuffer dataEvents) {
                for (DataEvent event : dataEvents) {
                    if (event.getType() == DataEvent.TYPE_CHANGED) {
                        DataItem item = event.getDataItem();
                        processConfigurationFor(item);
                    }
                }

                dataEvents.release();
                invalidateIfNecessary();
            }
        };

        private void processConfigurationFor(DataItem item) {
            if ("/simple_watch_face_config".equals(item.getUri().getPath())) {
                DataMap dataMap = DataMapItem.fromDataItem(item).getDataMap();
                if (dataMap.containsKey("KEY_BACKGROUND_COLOUR")) {
                    String backgroundColour = dataMap.getString("KEY_BACKGROUND_COLOUR");
                    watchFace.updateBackgroundColourTo(Color.parseColor(backgroundColour));
                }

                if (dataMap.containsKey("KEY_DATE_TIME_COLOUR")) {
                    String timeColour = dataMap.getString("KEY_DATE_TIME_COLOUR");
                    watchFace.updateDateAndTimeColourTo(Color.parseColor(timeColour));
                }
            }
        }

        private final ResultCallback<DataItemBuffer> onConnectedResultCallback = new ResultCallback<DataItemBuffer>() {
            @Override
            public void onResult(DataItemBuffer dataItems) {
                for (DataItem item : dataItems) {
                    processConfigurationFor(item);
                }

                dataItems.release();
                invalidateIfNecessary();
            }
        };

        @Override
        public void onConnectionSuspended(int i) {
            Log.e(TAG, "suspended GoogleAPI");
        }

        @Override
        public void onConnectionFailed(ConnectionResult connectionResult) {
            Log.e(TAG, "connectionFailed GoogleAPI");
        }

        @Override
        public void onDestroy() {
            ...
            releaseGoogleApiClient();

            super.onDestroy();
        }
    }
}

我們可以看到,每次watch face可見時(shí),連接到數(shù)據(jù)層,一旦連接后,增加兩個(gè)監(jiān)聽器。數(shù)據(jù)層每次發(fā)生變化時(shí),onDataChangedListener 將得到通知;當(dāng)服務(wù)連接時(shí),onConnectedResultCallback 將得到通知。在這兩種情況下,我們想處理接收到的DataItem - processConfigurationFor(DataItem)。在處理Items時(shí),為了抓到發(fā)送的值,使用path (/simple_watch_face_config)和 關(guān)聯(lián)到每一項(xiàng)的keys (在SimpleWatchFaceConfigurationActivity中定義了這些keys )。一旦項(xiàng)目被識(shí)別,傳遞它們到SimpleWatchFace 以更新顏色:

  • updateBackgroundColourTo(int colour):負(fù)責(zé)更新背景顏色

  • updateDateAndTimeColourTo(int colour):負(fù)責(zé)更新數(shù)據(jù)和時(shí)間的顏色
 public class SimpleWatchFace {  
    ...

    private static final int DATE_AND_TIME_DEFAULT_COLOUR = Color.WHITE;
    private static final int BACKGROUND_DEFAULT_COLOUR = Color.BLACK;

    private int backgroundColour = BACKGROUND_DEFAULT_COLOUR;
    private int dateAndTimeColour = DATE_AND_TIME_DEFAULT_COLOUR;

    public static SimpleWatchFace newInstance(Context context) {
        Paint timePaint = new Paint();
        timePaint.setColor(DATE_AND_TIME_DEFAULT_COLOUR);
        timePaint.setTextSize(context.getResources().getDimension(R.dimen.time_size));
        timePaint.setAntiAlias(true);

        Paint datePaint = new Paint();
        datePaint.setColor(DATE_AND_TIME_DEFAULT_COLOUR);
        datePaint.setTextSize(context.getResources().getDimension(R.dimen.date_size));
        datePaint.setAntiAlias(true);

        Paint backgroundPaint = new Paint();
        backgroundPaint.setColor(BACKGROUND_DEFAULT_COLOUR);

        return new SimpleWatchFace(timePaint, datePaint, backgroundPaint, new Time());
    }

    SimpleWatchFace(Paint timePaint, Paint datePaint, Paint backgroundPaint, Time time) {
        this.timePaint = timePaint;
        this.datePaint = datePaint;
        this.backgroundPaint = backgroundPaint;
        this.time = time;
    }

    public void draw(Canvas canvas, Rect bounds) {
        time.setToNow();
        canvas.drawRect(0, 0, bounds.width(), bounds.height(), backgroundPaint);

        ...
    }

    public void updateDateAndTimeColourTo(int colour) {
        dateAndTimeColour = colour;
        timePaint.setColor(colour);
        datePaint.setColor(colour);
    }

    public void updateBackgroundColourTo(int colour) {
        backgroundColour = colour;
        backgroundPaint.setColor(colour);
    }

    ...
}

最后剩下的是處理環(huán)境模式。在環(huán)境模式中,會(huì)默認(rèn)設(shè)置為黑色和白色的顏色。

@Override
public void onAmbientModeChanged(boolean inAmbientMode) {  
    super.onAmbientModeChanged(inAmbientMode);
    watchFace.setAntiAlias(!inAmbientMode);
    watchFace.setShowSeconds(!isInAmbientMode());

    if (inAmbientMode) {
        watchFace.updateBackgroundColourToDefault();
        watchFace.updateDateAndTimeColourToDefault();
    } else {
        watchFace.restoreBackgroundColour();
        watchFace.restoreDateAndTimeColour();
    }

    invalidate();

    startTimerIfNecessary();
}

每次進(jìn)入環(huán)境模式我們更新默認(rèn)的顏色,當(dāng)它存在環(huán)境模式,我們必須恢復(fù)其選擇的顏色:

public class SimpleWatchFace {
... public void updateBackgroundColourToDefault() { backgroundPaint.setColor(BACKGROUND_DEFAULT_COLOUR); }

public void updateDateAndTimeColourToDefault() {
    timePaint.setColor(DATE_AND_TIME_DEFAULT_COLOUR);
    datePaint.setColor(DATE_AND_TIME_DEFAULT_COLOUR);
}

public void restoreDateAndTimeColour() {
    timePaint.setColor(dateAndTimeColour);
    datePaint.setColor(dateAndTimeColour);
}

public void restoreBackgroundColour() {
    backgroundPaint.setColor(backgroundColour);
}

}



現(xiàn)在你可以開始測試端到端的全部配置。首先在你的腕表上運(yùn)行可穿戴模塊,然后是手機(jī)上的移動(dòng)模塊。跳到你手機(jī)上的 Android Wear 應(yīng)用程序,你可以選擇安裝腕表和訪問它的設(shè)置屏幕。從這里你可以改變背景以及日期和時(shí)間的顏色。
上一篇:Issue #165下一篇:Issue #158