我想要做的是水平移动背景并让它无限重复。
我尝试使用带有动画的ImageSwitcher
来实现此效果,但无法使其正常工作。这是我到目前为止的代码
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;
}
}
答案 0 :(得分:50)
为什么不尝试自己动画背景而不是使用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();
这导致连续动画无限期地重复背景,应该看起来像这样:
答案 1 :(得分:4)
您可以使用 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度。