Skip to content

Commit

Permalink
Merge pull request #61 from alibaba/develop
Browse files Browse the repository at this point in the history
Add transition animation support.
  • Loading branch information
zhi1ong authored Mar 21, 2017
2 parents 5ca6778 + fc3b2b8 commit 741eab8
Show file tree
Hide file tree
Showing 10 changed files with 183 additions and 23 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
7. 映射关系按组分类、多级管理,按需初始化
8. 支持用户指定全局降级与局部降级策略
9. 页面、拦截器、服务等组件均自动注册到框架
10. 支持多种方式配置转场动画

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

// 转场动画(常规方式)
ARouter.getInstance()
.build("/test/activity2")
.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
.navigation(this);
// 转场动画(API16+)
ActivityOptionsCompat compat = ActivityOptionsCompat.
makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);

ARouter.getInstance()
.build("/test/activity2")
.withOptionsCompat(compat)
.navigation();
// 使用绿色通道(跳过所有的拦截器)
ARouter.getInstance().build("/home/main").greenChannal().navigation();
ARouter.getInstance().build("/home/main").greenChannel().navigation();

// 使用自己的日志工具打印日志
ARouter.setLogger();
Expand Down
18 changes: 17 additions & 1 deletion README_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
7. 映射关系按组分类、多级管理,按需初始化
8. 支持用户指定全局降级与局部降级策略
9. 页面、拦截器、服务等组件均自动注册到框架
10. 支持多种方式配置转场动画

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

// 转场动画(常规方式)
ARouter.getInstance()
.build("/test/activity2")
.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
.navigation(this);
// 转场动画(API16+)
ActivityOptionsCompat compat = ActivityOptionsCompat.
makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);

ARouter.getInstance()
.build("/test/activity2")
.withOptionsCompat(compat)
.navigation();
// 使用绿色通道(跳过所有的拦截器)
ARouter.getInstance().build("/home/main").greenChannal().navigation();
ARouter.getInstance().build("/home/main").greenChannel().navigation();

