Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 自上而下-平移动画_Android_Animation_Translate Animation - Fatal编程技术网

Android 自上而下-平移动画

Android 自上而下-平移动画,android,animation,translate-animation,Android,Animation,Translate Animation,需要制作下一个动画(在android 2.2及以上版本上): 1.从上到下移动按钮(点击按钮后) 2.从下往上移动(再次点击他之后) 第一个动画效果很好,但第二个不行,btn从底部“跳”到顶部,而不是动画 代码: 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+

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

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

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

第一个动画效果很好,但第二个不行,btn从底部“跳”到顶部,而不是动画

代码:

布局:

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

好的,我解决了

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

  • 动画参数并不像您想象的那样是简单的“从(固定位置)”-->“到(固定位置)”。更像是“从(当前位置/0)”-->“要走多少步,朝哪个方向走(加号表示正/减号表示负)”

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

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

  • 守则:

        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,这非常有用。我想知道如何连续地从上到下移动视图,并在视图从每一步移动到下一步时获取其位置。如何使用动画实现它?他们还有其他方法吗?我的最终目的是检测移动视图和此视图之间的冲突?谢谢你的帮助
    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;
    
    }
    
        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;
    
    }