Что я хочу сделать, так это переместить фон по горизонтали и заставить его повторяться бесконечно.

Я пробовал использовать ImageSwitcher с анимацией, чтобы создать этот эффект, но не смог заставить его работать правильно. Это код, который у меня есть 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;
    }
}
25
Praveen Pandey 27 Апр 2016 в 18:32

2 ответа

Лучший ответ

Почему бы вам не попробовать просто анимировать фон самостоятельно вместо того, чтобы использовать ViewSwitcher? Все, что вам нужно, - это один простой ValueAnimator:

Сначала добавьте два одинаковых 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>

Затем используйте ValueAnimator для анимации их свойства 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();

Это приводит к непрерывной анимации, которая бесконечно повторяет фон и должна выглядеть примерно так:

64
Xaver Kapeller 28 Апр 2016 в 23:49

Вы можете использовать библиотеку 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" />

РЕДАКТИРОВАТЬ:

Как упоминал @Cliff Burton, вы можете повернуть вид на 90 градусов, если хотите прокрутить по вертикали.

5
Mohamed Ibrahim 28 Ноя 2016 в 12:52