从上到下 - 翻译动画

2024-03-24

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

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

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

第一个动画工作正常,但第二个动画不行,btn 从下到上“跳跃”并且没有动画。

Code:

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

Layout:

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

好的,我解决了。

关于动画,您应该了解以下几个问题:

  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;

}

}

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

从上到下 - 翻译动画 的相关文章

随机推荐