ViewAnimationUtils是Android5.0出来的API。其作用就是可以使控件能够呈现水波一样展开。
ViewAnimationUtils 在安装中的源码如下:
public final class ViewAnimationUtils {
private ViewAnimationUtils() {}
/**
* Returns an Animator which can animate a clipping circle.
* <p>
* Any shadow cast by the View will respect the circular clip from this animator.
* <p>
* Only a single non-rectangular clip can be applied on a View at any time.
* Views clipped by a circular reveal animation take priority over
* {@link View#setClipToOutline(boolean) View Outline clipping}.
* <p>
* Note that the animation returned here is a one-shot animation. It cannot
* be re-used, and once started it cannot be paused or resumed. It is also
* an asynchronous animation that automatically runs off of the UI thread.
* As a result {@link AnimatorListener#onAnimationEnd(Animator)}
* will occur after the animation has ended, but it may be delayed depending
* on thread responsiveness.
*
* @param view The View will be clipped to the animating circle.
* @param centerX The x coordinate of the center of the animating circle, relative to
* <code>view</code>.
* @param centerY The y coordinate of the center of the animating circle, relative to
* <code>view</code>.
* @param startRadius The starting radius of the animating circle.
* @param endRadius The ending radius of the animating circle.
*/
public static Animator createCircularReveal(View view,
int centerX, int centerY, float startRadius, float endRadius) {
return new RevealAnimator(view, centerX, centerY, startRadius, endRadius);
}
}
其实源码句一句话。
参数说明:
public static Animator createCircularReveal(View view,
int centerX, int centerY, float startRadius, float endRadius) {
return new RevealAnimator(view, centerX, centerY, startRadius, endRadius);
}
参数1 view: 要实现波纹效果的view。
参数2 centerX: 动画的中心点的x坐标;
参数3 centerY:动画的中心点的y坐标;
参数4 startRadius: 动画开始的波纹半径;
参数5 endRadius:动画结束时的波纹半径;
使用方法
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.zhengjunchen.mytest.MainActivity"
tools:showIn="@layout/activity_main">
<TextView
android:layout_centerInParent="true"
android:background="#aaff0000"
android:gravity="center"
android:id="@+id/tv"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Hello World!"/>
<Button
android:layout_marginTop="10dp"
android:layout_alignStart="@+id/tv"
android:layout_alignEnd="@+id/tv"
android:text="开始动画"
android:id="@+id/btn"
android:layout_below="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView tv;
private Button btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv = (TextView) findViewById(R.id.tv);
btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(this);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
@Override
public void onClick(View v) {
Animator animator = ViewAnimationUtils.createCircularReveal(tv, tv.getMeasuredWidth()/2,
tv.getMeasuredHeight()/2,
0,
(float) Math.sqrt((tv.getMeasuredWidth() * tv.getMeasuredWidth() + tv.getMeasuredHeight() * tv.getMeasuredHeight())));
animator.setDuration(5000);
animator.start();
}
}