连续向右移动背景并使其从左侧重新出现

时间:2016-12-21 14:11:25

标签: android android-layout android-animation

我的布局背景有一张图片。我希望它开始向右移动,从右边消失的每一帧都应该重新出现在左边。因此,图像将仅与照片的一帧连续移动。我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

这是最简单,最快捷的方式,它可以在ImageView中拥有两个ViewGroup,并使用两个不同的动画为它们设置动画效果。通过获取容器的宽度,第一个将从其位置(START)移动到右边缘(PARENT_WIDTH),第二个将从容器外部跟随({{1} })到里面(-PARENT_WIDTH)。最后,让动画重复START会产生真实循环的错觉。

INFINITE

容器ViewGroup的宽度为private ViewGroup parent; private ImageView imgInner, imgOutter; @Override public void onCreate(...) { ... parent = (ViewGroup) findViewById(R.id.parent_loop); imgInner = (ImageView) findViewById(R.id.image_loop_inner); imgOutter = (ImageView) findViewById(R.id.image_loop_outter); ... setImageLoop(); } private void setImageLoop() { // Need a thread to get the real size or the parent // container, after the UI is displayed imgInner.post(new Runnable() { @Override public void run() { TranslateAnimation outAnim = new TranslateAnimation( 0f, parent.getWidth(), 0f, 0f); // move from 0 (START) to width (PARENT_SIZE) outAnim.setInterpolator(new LinearInterpolator()); outAnim.setRepeatMode(Animation.INFINITE); // repeat the animation outAnim.setRepeatCount(Animation.INFINITE); outAnim.setDuration(2000); TranslateAnimation inAnim = new TranslateAnimation( - parent.getWidth(), 0f, 0f, 0f); // move from out width (-PARENT_SIZE) to 0 (START) inAnim.setInterpolator(new LinearInterpolator()); inAnim.setRepeatMode(Animation.INFINITE); inAnim.setRepeatCount(Animation.INFINITE); inAnim.setDuration(2000); // same duration as the first imgInner.startAnimation(outAnim); // start first anim imgOutter.startAnimation(inAnim); // start second anim } }); } ,但可以更改,因此match_parent属性将替换为START之类的内容。此布局可以是parent.getLeft()LinearLayout或其他任何内容。例如,我使用了这个:

RelativeLayout

这给出了我的输出(请记住,当它真的不是时,gif使它看起来不稳定):

Infinite loop for imageviews from right to left

答案 1 :(得分:0)

更新了代码:

 img = (ImageView) findViewById(R.id.imageView1);
 TranslateAnimation animation = new TranslateAnimation(-95.0f, 740.0f,
            0.0f, 0.0f); // new TranslateAnimation(xFrom,xTo, yFrom,yTo)
 animation.setDuration(5000); // animation duration
 animation.setRepeatCount(5); // animation repeat count

 img.startAnimation(animation); // start animation
相关问题