Java Android 使用动画连续移动背景

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36894384/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:46:05  来源:igfitidea点击:

Android move background continuously with animation

javaandroidandroid-animation

提问by Praveen Pandey

What I want to do is move the background horizontally and have it repeat infinitely.

我想要做的是水平移动背景并让它无限重复。

I tried using an ImageSwitcherwith animation to give this effect, but couldn't get it to work right. This is the code I have so farL

我尝试使用ImageSwitcherwith 动画来产生这种效果,但无法正常工作。这是我迄今为止的代码L

public class MainActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory {

    private Animation animSlide;
    private ImageSwitcher image;
    private ImageView imagePop;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageSwitcher) findViewById(R.id.image_switcher);

        image.setFactory(this);
        image.setImageResource(R.drawable.zc06);
        Animation in = AnimationUtils.loadAnimation(this, android.R.anim.slide_in_left);
        in.setDuration(10000);
        Animation out = AnimationUtils.loadAnimation(this, android.R.anim.slide_out_right);
        out.setDuration(10000);
        image.setInAnimation(in);
        image.setOutAnimation(out);
        Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {

            @Override
            public void run() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        image.setImageResource(R.drawable.zc06);
                    }
                });
            }

        }, 0, 10000);

        Animation mZoomInAnimation = AnimationUtils.loadAnimation(this, R.anim.zoom_in);

        Animation mZoomOutAnimation = AnimationUtils.loadAnimation(this, R.anim.zoom_out);
        imagePop.startAnimation(mZoomInAnimation);
        imagePop.startAnimation(mZoomOutAnimation);

    }

    @Override
    public View makeView() {
        ImageView myView = new ImageView(getApplicationContext());
        return myView;
    }
}

采纳答案by Xaver Kapeller

Why don't you try to just animate the background yourself instead of using a ViewSwitcher? All you need is one simple ValueAnimator:

为什么不尝试自己为背景设置动画而不是使用ViewSwitcher?您只需要一个简单的ValueAnimator

First add two identical ImageViewsto your layout and set the same background image to both of them:

首先添加两个相同ImageViews的布局并为它们设置相同的背景图像:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/background_one"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/background"/>

    <ImageView
        android:id="@+id/background_two"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/background"/>

</FrameLayout>

Then use a ValueAnimatorto animate their translationX property, but offset them by their width:

然后使用 aValueAnimator为它们的 translationX 属性设置动画,但用它们的宽度偏移它们:

final ImageView backgroundOne = (ImageView) findViewById(R.id.background_one);
final ImageView backgroundTwo = (ImageView) findViewById(R.id.background_two);

final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, 1.0f);
animator.setRepeatCount(ValueAnimator.INFINITE);
animator.setInterpolator(new LinearInterpolator());
animator.setDuration(10000L);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        final float progress = (float) animation.getAnimatedValue();
        final float width = backgroundOne.getWidth();
        final float translationX = width * progress;
        backgroundOne.setTranslationX(translationX);
        backgroundTwo.setTranslationX(translationX - width);
    }
});
animator.start();

This results in an continuous animation which repeats the background indefinitely and should look something like this:

这会产生一个连续的动画,它无限期地重复背景,看起来应该是这样的:

回答by Mohamed Ibrahim

you can use AndroidScrollingImageViewLibrary, all you have to do is to define speed and the drawable source

您可以使用AndroidScrollingImageView库,您所要做的就是定义速度和可绘制源

<com.q42.android.scrollingimageview.ScrollingImageView
    android:id="@+id/scrolling_background"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    scrolling_image_view:speed="1dp"
    scrolling_image_view:src="@drawable/scrolling_background" />

EDIT:

编辑:

as @Cliff Burton mentioned, you can rotate the view by 90 deg if you want to scroll vertically.

正如@Cliff Burton 提到的,如果您想垂直滚动,可以将视图旋转 90 度。