Не работает вложенная относительная компоновка анимации - PullRequest
0 голосов
/ 01 сентября 2018

Я пытаюсь переместить изображение по вертикали и горизонтали и одновременно добавить к нему эффект дрожания. Я вложил изображение в относительную компоновку, которая вложена в другую относительную компоновку. Самая верхняя относительная компоновка предназначена для перевода по оси y, под ним - трансляция по оси x, а на изображении видна бесконечная вращающаяся анимация. Проблема в том, что у меня есть перевод по оси Y и анимация дрожания на изображении, но горизонтальная анимация запускается, но не работает или повторяется.

Моя структура макета:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ccolutions1.tiktokdownloader.SplashActivity">

<RelativeLayout
    android:id="@+id/splash_image_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:paddingBottom="50dp"
    android:paddingTop="50dp">

    <RelativeLayout
        android:id="@+id/splash_image_sway_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:paddingBottom="20dp"
        android:paddingLeft="40dp"
        android:paddingRight="40dp"
        android:paddingTop="20dp">

        <ImageView
            android:id="@+id/splash_image"
            android:layout_width="120dp"
            android:layout_height="120dp"
            android:layout_centerInParent="true"
            app:srcCompat="@drawable/music_video_image" />
    </RelativeLayout>

</RelativeLayout>

Код анимации моего перевода:

RelativeLayout splashImageContainer = findViewById(R.id.splash_image_container);
    RelativeLayout splashImageSwayContainer = findViewById(R.id.splash_image_sway_container);
    ImageView splashImage = findViewById(R.id.splash_image);

    Animation shakeAnimation = AnimationUtils.loadAnimation(SplashActivity.this, R.anim.rotate_animation);

    splashImage.startAnimation(shakeAnimation);

    TranslateAnimation bounceAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -0.5f);
    bounceAnim.setRepeatMode(Animation.REVERSE);
    bounceAnim.setRepeatCount(Animation.INFINITE);
    bounceAnim.setDuration(1200);
    bounceAnim.setInterpolator(new LinearInterpolator());
    bounceAnim.setFillEnabled(true);
    bounceAnim.setFillAfter(true);

    TranslateAnimation swayAnimation = new TranslateAnimation(Animation.RELATIVE_TO_SELF, -1f, Animation.RELATIVE_TO_SELF, 1f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);
    bounceAnim.setRepeatMode(Animation.REVERSE);
    bounceAnim.setRepeatCount(Animation.INFINITE);
    bounceAnim.setDuration(600);
    bounceAnim.setInterpolator(new LinearInterpolator());
    bounceAnim.setFillEnabled(true);
    bounceAnim.setFillAfter(true);
    swayAnimation.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {
            System.out.println("swaying");
        }

        @Override
        public void onAnimationEnd(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            System.out.println("swaying again");
        }
    });

    splashImageContainer.startAnimation(bounceAnim);
    splashImageSwayContainer.startAnimation(swayAnimation);

Заранее спасибо.

...