Android - анимируйте изображение без настройки imageView - PullRequest
0 голосов
/ 21 мая 2018

В моем приложении я хочу анимировать изображение, которое поступает с одной стороны и вытекает с другой.Я использую для анимации фон, как в коде ниже, но в этом методе я установил imageView внутри моего activity_main, и я бы не знал, куда поместить imageView, когда анимированное изображение было бы отключено от экрана:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        final ImageView backgroundOne = (ImageView) findViewById(R.id.background_one);
        final ImageView backgroundTwo = (ImageView) findViewById(R.id.background_two);

        final ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.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 progress = (float) animation.getAnimatedValue();
                final float width = backgroundOne.getWidth();
                final float translationX = width * progress;
                backgroundOne.setTranslationX(translationX);
                backgroundTwo.setTranslationX(translationX - width);
            }
        });
        animator.start();

    }

}

1 Ответ

0 голосов
/ 21 мая 2018

Вы создаете отдельный макет с динамически вставленным изображением (image_layout.xml):

<ImageView
    android:id="@+id/anim_image"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:src="..."/>

Далее используйте «inflater»:

ViewGroup mainLayout = (ViewGroup) findViewById(R.id.main);
    v = (ViewGroup) getLayoutInflater().inflate(R.layout.image_layout, mainLayout, false);
    mainLayout.addView(v);

    final ImageView backgroundOne = (ImageView) v.findViewById(R.id.anim_image);

Завершено:

 ViewGroup mainLayout = (ViewGroup) findViewById(R.id.main);
    v = (ViewGroup) getLayoutInflater().inflate(R.layout.image_layout, mainLayout, false);
    mainLayout.addView(v);

    final ImageView backgroundOne = (ImageView) v.findViewById(R.id.anim_image);

    final ValueAnimator animator = ValueAnimator.ofFloat(1.0f, 0.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 progress = (float) animation.getAnimatedValue();
            final float width = v.getWidth();
            final float translationX = width * progress;
            v.setTranslationX(translationX);
            v.setTranslationX(translationX - width);
        }
    });
    animator.start();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...