Android 如何设置动画时,我点击按钮,在那个时候移动图像从左到右,从右到左在安卓?

Android 如何设置动画时,我点击按钮,在那个时候移动图像从左到右,从右到左在安卓?,android,Android,当我点击按钮时,我想翻译两幅图像,在使用android动画的模拟器上,从左图像向左移动到右,从右图像向右移动到左(此时两幅图像都在中间,停止动画)。我是android动画的新手。我该怎么做。? 非常感谢您的编码。谢谢。我不能在这里解释一切,但我可以帮助您做到这一点。首先,必须为每个图像创建两个新的XML文件(旧版本的动画或新版本的Tween动画),然后使用translate。例如: <translate android:fromXDelta="10" android:toXDelt

当我点击按钮时,我想翻译两幅图像,在使用android动画的模拟器上,从左图像向左移动到右,从右图像向右移动到左(此时两幅图像都在中间,停止动画)。我是android动画的新手。我该怎么做。?
非常感谢您的编码。谢谢。

我不能在这里解释一切,但我可以帮助您做到这一点。首先,必须为每个图像创建两个新的XML文件(旧版本的动画或新版本的Tween动画),然后使用translate。例如:

<translate android:fromXDelta="10"
    android:toXDelta="100"
    android:duration="2000"    
    />
在本例中,我为这两个动画使用了一个动画。
请记住,在本网站中,您可以从其他而非绝对代码获得帮助。

将两个xml文件添加到项目的
res/anim
文件夹中,如下所示:

lefttoright.xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<translate android:fromXDelta="-600%" android:toXDelta="0%"
    android:fromYDelta="0%" android:toYDelta="0%" android:duration="300"
    android:zAdjustment="bottom">
</translate>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<translate android:fromXDelta="0%" android:toXDelta="400%"
    android:fromYDelta="0%" android:toYDelta="0%" android:duration="300"
    android:zAdjustment="bottom">
</translate>

在布局文件中,在屏幕左侧保留一个
ImageView
,在屏幕右侧保留另一个

在按钮的onClick事件中使用以下代码段:

lefttoright = AnimationUtils.loadAnimation(context,
            R.anim.lefttoright);
    righttoleft = AnimationUtils.loadAnimation(context,
            R.anim.righttoleft);
imageView1.startAnimation(lefttoright);
imageView2.startAnimation(righttoleft);
然后实现动画侦听器:

lefttoright.setAnimationListener(new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // TODO Auto-generated method stub
                        //update imageView's position (e.g. center)
                    }
                });
右侧到左侧执行相同的操作


希望有帮助。

lefttoright=AnimationUtils.loadAnimation(context,R.anim.lefttoright);这里剩下的是什么对不起,忘记定义类型了
lefttoright
是一个
Animation
类型变量。如果这是您的答案,您必须接受它。这是我们回答你问题的理由。
lefttoright.setAnimationListener(new Animation.AnimationListener() {

                    @Override
                    public void onAnimationStart(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {
                        // TODO Auto-generated method stub

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        // TODO Auto-generated method stub
                        //update imageView's position (e.g. center)
                    }
                });