ViewAnimationUtils 的使用

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();
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章