How programmatically apply translate & scale anima

2020-06-25 04:15发布

问题:

How apply translate animation with scale animation on a view. I have to move a view to another view location with zoom out simultaneously.

How scale a view from its position to another view position (Second view is not fixed)?

startView - view that translate

finishView - where animation finish.

**Code **

  private void startAnimation(View startView, View finishView) {

    int startX = startView.getLeft() + startView.getWidth() / 2;
    int startY = startView.getTop() + startView.getHeight() / 2;
    int startViewLocation[]=new int[2];
    startView.getLocationInWindow(startViewLocation);
    int finishViewLocation[]=new int[2];
    finishView.getLocationInWindow(finishViewLocation);
    int endX = finishViewLocation[0];
    int endY=finishViewLocation[1];
    System.out.println("statX " + startX + " " + (startView.getLeft() + startView.getWidth() / 2));
    System.out.println("statY " + startY+" "+(startView.getTop() + startView.getHeight() / 2));

    System.out.println("endX " + endX+" "+finishViewLocation[0]);
    System.out.println("endY " + endY+" "+finishViewLocation[1]);

    TranslateAnimation translateAnimation = new TranslateAnimation(0, 0,
            TranslateAnimation.ABSOLUTE, endX, 0, 0,
            TranslateAnimation.ABSOLUTE, endY);
    ScaleAnimation scaleAnimation = new ScaleAnimation(1.0f, 0.0f, 1.0f,
            0.0f);
    AnimationSet set = new AnimationSet(true);

    set.addAnimation(translateAnimation);
    set.addAnimation(scaleAnimation);
    set.setFillAfter(true);
    set.setDuration(2000);
    set.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            // TODO Auto-generated method stub

        }
    });
    startView.startAnimation(set);

}

回答1:

private void scaleAnimation(final View startView, View finishView,final TextView cartItems) {
    int startViewLocation[] = new int[2];
    startView.getLocationInWindow(startViewLocation);
    int finishViewLocation[] = new int[2];
    finishView.getLocationInWindow(finishViewLocation);
    int startX = startViewLocation[0] + startView.getWidth() / 2;
    int startY = startViewLocation[1] + startView.getHeight() / 2;
    int endX = finishViewLocation[0] + finishView.getWidth() / 2;
    int endY = finishViewLocation[1] + finishView.getHeight() / 2;
    ScaleAnimation animation = new ScaleAnimation(1f, 0f, 1, 0f,
            Animation.ABSOLUTE, endX - startX + startView.getWidth() / 2,
            Animation.ABSOLUTE, endY - startY + startView.getHeight() / 2);
    // animation.scaleCurrentDuration(6000);
    animation.setDuration(2000);
    // animation.setStartOffset(50);
    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            startView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            startView.setVisibility(View.GONE);
            cartItems.setText(String.valueOf(totlaCartsItems));
        }
    });
    startView.startAnimation(animation);
}


回答2:

Scale animation alone is enough to get that feel, while expanding from a point to a point it gives translation effect too. Try this code,

  ScaleAnimation animation = new ScaleAnimation(1, 1, 0, 1, 1,
                    Animation.RELATIVE_TO_SELF, Animation.RELATIVE_TO_SELF, 0f);
                animation.scaleCurrentDuration(6000);
                animation.setDuration(300);
                animation.setStartOffset(50);
                animation.setAnimationListener(new AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {

                    }
                });
                    view.startAnimation(animation);

this code scales the view from top to bottom.