如何在android中使用翻译动画连续上下移动图像?

20 animation android

我已成功完成一侧动画,Translate Animation意味着图像从顶部到底部.这是代码:

private ImageView mScanner;
private Animation mAnimation;

@Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mScanner = (ImageView)findViewById(R.id.Scanner);

    mAnimation = new TranslateAnimation(0, 0, 0, 500);
    mAnimation.setDuration(10000);
    mAnimation.setFillAfter(true);
    mAnimation.setRepeatCount(-1);
    mAnimation.setRepeatMode(Animation.REVERSE);
    mScanner.setAnimation(mAnimation);
    mScanner.setVisibility(View.VISIBLE);
}
Run Code Online (Sandbox Code Playgroud)

现在我希望当图像到达屏幕底部时,它应该开始移回到顶部.我怎样才能做到这一点?

注意:完成反向模式.请参阅代码.但现在的问题是,它从底部移动到顶部时会留下线条.像附图一样.如何删除这一行? 截图

小智 45

根据这个修改你的代码:

   mScanner.setVisibility(View.VISIBLE);
   mAnimation = new TranslateAnimation(
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.ABSOLUTE, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 0f,
            TranslateAnimation.RELATIVE_TO_PARENT, 1.0f);
   mAnimation.setDuration(10000);
   mAnimation.setRepeatCount(-1);
   mAnimation.setRepeatMode(Animation.REVERSE);
   mAnimation.setInterpolator(new LinearInterpolator());
   mScanner.setAnimation(mAnimation);
Run Code Online (Sandbox Code Playgroud)

而且使用xml而不是image.请参阅以下代码并将其放入您的ImageViewsrc中.

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line">
    <solid android:color="#0000FF"/>
    <size android:width="480dp"
        android:height="10dp"/>
    <corners android:radius="1dp"/>
    <stroke android:width="3dp"
        android:color="#000000"/>
</shape>
Run Code Online (Sandbox Code Playgroud)

我希望它会对你有所帮助.


Luk*_*kap 7

您需要设置重复属性

机器人:REPEATMODE

INT.动画到达动画结尾时的行为方式.android:repeatCount必须设置为正整数或"-1"才能使此属性生效.设置为"反向"以使动画在每次迭代时反向或"重复"以使动画循环从每次开始.

Animation a;
a.setRepeatMode(Animation.REVERSE);
Run Code Online (Sandbox Code Playgroud)