geggiamarti
geggiamarti

Reputation: 129

Android animations - Translate and Scale don't work properly

I made a little method to translate a view above another view and scale to that view's size.

Method works, but it translate the view with a margin. This is the method:

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;
}

Then I use it like this

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);

And this is xml with many layouts, which I need to start the animation

<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>

Does anyone know another method to do this or why it doesn't work?

Upvotes: 1

Views: 183

Answers (0)

Related Questions