2017-03-04 90 views

回答

1

使用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(); 

EDIT1:

如果你想從服務器使用的圖片,我建議你用畢加索的圖片庫。 http://square.github.io/picasso/

  1. 添加畢加索依賴性的build.gradle文件。 (

    • 下面添加到依賴
      編譯 'com.squareup.picasso:畢加索:2.5.2' 的Android工作室
  2. 與畢加索加載圖像的頂部的

  3. 並按同步按鈕。

    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(); 
    
    • 添加網絡權限來訪問互聯網。
+0

哦,謝謝cricket_007。我無法修復該代碼塊。 –