从上到下-翻译动画 [英] Top to bottom - translate animation

查看:101
本文介绍了从上到下-翻译动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要制作下一个动画(在Android 2.2及更高版本上):

Need to make next animation (on android 2.2 and above):

1。上下移动按钮(单击鼠标后),

1.Moving button from top to bottom (after clicking on him),

2。从底部移到顶部(再次单击他之后)。

2.Moving back from bottom to top (After clicking on him again).

第一个动画效果很好,但是第二个效果很好

First animation works fine, but the second not, the btn "jumps" from bottom to top and not animate.

代码:

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>


推荐答案

好,我解决了。

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

There are few issuses you should know about animation:


  1. 动画参数不是您应该想到的简单的从(固定位置)->至(固定位置)。还有更多类似从(当前位置/ 0)开始->要执行多少步骤以及在哪个方向上执行(正号为正/负号为负)

  1. The animation paremeters are not simple "From (fixed position)" --> "To (fix position)" as you should think. There are more like "From (current position/0)" --> "How much steps to do and on which direction (pluse for positive/ minus for negative)"

动画不会更改屏幕上视图的实际位置,因此,如果要在结束位置停止动画,则应使用:

The Animation doesn't change the real position of the view on the screen, Therefore if you want to stop the animation at the end position, you should use:

animation.setFillAfter(true);


  • 如果确实要更改视图的REAL位置,则应更新视图参数在 onAnimationEnd上(如下面的代码),或手动计算位置并设置Y / X位置(同样在 onAnimationEnd上),例如:

  • If you do want to change the REAL position of the view you should update the view parameters on "onAnimationEnd" (like below code), or calculate position and set Y/X position manually (again on "onAnimationEnd"), like:

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

    }

    这篇关于从上到下-翻译动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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