【发布时间】:2020-08-11 16:51:49
【问题描述】:
我想使用 android 动画在模拟器上从左到右翻译图像。我是android动画的新手。我怎么能那样做?
谢谢。
【问题讨论】:
我想使用 android 动画在模拟器上从左到右翻译图像。我是android动画的新手。我怎么能那样做?
谢谢。
【问题讨论】:
ll = new LinearLayout(this);
ll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
ll.setOrientation(LinearLayout.VERTICAL);
tv = new TextView(this);
tv.setText("Animation");
moveLefttoRight = new TranslateAnimation(0, 200, 0, 0);
moveLefttoRight.setDuration(1000);
moveLefttoRight.setFillAfter(true);
button = new Button(this);
button.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
button.setText("PressMe");
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
tv.startAnimation(moveLefttoRight);
}
});
ll.addView(tv);
ll.addView(button);
setContentView(ll);
是一种方法。
【讨论】:
Animation.setDuration(1000) 和Animation.setFillAfter(true)。
使用 Android TranslateAnimation 从左到右和从右到左移动图像
ImageView img_animation = (ImageView) findViewById(R.id.img_animation);
TranslateAnimation animation = new TranslateAnimation(0.0f, 400.0f,
0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
animation.setDuration(5000); // animation duration
animation.setRepeatCount(5); // animation repeat count
animation.setRepeatMode(2); // repeat animation (left to right, right to left )
//animation.setFillAfter(true);
img_animation.startAnimation(animation); // start animation
【讨论】:
我参加聚会有点晚了,但值得在这里回答,
案例 1:
如果您的视图位于屏幕左侧,并且您想从左边缘移动到右边缘,请使用以下命令:
imageView.animate()
.translationX(((rootLayout.width - (imageView.width))).toFloat())
.setInterpolator(AccelerateInterpolator()).duration = 1500
案例 2: 如果您的视图位于屏幕的中心,并且您想从中心移动到右边缘,请使用:
imageView.animate()
.translationX(((rootLayout.width - (imageView.width)) / 2).toFloat())
.setInterpolator(AccelerateInterpolator()).duration = 1500
注意:
rootLayout 是您的 XML 的根视图
【讨论】:
添加此代码 R.anim 文件夹
<?xml version="1.0" encoding="utf-8"?>
<set
xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%p"
android:toXDelta="100%p"
android:duration="800" />
</set>
【讨论】: