How to create a looping animation by continuous translating an image?

前端 未结 2 903
离开以前
离开以前 2021-02-04 17:26

By using a repeating image like this,

\"repeating

is it possible to create animation like this?

2条回答
  •  孤城傲影
    2021-02-04 18:20

    I figured it out


    MainActivity.java:

    public class MainActivity extends Activity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            final int screenWidth = getScreenDimensions(this).x;
            final int waveImgWidth = getResources().getDrawable(R.drawable.wave).getIntrinsicWidth();
            int animatedViewWidth = 0;
            while (animatedViewWidth < screenWidth) {
                animatedViewWidth += waveImgWidth;
            }
            animatedViewWidth += waveImgWidth;
    
    
            View animatedView = findViewById(R.id.animated_view);
            FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams) animatedView.getLayoutParams();
            layoutParams.width = animatedViewWidth;
            animatedView.setLayoutParams(layoutParams);
    
    
            Animation waveAnimation = new TranslateAnimation(0, -waveImgWidth, 0, 0);
            waveAnimation.setInterpolator(new LinearInterpolator());
            waveAnimation.setRepeatCount(Animation.INFINITE);
            waveAnimation.setDuration(2500);
    
            animatedView.startAnimation(waveAnimation);
        }
    
        public static Point getScreenDimensions(Context context) {
            int width = context.getResources().getDisplayMetrics().widthPixels;
            int height = context.getResources().getDisplayMetrics().heightPixels;
            return new Point(width, height);
        }
    
    }
    


    activity_main.xml:

    
    
        
    
    
    


    wave_repeating_bg.xml:

    
    
    


    drawable-xxhdpi/wave.jpg:
    enter image description here

提交回复
热议问题