Android Weekly Issue #319

July 22nd, 2018.

Android Weekly Issue #319

本期内容包括: MotionLayout加动画; Kotlin中的when; Android Studio的快捷键; Google的开发者认证; 一个天气应用的的数据层构建; 一个Android项目的CI搭建; 用Kotlin写gradle build脚本;

测试库KotlinTest的使用; LiveData, TransformationsMediatorLiveData的使用; Kotlin中property delegation的使用; Kotlin的一个事件分发库:

KDispatcher.

ARTICLES & TUTORIALS

Creating Animations With MotionLayout for Android

ConstraintLayout非常有用, 但是想要给它的内容加复杂的动画却有点费时间. 所以Google在I/O 2018推出了MotionLayout.

MotionLayout在support库中, 继承了ConstraintLayout, 是目前唯一一个只用xml就可以为它的内容声明动画的控件, 并对于它所有的动画都提供了细粒度的控制.

本篇文章讲述如何使用MotionLayout.

创建布局

首先MotionLayout的使用和ConstraintLayout一样, 在其中放入View然后设置约束.

创建Motion Scene

然后, 创建一个motion scene:

<?xml version="1.0" encoding="utf-8"?>
<MotionScene
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"> <!-- More code here --> </MotionScene>

在其中加入ConstraintSet, 通常是两个, 一个为动画开始, 一个为结束:

<ConstraintSet android:id="@+id/starting_set">
<Constraint android:id="@+id/actor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_width="60dp"
android:layout_height="60dp"
/>
</ConstraintSet> <ConstraintSet android:id="@+id/ending_set">
<Constraint android:id="@+id/actor"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_width="60dp"
android:layout_height="60dp"
/>
</ConstraintSet>

注意每一个ConstraintSet都必须指明大小和位置, 因为它们将会覆盖layout中的设置.

还需要一个Transition元素:

<Transition
android:id="@+id/my_transition"
app:constraintSetStart="@+id/starting_set"
app:constraintSetEnd="@+id/ending_set"
app:duration="2000"> </Transition>

最后在MotionLayout中关联motion scene文件:

app:layoutDescription="@xml/my_scene"

运行动画

配置完成后, 当你运行程序的时候, MotionLayout会自动应用TransitionconstraintSetStart属性指定的constraint set.

所以为了开启动画, 你只需调用控件的transitionToEnd()方法.

事件监听

可以给MotionLayout附加TransitionListener来监听动画事件.

创建关键帧

上面的例子只规定了起始点和重点, 所以是一个直线, 如果想要更改路径, 可以通过定义几个关键点.

在motion scene的Transition中增加KeyFrameSet.

MotionLayout支持多种关键帧的类型, 本文示范了KeyPositionKeyCycle的用法.

前者规定一个位置, 后者规定振荡模式.

和动画的元素交互

可以直接和动画的元素交互, 目前支持点击和拖拽事件.

分别是通过在Transition中增加:

<OnClick
app:target="@+id/actor"
app:mode="transitionToEnd"/>

和:

<OnSwipe
app:touchAnchorId="@+id/actor"
app:touchAnchorSide="top"
app:dragDirection="dragUp" />

When is “when” exhaustive?

Kotlin中的when的使用.

要么提供所有的分支, 要么提供一个else.

本文提出写一个扩展, 让编译器强制我们实现一个完备的when.

Android Studio - Taming the interface

