Skip to content

Commit 741eab8

Browse files
authored
Merge pull request #61 from alibaba/develop
Add transition animation support.
2 parents 5ca6778 + fc3b2b8 commit 741eab8

File tree

10 files changed

+183
-23
lines changed

10 files changed

+183
-23
lines changed

README.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
7. 映射关系按组分类、多级管理,按需初始化
2929
8. 支持用户指定全局降级与局部降级策略
3030
9. 页面、拦截器、服务等组件均自动注册到框架
31+
10. 支持多种方式配置转场动画
3132

3233
#### 二、典型应用
3334
1. 从外部URL映射到内部页面,以及参数传递与解析
@@ -307,8 +308,23 @@
307308
.build("/home/main")
308309
.getExtra();
309310

311+
// 转场动画(常规方式)
312+
ARouter.getInstance()
313+
.build("/test/activity2")
314+
.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
315+
.navigation(this);
316+
317+
// 转场动画(API16+)
318+
ActivityOptionsCompat compat = ActivityOptionsCompat.
319+
makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);
320+
321+
ARouter.getInstance()
322+
.build("/test/activity2")
323+
.withOptionsCompat(compat)
324+
.navigation();
325+
310326
// 使用绿色通道(跳过所有的拦截器)
311-
ARouter.getInstance().build("/home/main").greenChannal().navigation();
327+
ARouter.getInstance().build("/home/main").greenChannel().navigation();
312328

313329
// 使用自己的日志工具打印日志
314330
ARouter.setLogger();

README_CN.md

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
7. 映射关系按组分类、多级管理,按需初始化
2727
8. 支持用户指定全局降级与局部降级策略
2828
9. 页面、拦截器、服务等组件均自动注册到框架
29+
10. 支持多种方式配置转场动画
2930

3031
#### 二、典型应用
3132
1. 从外部URL映射到内部页面,以及参数传递与解析
@@ -305,8 +306,23 @@
305306
.build("/home/main")
306307
.getExtra();
307308

309+
// 转场动画(常规方式)
310+
ARouter.getInstance()
311+
.build("/test/activity2")
312+
.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
313+
.navigation(this);
314+
315+
// 转场动画(API16+)
316+
ActivityOptionsCompat compat = ActivityOptionsCompat.
317+
makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);
318+
319+
ARouter.getInstance()
320+
.build("/test/activity2")
321+
.withOptionsCompat(compat)
322+
.navigation();
323+
308324
// 使用绿色通道(跳过所有的拦截器)
309-
ARouter.getInstance().build("/home/main").greenChannal().navigation();
325+
ARouter.getInstance().build("/home/main").greenChannel().navigation();
310326

311327
// 使用自己的日志工具打印日志
312328
ARouter.setLogger();

app/src/main/java/com/alibaba/android/arouter/demo/MainActivity.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@
22

33
import android.app.Activity;
44
import android.content.Intent;
5+
import android.os.Build;
56
import android.os.Bundle;
7+
import android.support.v4.app.ActivityOptionsCompat;
68
import android.support.v7.app.AppCompatActivity;
79
import android.util.Log;
810
import android.view.View;
11+
import android.widget.Toast;
912

1013
import com.alibaba.android.arouter.demo.testinject.TestObj;
1114
import com.alibaba.android.arouter.demo.testinject.TestParcelable;
@@ -64,6 +67,25 @@ public void onClick(View v) {
6467
.withString("key1", "value1")
6568
.navigation();
6669
break;
70+
case R.id.oldVersionAnim:
71+
ARouter.getInstance()
72+
.build("/test/activity2")
73+
.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
74+
.navigation(this);
75+
break;
76+
case R.id.newVersionAnim:
77+
if (Build.VERSION.SDK_INT >= 16) {
78+
ActivityOptionsCompat compat = ActivityOptionsCompat.
79+
makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);
80+
81+
ARouter.getInstance()
82+
.build("/test/activity2")
83+
.withOptionsCompat(compat)
84+
.navigation();
85+
} else {
86+
Toast.makeText(this, "API < 16,不支持新版本动画", Toast.LENGTH_SHORT).show();
87+
}
88+
break;
6789
case R.id.interceptor:
6890
ARouter.getInstance()
6991
.build("/test/activity4")
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android">
3+
<translate
4+
android:duration="2000"
5+
android:fromYDelta="100%p"
6+
android:toYDelta="0"/>
7+
<alpha
8+
android:duration="2000"
9+
android:fromAlpha="0.0"
10+
android:toAlpha="1.0"/>
11+
</set>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<set xmlns:android="http://schemas.android.com/apk/res/android">
3+
<translate
4+
android:duration="2000"
5+
android:fromYDelta="0%p"
6+
android:toYDelta="100%p"/>
7+
<alpha
8+
android:duration="2000"
9+
android:fromAlpha="1.0"
10+
android:toAlpha="0.0"/>
11+
</set>

