【问题标题】:Top to bottom - translate animation从上到下 - 翻译动画
【发布时间】:2013-03-18 17:13:07
【问题描述】:

需要制作下一个动画(android 2.2及以上):

1.从上到下移动按钮(点击他之后),

2.从下往上移动(再次点击他之后)。

第一个动画效果很好,但第二个动画效果不好,btn 从下到上“跳跃”而不是动画。

代码:

public class MainActivity extends Activity {

static RelativeLayout relativeLayout;
static Button btn;
static Boolean isUp = true;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn = (Button) findViewById(R.id.button1);
    relativeLayout = (RelativeLayout) findViewById(R.id.relative_layout);

    btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(isUp){
                isUp = false;
                v.startAnimation(MainActivity.getVerticalSlideAnimation(0,relativeLayout.getBottom() - v.getHeight(),500,0));
            }else{
                isUp = true;
                v.startAnimation(MainActivity.getVerticalSlideAnimation(relativeLayout.getBottom() - v.getHeight(),0,500,0));
            }
        }
    });
}


public static Animation getVerticalSlideAnimation(int fromYPosition, final int toYPosition, int duration, int startOffset)
{
  TranslateAnimation translateAnimation = new TranslateAnimation(1, 0.0F, 1, 0.0F, 0, fromYPosition, 0, toYPosition);
  translateAnimation.setDuration(duration);
  translateAnimation.setInterpolator(new AccelerateInterpolator());
  translateAnimation.setStartOffset(startOffset);

  //Stop animation after finishing.
  //translateAnimation.setFillAfter(true);

  translateAnimation.setAnimationListener(new AnimationListener() 
  {
    public void onAnimationStart(Animation animation) { }
    public void onAnimationRepeat(Animation animation) { }
    public void onAnimationEnd(Animation animation) {
        btn.setY(toYPosition);          
    }
  });

  return translateAnimation;
    }
}

布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/relative_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="Button" />

</RelativeLayout>

【问题讨论】:

    标签: android animation translate-animation


    【解决方案1】:

    好的,我解决了。

    关于动画你应该知道的几个问题:

    1. 动画参数并不像您想象的那样简单“从(固定位置)”->“到(固定位置)”。还有更多类似“从(当前位置/0)”->“要走多少步以及在哪个方向(加号为正/减号为负)”

    2. 动画不会改变视图在屏幕上的真实位置,因此如果你想在结束位置停止动画,你应该使用:

      animation.setFillAfter(true);
      
    3. 如果您确实想更改视图的真实位置,您应该更新“onAnimationEnd”上的视图参数(如下面的代码),或者手动计算位置并设置 Y/X 位置(再次在“onAnimationEnd”上),例如:

      animatedView.setY(stopPosition);
      

    代码:

        public class AnimationActivity extends Activity {
    
        private boolean isUp;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        ((Button) findViewById(R.id.button1))
                .setOnClickListener(new OnClickListener() {
    
                    public void onClick(final View v) {
    
                        final float direction = (isUp) ? -1 : 1;
                        final float yDelta = getScreenHeight() - (2 * v.getHeight());
                        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM;
    
                        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction);
    
                        animation.setDuration(500);
    
                        animation.setAnimationListener(new AnimationListener() {
    
                            public void onAnimationStart(Animation animation) {
                            }
    
                            public void onAnimationRepeat(Animation animation) {
                            }
    
                            public void onAnimationEnd(Animation animation) {
    
                                // fix flicking
                                // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker
                                TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
                                anim.setDuration(1);
                                v.startAnimation(anim);
    
    
                                //set new params
                                LayoutParams params = new LayoutParams(v.getLayoutParams());
                                params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                                params.addRule(layoutTopOrBottomRule);
                                v.setLayoutParams(params);
                            }
                        });
    
                        v.startAnimation(animation);
    
                        //reverse direction
                        isUp = !isUp;
                    }
                });
    }
    
    private float getScreenHeight() {
    
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return (float) displaymetrics.heightPixels;
    
    }
    

    }

    【讨论】:

    • 这真的很有帮助。我想知道如何将视图从上到下连续移动并在每次从每一步移动到下一步时获取其位置。如何使用动画实现它?他们还有其他方法吗?我的最终目的是检测移动视图和该视图之间的碰撞?感谢您的帮助
    【解决方案2】:
        public class AnimationActivity extends Activity {
    
        private boolean isUp;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    
        ((Button) findViewById(R.id.button1))
                .setOnClickListener(new OnClickListener() {
    
                    public void onClick(final View v) {
    
                        final float direction = (isUp) ? -1 : 1;
                        final float yDelta = getScreenHeight() - (2 * v.getHeight());
                        final int layoutTopOrBottomRule = (isUp) ? RelativeLayout.ALIGN_PARENT_TOP : RelativeLayout.ALIGN_PARENT_BOTTOM;
    
                        final Animation animation = new TranslateAnimation(0,0,0, yDelta * direction);
    
                        animation.setDuration(500);
    
                        animation.setAnimationListener(new AnimationListener() {
    
                            public void onAnimationStart(Animation animation) {
                            }
    
                            public void onAnimationRepeat(Animation animation) {
                            }
    
                            public void onAnimationEnd(Animation animation) {
    
                                // fix flicking
                                // Source : http://stackoverflow.com/questions/9387711/android-animation-flicker
                                TranslateAnimation anim = new TranslateAnimation(0.0f, 0.0f, 0.0f, 0.0f);
                                anim.setDuration(1);
                                v.startAnimation(anim);
    
    
                                //set new params
                                LayoutParams params = new LayoutParams(v.getLayoutParams());
                                params.addRule(RelativeLayout.CENTER_HORIZONTAL);
                                params.addRule(layoutTopOrBottomRule);
                                v.setLayoutParams(params);
                            }
                        });
    
                        v.startAnimation(animation);
    
                        //reverse direction
                        isUp = !isUp;
                    }
                });
    }
    
    private float getScreenHeight() {
    
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return (float) displaymetrics.heightPixels;
    
    }
    

    【讨论】:

    • 您可以简单地设置 onClickListener 而无需转换为 Button
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-13
    • 2014-10-03
    • 2013-08-14
    • 2011-09-18
    • 1970-01-01
    相关资源
    最近更新 更多