Android анимации - Перевод и Масштаб не работают должным образом - PullRequest
1 голос
/ 09 марта 2020

Я сделал небольшой метод для перевода вида над другим видом и масштабирования до размера этого вида.

Метод работает, но он переводит представление с полем. Это метод:

public AnimationSet getFromToAnimation(View from, View to, final Runnable onEnd){
    AnimationSet animationSet = new AnimationSet(true);
    animationSet.setInterpolator(new AccelerateDecelerateInterpolator());
    animationSet.setFillEnabled(true);
    animationSet.setDuration(3000);

    LinearLayout.LayoutParams fromParams = (LinearLayout.LayoutParams) from.getLayoutParams();
    LinearLayout.LayoutParams toParams = (LinearLayout.LayoutParams) to.getLayoutParams();
    float tx = (to.getX() + toParams.leftMargin) - (from.getX() + fromParams.leftMargin);
    float ty = (to.getY() + toParams.topMargin) - (from.getY() + fromParams.topMargin);
    TranslateAnimation translateAnimation = new TranslateAnimation(0, tx,0, ty);

    float x = (float) to.getWidth() / (float) from.getWidth();
    float y = (float) to.getHeight() / (float) from.getHeight();
    ScaleAnimation scaleAnimation = new ScaleAnimation(1f, x, 1f, y,
            Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f);

    animationSet.addAnimation(translateAnimation);
    animationSet.addAnimation(scaleAnimation);
    animationSet.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {}
        @Override
        public void onAnimationEnd(Animation animation) {
            onEnd.run();
        }
        @Override
        public void onAnimationRepeat(Animation animation) {}
    });
    return animationSet;
}

Затем я использую его следующим образом

LinearLayout lnrFrom = findViewById(R.id.lnr_game_operation);
LinearLayout lnrTo = findViewById(R.id.lnr_game_operation_old);
AnimationSet animation = getFromToAnimation(lnrFrom, lnrTo, new Runnable() {
    @Override
    public void run() {
        ...
    }
});
lnrFrom.startAnimation(animation);

И это xml со многими макетами, для которых мне нужно запустить анимацию

<LinearLayout
    android:gravity="center"
    android:orientation="vertical"
    android:layout_centerInParent="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <LinearLayout
        android:id="@+id/lnr_game_operation_old"
        android:background="@drawable/operation"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="@dimen/dp100"
        android:layout_height="@dimen/dp60">...</LinearLayout>

    <LinearLayout
        android:id="@+id/lnr_game_operation"
        android:background="@drawable/operation"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="@dimen/dp120"
        android:layout_height="@dimen/dp70">...</LinearLayout>

    <LinearLayout
        android:id="@+id/lnr_game_operation_new"
        android:visibility="invisible"
        android:background="@drawable/operation"
        android:gravity="center"
        android:orientation="horizontal"
        android:layout_width="@dimen/dp100"
        android:layout_height="@dimen/dp60">...</LinearLayout>

</LinearLayout>

Кто-нибудь знает другой способ сделать это или почему он не работает?

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

...