0
\$\begingroup\$

I'm making some sort of clone for FlappyBird, but with some adjustments.

I'm trying to have an animated background running infinitely times. I already have the picture mirrored so the flow will be clear as possible. Problem is, the gameplay itself (my GameView class) doesn't have any XML layout, so I can't use the regular way with the following code:

        ImageView bg1 = findViewById(R.id.bg1);
        ImageView bg2 = findViewById(R.id.bg2);

        ValueAnimator animator=ValueAnimator.ofFloat(0.0f,1.0f);
        animator.setRepeatCount(ValueAnimator.INFINITE);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(10000L);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                final float prog = (float) animation.getAnimatedValue();
                final float wid = bg1.getWidth();
                final float translationX = wid*prog;
                bg1.setTranslationX(translationX);
                bg2.setTranslationX(translationX-wid);
            }
        });
        animator.start();

Is there a way I can change the ImageView to Bitmap (couldn't find alternative to ImageView's setTranslationX)? If not, is there a possible to relate the class to certain XML layout without using setContentView() (my class doesn't relay or change any XML layout)?

Thank you in advance!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.