【问题标题】:Android move background continuously with animationAndroid用动画连续移动背景
【发布时间】:2016-08-22 00:29:26
【问题描述】:

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

我尝试使用带有动画的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;
    }
}

【问题讨论】:

  • 你为什么使用视图动画?你真的不应该再使用它们了……改用新的动画 API。无论如何,您可以通过在布局中添加两个ImageViews 并使用ValueAnimator 翻译它们来轻松创建这样的动画。有关更多信息,请参阅我的答案。

标签: java android android-animation


【解决方案1】:

您为什么不尝试自己制作背景动画而不是使用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();

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

【讨论】:

  • 如果您想减慢动画速度,请增加持续时间! :-)
  • 可以通过改变 ValueAnimator.ofFloat(0.0f, -1.0f) 和 backgroundTwo.setTranslation(translation + width) 来反转动画
  • 对我来说效果很好,谢谢! +1
【解决方案2】:

你可以使用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 度。

【讨论】:

  • 有没有办法用这个垂直滚动?
  • @CliffBurton 我之前没试过,但你可以玩一下,也许你可以在 xml 代码中添加方向,或者在 java 代码中可能有一种方法可以做到这一点,但这些只是猜测。
  • 我刚刚意识到,如果您想垂直滚动,您可以将视图旋转 90 度...如果您愿意,您可以使用此解决方案更新您的答案;)
  • @CliffBurton 如何将视图旋转 90 度。 ?
  • @bhanukaushik 我设法在 xml 布局的 ScrollingImageView 视图中使用参数 android:rotation="90" 旋转视图
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2016-03-29
  • 1970-01-01
相关资源
最近更新 更多