Android : Android Animation leaving images outside parent view

on Sunday, July 6, 2014


I have a basic android game similar to 2048. All that's important to the problem is images move around a grid of 4x4. When they move, I animate the move. On some devices the animation spills over past it's cell and does not clean up - it leaves remnants of the image outside the cell. I can't find a fix, please help or offer some advice on a better way to do it :(



  • I have a grid layout of 4x4 - 16 squares

  • In each square is a relative layout.


  • In the relative layout I put the Images



    <RelativeLayout
    android:id="@+id/relativeLayoutMon00"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_column="0"
    android:layout_margin="1dp"
    android:layout_row="0"
    android:gravity="center"
    android:clipChildren="false"
    >

    <ImageView
    android:id="@+id/imageMon00"
    android:layout_width="@dimen/square_size"
    android:layout_height="@dimen/square_size"
    android:layout_margin="1dp"
    android:gravity="center"
    android:contentDescription="@string/ig00"
    android:scaleType="fitXY"
    />

    </RelativeLayout>



My animation moves to like images together, produces a 'squash' and then replaces them with a new image



//Move the Image
TranslateAnimation move = new TranslateAnimation(fromLoc[0]-toLoc[0],0,fromLoc[1]-toLoc[1],0);
move.setDuration(durationSlide);
move.setZAdjustment(Animation.ZORDER_TOP);
tmpImage.startAnimation(move);
move.setAnimationListener(new IVAnimationListener(getApplicationContext(),toRL,tmpImage,toImage,mon) {
@Override
public void onAnimationEnd(Animation animation) {
//When the move is finished do the squash animation ontop and put the new image below
int imgID = getResources().getIdentifier(monImagePrefix+String.valueOf(monTo.level), "drawable", getPackageName());
imageTo.setImageResource(imgID);
Animation popin = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.popin);
popin.setDuration(durationUpgrade);
imageTo.startAnimation(popin);
imageFrom.setImageResource(R.drawable.splat);
imageFrom.bringToFront();
Animation splat = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.squash);
//When it's all done get rid of the 'squash'
imageFrom.startAnimation(splat);
splat.setAnimationListener(new IVAnimationListener(getApplicationContext(),layout,imageFrom,imageTo,monTo) {
@Override
public void onAnimationEnd(Animation animation) {
layout.removeView(imageFrom);
}
});
}
});


and the popin Animation



<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.0"
android:toXScale="1.0"
android:fromYScale="0.0"
android:toYScale="1.0"
android:duration="200"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="200"
>
</scale>


and the Squash Animation



<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="0.1"
android:toXScale="1.5"
android:fromYScale="0.1"
android:toYScale="1.5"
android:duration="100"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="0"
>
</scale>

<scale xmlns:android="http://schemas.android.com/apk/res/android"
android:fromXScale="1.5"
android:toXScale="0"
android:fromYScale="1.5"
android:toYScale="0"
android:duration="200"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="100"
>
</scale>


This works fine. But one some devices leaves a image behind outside of the effected grid cell. A picture show it better:


ScreenShot


Or it's still broken in my release version: https://play.google.com/store/apps/details?id=com.h17.monstermash


0 comments:

Post a Comment