app/src/main/res/layout/activity_main.xml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,20 @@
9494
android:layout_height="wrap_content"
9595
android:onClick="onClick"
9696
android:text="携带参数的应用内跳转" />
97+
98+
<Button
99+
android:id="@+id/oldVersionAnim"
100+
android:layout_width="match_parent"
101+
android:layout_height="wrap_content"
102+
android:onClick="onClick"
103+
android:text="旧版本转场动画" />
104+
105+
<Button
106+
android:id="@+id/newVersionAnim"
107+
android:layout_width="match_parent"
108+
android:layout_height="wrap_content"
109+
android:onClick="onClick"
110+
android:text="新版本转场动画" />
97111
</LinearLayout>
98112

99113
<LinearLayout

arouter-api/src/main/java/com/alibaba/android/arouter/facade/Postcard.java

Lines changed: 68 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
import android.os.Parcelable;
99
import android.support.annotation.IntDef;
1010
import android.support.annotation.Nullable;
11+
import android.support.annotation.RequiresApi;
12+
import android.support.v4.app.ActivityOptionsCompat;
1113
import android.util.SparseArray;
1214

1315
import com.alibaba.android.arouter.facade.callback.NavigationCallback;
@@ -25,17 +27,35 @@
2527
* A container that contains the roadmap.
2628
*
2729
* @author Alex <a href="mailto:zhilong.liu@aliyun.com">Contact me.</a>
28-
* @version 1.0
30+
* @version 1.1.0
2931
* @since 16/8/22 19:16
3032
*/
3133
public final class Postcard extends RouteMeta {
34+
// Base
3235
private Uri uri;
3336
private Object tag; // A tag prepare for some thing wrong.
34-
private Bundle mBundle; // Data to tranform
37+
private Bundle mBundle; // Data to transform
3538
private int flags = -1; // Flags of route
36-
private int timeout = 300; // Navigation timeout, TimeUnit.Second !
39+
private int timeout = 300; // Navigation timeout, TimeUnit.Second
3740
private IProvider provider; // It will be set value, if this postcard was provider.
38-
private boolean greenChannal;
41+
private boolean greenChannel;
42+
43+
// Animation
44+
private Bundle optionsCompat; // The transition animation of activity
45+
private int enterAnim;
46+
private int exitAnim;
47+
48+
public Bundle getOptionsBundle() {
49+
return optionsCompat;
50+
}
51+
52+
public int getEnterAnim() {
53+
return enterAnim;
54+
}
55+
56+
public int getExitAnim() {
57+
return exitAnim;
58+
}
3959

4060
public IProvider getProvider() {
4161
return provider;
@@ -61,8 +81,8 @@ public Postcard(String path, String group, Uri uri, Bundle bundle) {
6181
this.mBundle = (null == bundle ? new Bundle() : bundle);
6282
}
6383

64-
public boolean isGreenChannal() {
65-
return greenChannal;
84+
public boolean isGreenChannel() {
85+
return greenChannel;
6686
}
6787

6888
public Object getTag() {
@@ -149,12 +169,12 @@ public void navigation(Activity mContext, int requestCode, NavigationCallback ca
149169
}
150170

151171
/**
152-
* Green channal, it will skip all of interceptors.
172+
* Green channel, it will skip all of interceptors.
153173
*
154174
* @return this
155175
*/
156176
public Postcard greenChannel() {
157-
this.greenChannal = true;
177+
this.greenChannel = true;
158178
return this;
159179
}
160180

@@ -540,8 +560,47 @@ public Postcard withBundle(@Nullable String key, @Nullable Bundle value) {
540560
return this;
541561
}
542562

563+
/**
564+
* Set normal transition anim
565+
*
566+
* @param enterAnim enter
567+
* @param exitAnim exit
568+
* @return current
569+
*/
570+
public Postcard withTransition(int enterAnim, int exitAnim) {
571+
this.enterAnim = enterAnim;
572+
this.exitAnim = exitAnim;
573+
return this;
574+
}
575+
576+
/**
577+
* Set options compat
578+
*
579+
* @param compat compat
580+
* @return this
581+
*/
582+
@RequiresApi(16)
583+
public Postcard withOptionsCompat(ActivityOptionsCompat compat) {
584+
if (null != compat) {
585+
this.optionsCompat = compat.toBundle();
586+
}
587+
return this;
588+
}
589+
543590
@Override
544591
public String toString() {
545-
return "Postcard " + super.toString();
592+
return "Postcard{" +
593+
"uri=" + uri +
594+
", tag=" + tag +
595+
", mBundle=" + mBundle +
596+
", flags=" + flags +
597+
", timeout=" + timeout +
598+
", provider=" + provider +
599+
", greenChannel=" + greenChannel +
600+
", optionsCompat=" + optionsCompat +
601+
", enterAnim=" + enterAnim +
602+
", exitAnim=" + exitAnim +
603+
"}\n" +
604+
super.toString();
546605
}
547606
}

arouter-api/src/main/java/com/alibaba/android/arouter/facade/template/IPolicy.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* @version 1.0
88
* @since 16/8/24 10:15
99
*/
10+
@Deprecated
1011
public interface IPolicy {
1112
int getFlag();
1213
}

arouter-api/src/main/java/com/alibaba/android/arouter/launcher/_ARouter.java

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
import android.content.Context;
66
import android.content.Intent;
77
import android.net.Uri;
8+
import android.os.Handler;
9+
import android.os.Looper;
10+
import android.support.v4.app.ActivityCompat;
811
import android.util.Log;
912
import android.widget.Toast;
1013

@@ -24,7 +27,6 @@
2427
import com.alibaba.android.arouter.thread.DefaultPoolExecutor;
2528
import com.alibaba.android.arouter.utils.Consts;
2629
import com.alibaba.android.arouter.utils.DefaultLogger;
27-
2830
import com.alibaba.android.arouter.utils.TextUtils;
2931

3032
import java.lang.reflect.Field;
@@ -290,7 +292,7 @@ protected Object navigation(final Context context, final Postcard postcard, fina
290292
callback.onFound(postcard);
291293
}
292294

293-
if (!postcard.isGreenChannal()) { // It must be run in async thread, maybe interceptor cost too mush time made ANR.
295+
if (!postcard.isGreenChannel()) { // It must be run in async thread, maybe interceptor cost too mush time made ANR.
294296
interceptorService.doInterceptions(postcard, new InterceptorCallback() {
295297
/**
296298
* Continue process
@@ -320,13 +322,12 @@ public void onInterrupt(Throwable exception) {
320322
}
321323

322324
private Object _navigation(final Context context, final Postcard postcard, final int requestCode) {
323-
Context currentContext = null == context ? mContext : context;
325+
final Context currentContext = null == context ? mContext : context;
324326

325327
switch (postcard.getType()) {
326328
case ACTIVITY:
327-
328329
// Build intent
329-
Intent intent = new Intent(currentContext, postcard.getDestination());
330+
final Intent intent = new Intent(currentContext, postcard.getDestination());
330331
intent.putExtras(postcard.getExtras());
331332

332333
// Set flags.
@@ -337,12 +338,21 @@ private Object _navigation(final Context context, final Postcard postcard, final
337338
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
338339
}
339340

340-
// Judgment activity start type.
341-
if (requestCode > 0) { // RequestCode exist, need startActivityForResult, so this context must son of activity.
342-
((Activity) currentContext).startActivityForResult(intent, requestCode);
343-
} else {
344-
currentContext.startActivity(intent);
345-
}
341+
// Navigation in main looper.
342+
new Handler(Looper.getMainLooper()).post(new Runnable() {
343+
@Override
344+
public void run() {
345+
if (requestCode > 0) { // Need start for result
346+
ActivityCompat.startActivityForResult((Activity) currentContext, intent, requestCode, postcard.getOptionsBundle());
347+
} else {
348+
ActivityCompat.startActivity(currentContext, intent, postcard.getOptionsBundle());
349+
}
350+
351+
if ((0 != postcard.getEnterAnim() || 0 != postcard.getExitAnim()) && currentContext instanceof Activity) { // Old version.
352+
((Activity) currentContext).overridePendingTransition(postcard.getEnterAnim(), postcard.getExitAnim());
353+
}
354+
}
355+
});
346356

347357
break;
348358
case PROVIDER:

gradle.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ MIN_SDK_VERSION=12
2525
TARGET_SDK_VERSION=25
2626

2727
arouter_compiler_version=1.0.4
28-
arouter_api_version=1.0.9
28+
arouter_api_version=1.1.0
2929
arouter_annotation_version=1.0.3
3030

3131
bintrayRepo=maven

0 commit comments

Comments
 (0)