Evan Sanderson
Evan Sanderson

Reputation: 105

How does the matrix class work?

I have been working on a card game for android lately. Everything runs smoothly and without error, so I would like to begin touching it up a little. One thing I would like to do is to have my cards slide in from the edge of the screen when they are dealt. I heard that the matrix array class could be of great use when doing scrolling animation, but I have no clue how to do this, and online searches seem to turn up nothing of use. Is there a more practical and efficient way to do this than by using the matrix? If not, could somebody explain how the matrix works?

Upvotes: 0

Views: 102

Answers (1)

tim687
tim687

Reputation: 2276

You can create and attach an Animator to the card objects. (or just load the animation and play it when needed, which I'll show below using some code)

Then you'll need to create a folder in res called anim, in that folder create two files:

slide_up_left.xml

     <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <translate
        android:duration="@integer/config_slide_time"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

    <alpha
        android:duration="@integer/config_slide_time"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

    <rotate
        android:duration="@integer/config_slide_time"
        android:fromDegrees="25"
        android:pivotX="0"
        android:pivotY="0"
        android:toDegrees="0" />

</set>

slide_up_right.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >

    <translate
        android:duration="@integer/config_slide_time"
        android:fromYDelta="100%p"
        android:toYDelta="0" />

    <alpha
        android:duration="@integer/config_slide_time"
        android:fromAlpha="0.0"
        android:toAlpha="1.0" />

    <rotate
        android:duration="@integer/config_slide_time"
        android:fromDegrees="-25"
        android:pivotX="100%"
        android:pivotY="0"
        android:toDegrees="0" />

</set>

And create a xml file called integers in your values folder that contains this:

 <?xml version="1.0" encoding="utf-8"?>
<resources>
     <integer name="config_slide_time">800</integer>
</resources>

If you don't need an Animator object use this code:

YOURCARD_VIEW.startAnimation(AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_left)); 

Or

YOURCARD_VIEW.startAnimation( AnimationUtils.loadAnimation(getContext(), R.anim.slide_up_right));

The YOURCARD_VIEW is a View object so you must have the card defined as a View

Upvotes: 1

Related Questions