Циклическое раскрытие перехода активности (неправильная позиция) - PullRequest
0 голосов
/ 27 января 2020

Я хочу переключаться между действиями из приложения android. Я думал, что лучший вариант в этом - это круговая анимация. После некоторых исследований я нашел некоторые ресурсы. Это работает очень хорошо, но я хочу, чтобы он открывался именно там, где я нажимаю кнопку. Анимация перехода открывается из центра и верхней части экрана. Я думаю, что позиция также имеет проблему.

Fab Button Listener Listener

fabButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        startRevealActivity(v);
    }
});


   private void startRevealActivity(View v) {
    //calculates the center of the View v you are passing
    int revealX = (int) (v.getX() + v.getWidth() / 2); // I think the problem is here
    int revealY = (int) (v.getY() + v.getHeight() / 2);

    //create an intent, that launches the second activity and pass the x and y coordinates
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra(RevealAnimation.EXTRA_CIRCULAR_REVEAL_X, revealX);
    intent.putExtra(RevealAnimation.EXTRA_CIRCULAR_REVEAL_Y, revealY);

    //just start the activity as an shared transition, but set the options bundle to null
    ActivityCompat.startActivity(this, intent, null);

    //to prevent strange behaviours override the pending transitions
    overridePendingTransition(0, 0);
}

Класс анимации

    public class RevealAnimation {
    public static final String EXTRA_CIRCULAR_REVEAL_X = "EXTRA_CIRCULAR_REVEAL_X";
    public static final String EXTRA_CIRCULAR_REVEAL_Y = "EXTRA_CIRCULAR_REVEAL_Y";

    private final View mView;
    private Activity mActivity;

    private int revealX;
    private int revealY;

    public RevealAnimation(View view, Intent intent, Activity activity) {
        mView = view;
        mActivity = activity;

        //when you're android version is at leat Lollipop it starts the reveal activity
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
                intent.hasExtra(EXTRA_CIRCULAR_REVEAL_X) &&
                intent.hasExtra(EXTRA_CIRCULAR_REVEAL_Y)) {
            view.setVisibility(View.INVISIBLE);

            revealX = intent.getIntExtra(EXTRA_CIRCULAR_REVEAL_X, 0);
            revealY = intent.getIntExtra(EXTRA_CIRCULAR_REVEAL_Y, 0);

            ViewTreeObserver viewTreeObserver = view.getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        revealActivity(revealX, revealY);
                        mView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                });
            }
        } else {

            //if you are below android 5 it jist shows the activity
            view.setVisibility(View.VISIBLE);
        }
    }

    public void revealActivity(int x, int y) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            float finalRadius = (float) (Math.max(mView.getWidth(), mView.getHeight()) * 1.1);

            // create the animator for this view (the start radius is zero)
            Animator circularReveal = ViewAnimationUtils.createCircularReveal(mView, x, y, 0, finalRadius);
            circularReveal.setDuration(300);
            circularReveal.setInterpolator(new AccelerateInterpolator());

            // make the view visible and start the animation
            mView.setVisibility(View.VISIBLE);
            circularReveal.start();
        } else {
            mActivity.finish();
        }
    }

    public void unRevealActivity() {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
            mActivity.finish();
        } else {
            float finalRadius = (float) (Math.max(mView.getWidth(), mView.getHeight()) * 1.1);
            Animator circularReveal = ViewAnimationUtils.createCircularReveal(
                    mView, revealX, revealY, finalRadius, 0);

            circularReveal.setDuration(300);
            circularReveal.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mView.setVisibility(View.INVISIBLE);
                    mActivity.finish();
                    mActivity.overridePendingTransition(0, 0);
                }
            });

            circularReveal.start();
        }
    }
}

SecondActivity

RevealAnimation mRevealAnimation;

@Override
protected void onCreate(Bundle savedInstanceState) {

   Intent intent = this.getIntent();   //get the intent to recieve the x and y coords, that you passed before

   LinearLayout rootLayout = (LinearLayout) findViewById(R.id.root_layout); //there you have to get the root layout of your second activity
   mRevealAnimation = new RevealAnimation(rootLayout, intent, this);
}

Исходный код

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...