Artifis
Artifis

Reputation: 87

ImageView disappears when TranslateAnimation is applied

I understand, that this is local problem, but I was fighting with it for 1 day and I want ask for your help.

I have such kind of "game", where user can move items by swype, in this case, we are doing swype from top to bottom. Items are actual ImageViews.

On the first image we can see 7 items before the actual swype

Item goes as down as he can, until he meets another item or border of the field (invisible now)

On the second image, items have been moved once to down (only those who didnt have obstacle moved, 4 of them)

And, final position of items after second swype:

Now the problem : I can't explain why, but exactly when I do first swype, from top to bottom, and apply to those 4 items which will move down the actual animation of transition to new position, they dissapear on half way and appear on the new place!

When I do any other swype, to any direction, any items are animated, there are no problems. Everything goes fine when I do second swype to bottom.

I checked and overchecked, animation is done properly, there comes right parameters, and there are no other ImageViews on the way of animation, but something is overlaying the animation exactly in case where there are obsticles on the top of ImageViews! This is just weird.

All needed code is following:

Animation void

private void moveItem(final FieldItem item, final float x, final float y) {
                      // item to animate, transition X, transition Y
    final RelativeLayout.LayoutParams oldParams = (LayoutParams) item
            .getLayoutParams();

    TranslateAnimation animation = new TranslateAnimation(0.0f, x, 0.0f, y);
    animation.setDuration(500);
    animation.setFillAfter(true);
    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            item.invalidate();
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {

            item.clearAnimation();

            //all following is just to remove old item from layout
            //and put new item on the place where animation should have ended
            //nothing which can affect, it works perfect with any other condition

            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                    itemSize, itemSize);

            params.leftMargin = (int) (oldParams.leftMargin + x);
            params.topMargin = (int) (oldParams.topMargin + y);


            gView.rLayout.removeView(item);
            putItem(1, params);
            gView.rLayout.invalidate();

            //it's only ArrayList which contains copies of all Layout's children
            //of ImageView class, for providing field moves logic, can't affect
            //also recounts how many imageViews are in children of Layout
            updateField();

            //shows right margins, right amount of items on the field, they are always recounted depending of RelativeLayout children
            Log.i("my_log", "Pos change, from " + oldParams.leftMargin + "/" + oldParams.topMargin + " to " + params.leftMargin + "/" + params.topMargin);
            Log.i("my_log", "Size of field: " + field.size());
        }
    });

    item.startAnimation(animation);
}

Layout which is used in here:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rootLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:clipChildren="false"
    android:orientation="vertical" >

    <com.crispy_chocolate.outofthebox.GameView
        android:id="@+id/game"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:clipChildren="false" />

</RelativeLayout>

So the question is - how can ImageView even dissapear during the local case of animation when in any other case the same methods work totally fine? Maybe someone will be kind to help me.

Upvotes: 0

Views: 579

Answers (1)

Artifis
Artifis

Reputation: 87

Apparently, it's some kind of a bug, because If I put any imageview to the bottom part of the screen, or write some text, animation starts to go completely OK. weird.

Upvotes: 1

Related Questions