Android Phone 和 Android Wear

展示同样的功能 显示文本hello  world

android wear 效果图


hello world 1

android phone


hello world 2



第一 处比较  AndroidManifest.xml文件

Android Phone 的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.zhoumiao.myandroidphonedome01"> <uses-permission android:name="android.permission.WAKE_LOCK" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@style/AppTheme.NoActionBar"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application></manifest>

Android Wear 的 AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" package="com.example.zhoumiao.mywear01"> <uses-feature android:name="android.hardware.type.watch" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <!-- The ACCESS_COARSE/FINE_LOCATION permissions are not required to use Google Maps Android API v2, but you must specify either coarse or fine location permissions for the 'MyLocation' functionality. --> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <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" /> <!-- Set to true if your app is Standalone, that is, it does not require the handheld app to run. --> <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>

从上面粗黑体可以看到有三个wear 多了三个标签 <uses-feature android:name="android.hardware.type.watch" /> 、<uses-library android:name="com.google.android.wearable" android:required="true" /> 、<meta-data android:name="com.google.android.wearable.standalone" android:value="true" />

第二处比较 activity_main.xml文件

Android Phone 的activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout 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" android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" tools:context="com.example.zhoumiao.myandroidphonedome01.MainActivity" tools:showIn="@layout/activity_main">

    <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello World!" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /></android.support.constraint.ConstraintLayout>

Android Wear 的activity_main.xml

<?xml version="1.0" encoding="utf-8"?><android.support.wear.widget.BoxInsetLayout 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" 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.example.zhoumiao.mywear01.MainActivity" tools:deviceIds="wear"> <FrameLayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="@dimen/inner_frame_layout_padding" app:boxedEdges="all"> <TextView android:id="@+id/text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> </FrameLayout></android.support.wear.widget.BoxInsetLayout>

可以看到他们使用的组件是一样的都是 TextView 组件,是Android sdk 的api。但是 phone 使用的布局是ConstraintLayout,wear 使用的是BoxInsetLayout 中包含FrameLayout 

ConstraintLayout api部分代码截取

public class ConstraintLayout extends ViewGroup { static final boolean ALLOWS_EMBEDDED = false; private static final boolean CACHE_MEASURED_DIMENSION = false; public static final String VERSION = "ConstraintLayout-1.1.3"; private static final String TAG = "ConstraintLayout"; private static final boolean USE_CONSTRAINTS_HELPER = true; private static final boolean DEBUG = false; SparseArray<View> mChildrenByIds = new SparseArray();

BoxInsetLayout  api 部分代码截取

@UiThreadpublic   class BoxInsetLayout extends ViewGroup { private static final float FACTOR = 0.146447f; //(1 - sqrt(2)/2)/2 private static final int DEFAULT_CHILD_GRAVITY = Gravity.TOP | Gravity.START; private final int mScreenHeight; private final int mScreenWidth;    private boolean mIsRound; private Rect mForegroundPadding; private Rect mInsets; private Drawable mForegroundDrawable; 

可以看到其实ConstraintLayout  和 BoxInsetLayout 都是继承了同一个 api ,即ViewGroup 

第三处,看MainActivity

phone 的继承关系

 MainActivity -> AppCompatActivity -> FragmentActivity -> BaseFragmentActivityApi16 ->BaseFragmentActivityApi14 ->SupportActivity ->Activity

wear 的继承关系

WearableActivity ->Activity

通过以上三处比较可以看出

phone和wear 最基本的 api 都是一样的,只是在不同平台实现上做了布局上的差异调整。

build.gradle文件看依赖库差异

Phone 的 build.gradle

dependencies { implementation fileTree(dir: 'libs', include: ['*.jar']) implementation 'com.android.support:appcompat-v7:26.1.1' implementation 'com.android.support.constraint:constraint-layout:1.1.3' implementation 'com.android.support:design:26.1.1' testImplementation 'junit:junit:4.12' androidTestImplementation 'com.android.support.test:runner:1.0.2' androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'}

wear 的 build.gradle

dependencies { implementation 'com.android.support:wear:27.1.1' implementation 'com.google.android.support:wearable:2.3.0' compileOnly 'com.google.android.wearable:wearable:2.3.0'}


现在添加 图片ImageView和精度条组件ProgressBar

效果图

phone


phone


wear 


wear

三个组件。

<ImageView android:id="@+id/bgImages" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/watch" />

<TextView android:id="@+id/text" android:textColor="@color/orange" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" />

<ProgressBar android:layout_width="match_parent" android:layout_marginTop="230px" android:layout_height="match_parent" android:max="100" android:progress="50" style="?android:attr/progressBarStyleHorizontal"/>

wear 的Android api 和phone 的Android api 的差异

phone 支持的 Android sdk版本

Wear 支持的 Android sdk版本

由上面可以看到在 api 19 以前 不支持 手表,到api 20 以后才支持手表

那么api 19 和 api 20 有什么差异呢?

api 19 对应Android 4.2,4.2.2 叫做KITKAT ,api 20 对应Android 4.4W  叫做KITKAT_WATCH

Android 4.2,4.2.2及更小版本,不支持手表开发,Android 4.4W开始支持手表开发。

Android 4.4W 里面有 Android Wear ,Android Wear是指2014年03月19日,谷歌为智能手表打造的全新智能平台Android Wear。

2016年05月19日凌晨,在2016谷歌I/O大会上,谷歌推出了新一代的智能手表操作系统Android Wear 2.0,其中最重要的更新为操作系统自带LTE网络和Wi-Fi。


11
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容