【发布时间】:2017-03-04 17:19:23
【问题描述】:
http://www.talk4u.in/TagoreAcadmy/marque.html
这是我的 html 格式的 url 现在我想要像这样的 bulid 图像滑块,但我不想使用 webview。我想使用 android 视图进行开发 并且图像移动应该像地球一样从重新开始到结束。
我有来自 mysql 数据库的多图像 url fatch,而不是像 marquee 那样显示在滑块上。
谢谢你
【问题讨论】:
http://www.talk4u.in/TagoreAcadmy/marque.html
这是我的 html 格式的 url 现在我想要像这样的 bulid 图像滑块,但我不想使用 webview。我想使用 android 视图进行开发 并且图像移动应该像地球一样从重新开始到结束。
我有来自 mysql 数据库的多图像 url fatch,而不是像 marquee 那样显示在滑块上。
谢谢你
【问题讨论】:
对“两个图像”使用 ValueAnimator 和 setTranslationX。
看到这个:Android move background continuously with animation
此外,在您的情况下,您需要更改滚动方向。 请注意下面的“已修改”。
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); // Modified
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:
如果你想使用来自服务器的图像,我建议你使用毕加索图像库。 http://square.github.io/picasso/
将毕加索依赖添加到 Build.gradle 文件中。 (
用毕加索加载图像。
final ImageView backgroundOne = (ImageView) findViewById(R.id.background_one);
final ImageView backgroundTwo = (ImageView) findViewById(R.id.background_two);
Picasso.with(this).load("http://www.talk4u.in/wp-content/uploads/2017/02/cropped-logo2.png").into(backgroundOne);
Picasso.with(this).load("http://www.talk4u.in/wp-content/uploads/2017/02/cropped-logo2.png").into(backgroundTwo);
final ValueAnimator animator = ValueAnimator.ofFloat(0.0f, -1.0f); // Modified
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();
【讨论】: