5

需要制作下一个动画(在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>
4

2 回答 2

10

好的,我解决了。

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

  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;

}

}

于 2012-10-21T09:28:57.203 回答
-1
    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;

}
于 2013-03-18T08:57:13.583 回答