如何以编程方式应用翻译和在视图上同时缩放动画 [英] How programmatically apply translate & scale animation simultaneously on a view

查看:98
本文介绍了如何以编程方式应用翻译和在视图上同时缩放动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在视图上应用带有比例动画的平移动画。我必须将视图同时缩小到另一个视图位置。

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-翻译的视图

startView - view that translate

finishView-动画结束的位置。

finishView - where animation finish.

**代码**

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

}


推荐答案

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

这篇关于如何以编程方式应用翻译和在视图上同时缩放动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