How programmatically apply translate & scale animation simultaneously on a view

此生再无相见时 提交于 2019-12-03 21:14:18
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);
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!