我目前正在创建一款Android应用程序,用户可以输入他们的姓名,按下按钮,然后只是将他们的姓名输出回来。
我想要实现的一个效果是,在他们按下按钮之后,输入和按钮会消失(完成到这一步),然后MainActivity视图的背景颜色将产生涟漪效果(从中心开始)并且变成新的颜色,最终改变整个背景颜色。
由于我只能找到关于在按下按钮时添加涟漪效果的教程,那么我该如何在代码中实现这个效果呢?
我目前正在创建一款Android应用程序,用户可以输入他们的姓名,按下按钮,然后只是将他们的姓名输出回来。
我想要实现的一个效果是,在他们按下按钮之后,输入和按钮会消失(完成到这一步),然后MainActivity视图的背景颜色将产生涟漪效果(从中心开始)并且变成新的颜色,最终改变整个背景颜色。
由于我只能找到关于在按下按钮时添加涟漪效果的教程,那么我该如何在代码中实现这个效果呢?
编辑:
我通过制作一个小应用程序进行了测试。
首先隐藏您想要在此动画中显示的视图。
该视图可以来自相同的布局,并且在xml中其可见性应为不可见,以便动画将其显示。
如果您想创建全屏动画,则可以将视图高度和宽度设置为match parent。
将原始视图和要显示的视图都放在帧布局中。
在我的情况下,我使用了这个:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView android:text="Hello World!"
android:layout_width="wrap_content"
android:textSize="20sp"
android:layout_height="wrap_content" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimaryDark"
android:id="@+id/revealiew"
android:visibility="invisible"
>
</FrameLayout>
然后在您的活动中,当按钮点击
或某些事件发生时,请执行以下操作:
fab.setOnClickListener(new View.OnClickListener() {
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View view) {
// previously invisible view
View myView = findViewById(R.id.revealview);
// get the center for the clipping circle
int cx = myView.getWidth() / 2;
int cy = myView.getHeight() / 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);
//Interpolator for giving effect to animation
anim.setInterpolator(new AccelerateDecelerateInterpolator());
// Duration of the animation
anim.setDuration(1000);
// make the view visible and start the animation
myView.setVisibility(View.VISIBLE);
anim.start();
}
});
}
您可以在此处查看官方文档的详细信息: http://developer.android.com/training/material/animations.html
您所描述的是背景上的揭示效果。
从官方文档中,您可以找到现成的示例:
1)以下是如何使用揭示效果来显示先前不可见的视图:
// previously invisible view
View myView = findViewById(R.id.my_view);
// get the center for the clipping circle
int cx = myView.getWidth() / 2;
int cy = myView.getHeight() / 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();
2) 这是如何使用揭示效果隐藏先前可见的视图:
// previously visible view
final View myView = findViewById(R.id.my_view);
// get the center for the clipping circle
int cx = myView.getWidth() / 2;
int cy = myView.getHeight() / 2;
// get the initial radius for the clipping circle
int initialRadius = myView.getWidth();
// create the animation (the final radius is zero)
Animator anim =
ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, 0);
// make the view invisible when the animation is done
anim.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
myView.setVisibility(View.INVISIBLE);
}
});
// start the animation
anim.start();
在您的应用程序中,您可以使用一个带有彩色背景层(一开始是不可见的),然后在其上使用揭示效果。
请查看这个网站,“Android Ripple Background”是一个库,可以实现此功能,最低SDK版本为11(Android 3.0 Honeycomb)
https://android-arsenal.com/details/1/1107