如何在android中使用翻译动画连续上下移动图像? [英] How to move image up and down continuously using translate animation in android?

查看:29
本文介绍了如何在android中使用翻译动画连续上下移动图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用 Translate Animation 成功地完成了一侧动画,这意味着图像从上到下.代码如下:

I have successfully done one side animation using Translate Animation means the image goes from top to the bottom. Here is the code:

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

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

Now I want that when image reaches to the bottom of the screen, it should start moving back to the top. How can I do that?

注意:完成反向模式.请看代码.但现在的问题是,从下到上移动时会留下线条.喜欢附加的图像.如何删除此行?

Note: Done the reverse mode. Please see the code. But now problem is that, it leaves lines when moving from bottom to top. Like the attached image. How to remove this lines?

推荐答案

根据这个修改你的代码:

Modify your code according to this:

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

此外使用 xml 而不是图像.请查看以下代码并将其放入您的 ImageView src.

And moreover use xml rather than image. Please see the below code and put it in your ImageView src.

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

希望能帮到你.

这篇关于如何在android中使用翻译动画连续上下移动图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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