// 使用自己的日志工具打印日志
ARouter.setLogger();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.alibaba.android.arouter.demo.testinject.TestObj;
import com.alibaba.android.arouter.demo.testinject.TestParcelable;
Expand Down Expand Up @@ -64,6 +67,25 @@ public void onClick(View v) {
.withString("key1", "value1")
.navigation();
break;
case R.id.oldVersionAnim:
ARouter.getInstance()
.build("/test/activity2")
.withTransition(R.anim.slide_in_bottom, R.anim.slide_out_bottom)
.navigation(this);
break;
case R.id.newVersionAnim:
if (Build.VERSION.SDK_INT >= 16) {
ActivityOptionsCompat compat = ActivityOptionsCompat.
makeScaleUpAnimation(v, v.getWidth() / 2, v.getHeight() / 2, 0, 0);

ARouter.getInstance()
.build("/test/activity2")
.withOptionsCompat(compat)
.navigation();
} else {
Toast.makeText(this, "API < 16,不支持新版本动画", Toast.LENGTH_SHORT).show();
}
break;
case R.id.interceptor:
ARouter.getInstance()
.build("/test/activity4")
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/res/anim/slide_in_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromYDelta="100%p"
android:toYDelta="0"/>
<alpha
android:duration="2000"
android:fromAlpha="0.0"
android:toAlpha="1.0"/>
</set>
11 changes: 11 additions & 0 deletions app/src/main/res/anim/slide_out_bottom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:duration="2000"
android:fromYDelta="0%p"
android:toYDelta="100%p"/>
<alpha
android:duration="2000"
android:fromAlpha="1.0"
android:toAlpha="0.0"/>
</set>
14 changes: 14 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,20 @@
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="携带参数的应用内跳转" />

<Button
android:id="@+id/oldVersionAnim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="旧版本转场动画" />

<Button
android:id="@+id/newVersionAnim"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="新版本转场动画" />
</LinearLayout>

<LinearLayout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import android.os.Parcelable;
import android.support.annotation.IntDef;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityOptionsCompat;
import android.util.SparseArray;

import com.alibaba.android.arouter.facade.callback.NavigationCallback;
Expand All @@ -25,17 +27,35 @@
* A container that contains the roadmap.
*
* @author Alex <a href="mailto:[email protected]">Contact me.</a>
* @version 1.0
* @version 1.1.0
* @since 16/8/22 19:16
*/
public final class Postcard extends RouteMeta {
// Base
private Uri uri;
private Object tag; // A tag prepare for some thing wrong.
private Bundle mBundle; // Data to tranform
private Bundle mBundle; // Data to transform
private int flags = -1; // Flags of route
private int timeout = 300; // Navigation timeout, TimeUnit.Second !
private int timeout = 300; // Navigation timeout, TimeUnit.Second
private IProvider provider; // It will be set value, if this postcard was provider.
private boolean greenChannal;
private boolean greenChannel;

// Animation
private Bundle optionsCompat; // The transition animation of activity
private int enterAnim;
private int exitAnim;

public Bundle getOptionsBundle() {
return optionsCompat;
}

public int getEnterAnim() {
return enterAnim;
}

public int getExitAnim() {
return exitAnim;
}

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

public boolean isGreenChannal() {
return greenChannal;
public boolean isGreenChannel() {
return greenChannel;
}

public Object getTag() {
Expand Down Expand Up @@ -149,12 +169,12 @@ public void navigation(Activity mContext, int requestCode, NavigationCallback ca
}

/**
* Green channal, it will skip all of interceptors.
* Green channel, it will skip all of interceptors.
*
* @return this
*/
public Postcard greenChannel() {
this.greenChannal = true;
this.greenChannel = true;
return this;
}

Expand Down Expand Up @@ -540,8 +560,47 @@ public Postcard withBundle(@Nullable String key, @Nullable Bundle value) {
return this;
}

/**
* Set normal transition anim
*
* @param enterAnim enter
* @param exitAnim exit
* @return current
*/
public Postcard withTransition(int enterAnim, int exitAnim) {
this.enterAnim = enterAnim;
this.exitAnim = exitAnim;
return this;
}

/**
* Set options compat
*
* @param compat compat
* @return this
*/
@RequiresApi(16)
public Postcard withOptionsCompat(ActivityOptionsCompat compat) {
if (null != compat) {
this.optionsCompat = compat.toBundle();
}
return this;
}

@Override
public String toString() {
return "Postcard " + super.toString();
return "Postcard{" +
"uri=" + uri +
", tag=" + tag +
", mBundle=" + mBundle +
", flags=" + flags +
", timeout=" + timeout +
", provider=" + provider +
", greenChannel=" + greenChannel +
", optionsCompat=" + optionsCompat +
", enterAnim=" + enterAnim +
", exitAnim=" + exitAnim +
"}\n" +
super.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* @version 1.0
* @since 16/8/24 10:15
*/
@Deprecated
public interface IPolicy {
int getFlag();
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Looper;
import android.support.v4.app.ActivityCompat;
import android.util.Log;
import android.widget.Toast;

Expand All @@ -24,7 +27,6 @@
import com.alibaba.android.arouter.thread.DefaultPoolExecutor;
import com.alibaba.android.arouter.utils.Consts;
import com.alibaba.android.arouter.utils.DefaultLogger;

import com.alibaba.android.arouter.utils.TextUtils;

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

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

private Object _navigation(final Context context, final Postcard postcard, final int requestCode) {
Context currentContext = null == context ? mContext : context;
final Context currentContext = null == context ? mContext : context;

switch (postcard.getType()) {
case ACTIVITY:

// Build intent
Intent intent = new Intent(currentContext, postcard.getDestination());
final Intent intent = new Intent(currentContext, postcard.getDestination());
intent.putExtras(postcard.getExtras());

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

// Judgment activity start type.
if (requestCode > 0) { // RequestCode exist, need startActivityForResult, so this context must son of activity.
((Activity) currentContext).startActivityForResult(intent, requestCode);
} else {
currentContext.startActivity(intent);
}
// Navigation in main looper.
new Handler(Looper.getMainLooper()).post(new Runnable() {
@Override
public void run() {
if (requestCode > 0) { // Need start for result
ActivityCompat.startActivityForResult((Activity) currentContext, intent, requestCode, postcard.getOptionsBundle());
} else {
ActivityCompat.startActivity(currentContext, intent, postcard.getOptionsBundle());
}

if ((0 != postcard.getEnterAnim() || 0 != postcard.getExitAnim()) && currentContext instanceof Activity) { // Old version.
((Activity) currentContext).overridePendingTransition(postcard.getEnterAnim(), postcard.getExitAnim());
}
}
});

break;
case PROVIDER:
Expand Down
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ MIN_SDK_VERSION=12
TARGET_SDK_VERSION=25

arouter_compiler_version=1.0.4
arouter_api_version=1.0.9
arouter_api_version=1.1.0
arouter_annotation_version=1.0.3

bintrayRepo=maven
Expand Down

0 comments on commit 741eab8

Please sign in to comment.