Android Autoscroll imageview

前端 未结 3 1129
囚心锁ツ
囚心锁ツ 2021-02-06 15:30

In my activity I have just an ImageView. In it, the src is a picture that is a lot bigger than the screen. I want the picture to scroll slooooowly from left to right until it re

3条回答
  •  礼貌的吻别
    2021-02-06 15:51

    So, the solution to my problem is here (for all of the new android developers, like me, who might need this for their apps):

    public class MainActivity extends Activity {
    
        Animation           _translateAnimation;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            _translateAnimation = new TranslateAnimation(TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, -300f, TranslateAnimation.ABSOLUTE, 0f, TranslateAnimation.ABSOLUTE, 0f);
            _translateAnimation.setDuration(8000);
            _translateAnimation.setRepeatCount(-1);
            _translateAnimation.setRepeatMode(Animation.REVERSE); // REVERSE
            _translateAnimation.setInterpolator(new LinearInterpolator());
            ImageView img = (ImageView) findViewById(R.id.img);
            img.startAnimation(_translateAnimation);
        }
    }
    

    and the layout:

    
    
    
    
    
        
    
        
      
    
    

    just make sure your image is big/high enough to fit the screen, and is (+300dp) wider than the screen width (or adjust the code above).

    Happy coding ;)

提交回复
热议问题