android揭示动画混蛋观察者

时间:2015-12-19 06:33:50

标签: android-animation android-5.0-lollipop

您好我正在尝试在Android棒棒糖中做简单的揭示动画。但是在动画的开始和结束时都会观察到突然的混蛋。你能说出如何克服这一点吗?我尝试增加和减少动画持续时间,但它似乎没有工作。

    package in.ashish29agre.revealfab;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewAnimationUtils;

public class MainActivity extends AppCompatActivity {

    private boolean isViewVisible = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(isViewVisible) {
                    hide(fab, findViewById(R.id.reveal_view));
                    isViewVisible = false;
                } else {
                    show(fab, findViewById(R.id.reveal_view));
                    isViewVisible = true;
                }
            }
        });
    }

    private void show(final View view, final View layoutView) {
        // get the center for the clipping circle
        int cx = (view.getLeft() + view.getRight()) / 2;
        int cy = (view.getTop() + view.getBottom()) / 2;

        // get the final radius for the clipping circle
        int finalRadius = Math.max(layoutView.getWidth(), layoutView.getHeight());

        // create the animator for this view (the start radius is zero)
        Animator anim = ViewAnimationUtils.createCircularReveal(layoutView, cx, cy,
                0, finalRadius);
        anim.setDuration(2000);

        // make the view visible and start the animation
        layoutView.setVisibility(View.VISIBLE);
        anim.start();
    }

    // To hide a previously visible view using this effect:
    private void hide(final View view, final View layoutView) {

        // get the center for the clipping circle
        int cx = (view.getLeft() + view.getRight()) / 2;
        int cy = (view.getTop() + view.getBottom()) / 2;

        // get the initial radius for the clipping circle
        int initialRadius = layoutView.getWidth();

        // create the animation (the final radius is zero)
        Animator anim = ViewAnimationUtils.createCircularReveal(layoutView, cx, cy,
                initialRadius, 0);
        anim.setDuration(1000);

        // make the view invisible when the animation is done
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                layoutView.setVisibility(View.INVISIBLE);
            }
        });

        // start the animation
        anim.start();
    }
}

1 个答案:

答案 0 :(得分:0)

揭示动画基于视图本身的坐标。如果这个混蛋是突然部分隐藏的同时消失并突然显示出来的话,那就是你的罪魁祸首。使用:

float cx = getWidth() / 2f;
float cy = getHeight() / 2f;
float radius = (float)Math.sqrt(cx * cx + cy * cy);