Android Studio的一些快捷键:

  • ⌥ + number: open/close views

  • ⇧ + ⌘ + ↑: enlarge view

  • ⇧ + ⌘ + ↓: shrink view

  • ⇧ + ⌘ + →: enlarge side view

  • ⇧ + ⌘ + ←: shrink side view

  • ⇧ + ⌘ + F12: close all views

  • ⇧ + ⌘ + ]: next tab

  • ⇧ + ⌘ + [: previous tab

  • ^ + ⇧ + →: text view (xml layout editing)

  • ^ + ⇧ + ←: design view (visual layout editing)

  • ⌥ + letter: invoke button

  • ⌘ + ⇧ + A: action lookup

Becoming Google Certified Associate Android Developer

一个谷歌认证的考试: Associate Android Developer Exam介绍.

包括一个需要在24h内完成的项目和一个10分钟的在线视频面试. 费用$149, 完成之后可以获得Associate Android Developer称号.

Maintainable Architecture – Daily Forecast

一个天气应用的数据层设计.

(Kotlin, dagger. 上一次介绍的是

UI层.)

Cloud Continuous Integration on Android with Kotlin Project

作者为自己的项目:

  • 搭建CI, 用Travis.
  • 集成覆盖率测试报告, 用Codecov.io + JaCoCo.
  • 可以把CI和覆盖率状态加在readme里.

Moving Your Gradle Build Scripts to Kotlin

gradle是用Groovy写的.

首先讨论了Groovy写脚本的几个缺点:

  • 动态类型.
  • IDE的代码提示慢, 不完整.
  • Groovy写的Gradle不能调试.

本文介绍解决这个问题的方法: 增加一个buildSrc module.

buildSrc是一个特殊的module, 在gradle构建中是第一个被编译的模块, 在里面你可以做这些事情:

  • 写自定义的gradle插件.
  • 提取工具函数和配置, 让它们全局可见.
  • 写自定义的task类.
  • 整理自定义的task graph.
  • 单步调试.

文中介绍了用kotlin写build脚本的详细做法.

Data Driven Testing with KotlinTest

Data driven testing是指同样的测试代码, 但是要用很多不同的输入数据和期待结果来进行测试.

KotlinTest是一个Kotlin写测试的库.

本文介绍了如何使用它来写一些测试.

Reactive patterns using Transformations and MediatorLiveData

利用LiveData来构建流式的构架.

LiveData的目的: 解决View controller(Activity, Fragment)和数据源(ViewModel)之间的通信, 让View只在生命周期允许的时候接收数据. 也即, 你不用手动地去取消View和ViewModel之间的订阅.

你可以利用这种方式去观察应用的其他组件, 比如:

  • SharedPreference的改变.
  • Firestore的document或collection.
  • Authentication SDK(比如FirebaseAuth)中的当前用户.
  • Room中的一个query.

这种机制的好处是以为一切都绑定在一起, 一旦数据改变了, UI就会自动更新.

缺点就是LiveData没有想Rx那样, 自带绑定流式数据或管理线程的工具集.

为了在组件中传递数据, 我们需要map和combine的方法, 用了MediatorLiveData, 结合Transformations中的方法:

  • 一对一静态转换: Transformations.map: 在ViewModel中把从repository取来的数据转化成UI model.
  • 一对一动态转换: Transformations.switchMap: 在观察repository之前需要观察user manager得到一个用户id.
  • 一对多依赖: MediatorLiveData让你可以给单个observable加一到多个数据源.

什么时候不需要用LiveData?

当你的组件和UI没有联系的时候, 就很可能用不上LiveData.

Antipattern: 共享LiveData的实例, 可能会有数据更新错误或显示错误的情况. 所以尽量不要共享LiveData的实例.

文章后面说了一些MediatorLiveData, mapswitchMap使用时的注意事项, 以及用Kotlin来实现时的简洁版.

Delegate your Lifecycle to Kotlin

Property delegation是Kotlin中非常有用的一个特性.

通过这个模式, 我们可以把get和set一个property的任务交给一个类来处理. 但是表面上用起来仍然像是在直接使用这个property.

本文讲如何用Kotlin的property delegation来处理Fragment和Activity之间的通信.

(笔者注: 但是我觉得文中所讲的例子并没有内存泄露的情况, 所以问题不存在, 解决方案也没有必要了.)

KDispatcher simple and light-weight event bus for Kotlin

一个Kotlin的事件分发库: KDispatcher.

类似于EventBus.

LIBRARIES & CODE

wearfaceutils

写Android手表表面的工具类.

pickle

一个Android cucumber测试的代码生成库.

kotlintest

Kotlin测试框架.

SdkSearch

Android应用和Chrome extension用来搜索Android Sdk的文档.

ketro

Kotlin包装的Retrofit和LiveData.

KDispatcher

Kotlin的事件分发库, 功能类似于EventBus.

Android Weekly Notes Issue #319的更多相关文章

  1. Android Weekly Notes Issue #230

    Android Weekly Notes Issue #230 November 6th, 2016 Android Weekly Issue #230. Android Weekly笔记, 本期内容 ...

  2. Android Weekly Notes Issue #227

    Android Weekly Issue #227 October 16th, 2016 Android Weekly Issue #227. 本期内容包括: Google的Mobile Vision ...

  3. Android Weekly Notes Issue #237

    Android Weekly Issue #237 December 25th, 2016 Android Weekly Issue #237 这是本年的最后一篇issue, 感谢大家. 本期内容包括 ...

  4. Android Weekly Notes Issue #229

    Android Weekly Issue #229 October 30th, 2016 Android Weekly Issue #229 Android Weekly笔记, 本期内容包括: 性能库 ...

  5. Android Weekly Notes Issue #221

    Android Weekly Issue #221 September 4th, 2016 Android Weekly Issue #221 ARTICLES & TUTORIALS And ...

  6. Android Weekly Notes Issue #219

    Android Weekly Issue #219 August 21st, 2016 Android Weekly Issue #219 ARTICLES & TUTORIALS Andro ...

  7. Android Weekly Notes Issue #236

    Android Weekly Issue #236 December 18th, 2016 Android Weekly Issue #236 本期内容包括: Google的物联网平台Android ...

  8. Android Weekly Notes Issue #235

    Android Weekly Issue #235 December 11th, 2016 Android Weekly Issue #235 本期内容包括: 开发一个自定义View并发布为开源库的完 ...

  9. Android Weekly Notes Issue #234

    Android Weekly Issue #234 December 4th, 2016 Android Weekly Issue #234 本期内容包括: ConstraintLayout的使用; ...

随机推荐

  1. 14.【nuxt起步】-Pm2 和nuxt服务运行

    1.安装pm2 npm install pm2 -gd 2.启动 Pm2 start ./bin/www 3. pm2 save 4.Pm2 startup 5.Pm2 save修改 package. ...

  2. Spring boot Security Disable security

    When I use security.basic.enabled=false to disable security on a Spring Boot project that has the fo ...

  3. java 逻辑运算符 短路(条件操作)

    两个数字计算时都会先把数字转换成二进制后再进行换算,二进制就是由0和1组成的数字  http://yxwang0615.iteye.com/blog/1084288    

  4. DOTA游戏相关的文章

    DOTA里面到底有几号位?各代表什么? DOTA新手进阶之S.SHIFT及M键的使用 Dota开局 对线方法技巧总结 dota补刀的技巧 dota需要注意的小细节 dota前期如何对线 DotA 命令 ...

  5. Java 实现对Sql语句解析

    原文地址:http://www.cnblogs.com/zcftech/archive/2013/06/10/3131286.html 最近要实现一个简易的数据库系统,除了要考虑如何高效的存储和访问数 ...

  6. 搭建SpringMVC+Hibernate4+Spring3+Ajax+Maven项目

    首先新建一个Maven项目.百度一下会有非常多实例,这里就不介绍了,直接奔主题. 如题:这里使用的是Hibernate4和Spring3,使用的JPA和Spring注解,然后JDK版本号是1.7 以下 ...

  7. [ACM] POJ 1068 Parencodings(模拟)

    Parencodings Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19352   Accepted: 11675 De ...

  8. Fragment小结

    Fragment是Android3.0之后增加的新特性,通常人们叫它碎片.可是,我认为把它理解成一个View模块比較好,尽管它不是继承自View.假设阅读过源代码就知道它是内置View对象从而实现Vi ...

  9. [转]FPGA网站推荐

    1. OPENCORES.ORG这里提供非常多,非常好的PLD了内核,8051内核就可以在里面找到.进入后,选择project或者由http//www.opencores.org/browse.cgi ...

  10. Android 逐帧动画( Drawable 动画),这一篇就够了

    前言 作为 Android 最常见的两种动画形式,逐帧动画( Drawable 动画),有着极其广泛的应用,它的原理与早起的电影以及 GIF 类似,就是把一张的图,按顺序快速切换,这样一来看上去就好像 ...