Ish*_*arg 67 animation android material-design
根据https://developer.android.com/training/material/animations.html
通过该
ViewAnimationUtils.createCircularReveal()
方法,您可以设置剪贴圆的动画以显示或隐藏视图.要使用此效果显示以前不可见的视图:
Run Code Online (Sandbox Code Playgroud)// previously invisible view View myView = findViewById(R.id.my_view); // get the center for the clipping circle int cx = (myView.getLeft() + myView.getRight()) / 2; int cy = (myView.getTop() + myView.getBottom()) / 2; // get the final radius for the clipping circle int finalRadius = Math.max(myView.getWidth(), myView.getHeight()); // create the animator for this view (the start radius is zero) Animator anim = ViewAnimationUtils.createCircularReveal(myView, cx, cy, 0, finalRadius); // make the view visible and start the animation myView.setVisibility(View.VISIBLE); anim.start();
这是为了揭示一个观点.如何使用它循环显示整个活动,没有任何共享元素?
具体来说,我希望我的searchActivity从工具栏中的搜索操作按钮循环显示.
Ste*_*ack 86
在寻找一个没有结果的解决方案半天之后,我想出了一个自己的实现.我正在使用具有匹配根布局的透明活动.根布局是一个可以随后显示的视图createCircularReveal()
.
我的代码看起来像这样:
styles.xml中的主题定义
<style name="Theme.Transparent" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowBackground">@android:color/transparent</item>
</style>
Run Code Online (Sandbox Code Playgroud)
AndroidManifest.xml中的Activity定义
<activity
android:name=".ui.CircularRevealActivity"
android:theme="@style/Theme.Transparent"
android:launchMode="singleTask"
/>
Run Code Online (Sandbox Code Playgroud)
然后我为我的活动宣布了一个布局(我选择了DrawerLayout,这样我就可以拥有一个NavDrawer.每个布局都应该在这里工作.)
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawer_layout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="@+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/honey_melon"
>
<!-- Insert your actual layout here -->
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
Run Code Online (Sandbox Code Playgroud)
重要的是具有id的FrameLayout root_layout
.此视图将在活动中显示.
最后我实施CircularRevealActivity
并覆盖了onCreate()
:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.do_not_move, R.anim.do_not_move);
setContentView(R.layout.activity_reveal_circular);
if (savedInstanceState == null) {
rootLayout.setVisibility(View.INVISIBLE);
ViewTreeObserver viewTreeObserver = rootLayout.getViewTreeObserver();
if (viewTreeObserver.isAlive()) {
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
circularRevealActivity();
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
rootLayout.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
circularRevealActivity()
放入a 非常重要OnGlobalLayoutListener
,因为需要为动画绘制视图.
circularRevealActivity()
看起来像Ishaan的提议:
private void circularRevealActivity() {
int cx = rootLayout.getWidth() / 2;
int cy = rootLayout.getHeight() / 2;
float finalRadius = Math.max(rootLayout.getWidth(), rootLayout.getHeight());
// create the animator for this view (the start radius is zero)
Animator circularReveal = ViewAnimationUtils.createCircularReveal(rootLayout, cx, cy, 0, finalRadius);
circularReveal.setDuration(1000);
// make the view visible and start the animation
rootLayout.setVisibility(View.VISIBLE);
circularReveal.start();
}
Run Code Online (Sandbox Code Playgroud)
编辑1
R.anim.do_not_move
添加了定义.但是,如果您的设计没有为活动指定默认转换,它也应该在没有该行的情况下工作.让我知道
R.anim.do_not_move:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
android:fromYDelta="0"
android:toYDelta="0"
android:duration="@android:integer/config_mediumAnimTime"
/>
</set>
Run Code Online (Sandbox Code Playgroud)
要反转CircularReveal
动画,请交换startRadius
和endRadius
参数.此外,您还需要设置一个AnimatorListener
和在onAnimationEnd()
回调方法中可以调用的地方finishAfterTransition()
.这是当您按up navigation
或单击时back button
.
小智 8
如果要在离开活动时反转循环显示,请对onBackPressed()使用以下修改.
@Override
public void onBackPressed() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
int cx = rootLayout.getWidth();
int cy = 0;
float finalRadius = Math.max(rootLayout.getWidth(), rootLayout.getHeight());
Animator circularReveal = ViewAnimationUtils.createCircularReveal(rootLayout, cx, cy, finalRadius, 0);
circularReveal.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animator) {
}
@Override
public void onAnimationEnd(Animator animator) {
rootLayout.setVisibility(View.INVISIBLE);
finish();
}
@Override
public void onAnimationCancel(Animator animator) {
}
@Override
public void onAnimationRepeat(Animator animator) {
}
});
circularReveal.setDuration(400);
circularReveal.start();
}else{
super.onBackPressed();
}
}
Run Code Online (Sandbox Code Playgroud)
我想你可以用ActivityOptionsCompat.makeClipRevealAnimation
.
[ https://developer.android.com/reference/android/support/v4/app/ActivityOptionsCompat.html#makeClipRevealAnimation(android.view.View,INT,INT,INT,INT)](https://developer.android .com/reference/android/support/v4/app/ActivityOptionsCompat.html#makeClipRevealAnimation(android.view.View,int,int,int,int))
归档时间: |
|
查看次数: |
40099 次 |
最近记录: |