PowenKo, Android Tutorial 085-6, Animation,XML


create a animation via XML on android

step1:

add a new xml

step2:

setup “animation” and name

res\ani\myalpha.xml

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

    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="12000" />
</set>

res\ani\myrotate.xml

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

    <rotate
        android:fromDegrees="359"
        android:toDegrees="0"
        android:duration="3000"
        android:pivotX="50%"
        android:pivotY="50%" />

</set>

res\ani\myscale.xml

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

    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="2.0"
        android:toYScale="2.0"
        android:duration="3000"></scale>
</set>

res\ani\mytranslate.xml

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

    <translate
        android:fromXDelta="-200%"
        android:toXDelta="0%"
        android:fromYDelta="200%"
        android:toYDelta="0%"
        android:duration="3000"
        android:zAdjustment="top" />

</set>

res\ani\myani1.xml

<?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android">
    <alpha
        android:fromAlpha="0.0"
        android:toAlpha="1.0"
        android:duration="3000" />
   <rotate
        android:fromDegrees="359"
        android:toDegrees="0"
        android:duration="3000"
        android:pivotX="50%"
        android:pivotY="50%" />

    <scale
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="2.0"
        android:toYScale="2.0"
        android:duration="3000"></scale>
</set>

res\layout\main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal">
    <TextView
        android:id="@+id/firstTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="PowenKo,1"
        android:textSize="42sp" />
    <TextView
        android:text="PowenKo,2"
        android:id="@+id/secondTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="42sp"></TextView>
    <TextView
        android:text="PowenKo,3"
        android:id="@+id/thirdTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="42sp"></TextView>
    <TextView
        android:text="PowenKo,4"
        android:id="@+id/fourthTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="42sp"></TextView>
    <TextView
        android:text="PowenKo,5"
        android:id="@+id/fifthTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:textSize="42sp"></TextView>
</LinearLayout>
package com.powenko.Tutorial_Animation_XML;

import android.app.Activity;
import android.os.Bundle;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.TextView;

public class Tutorial_Animation_XMLActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        RunMyAnimations();
    }
    private void RunMyAnimations() {
        Animation a = AnimationUtils.loadAnimation(this, R.anim.myalpha);
        a.reset();
        TextView tv = (TextView) findViewById(R.id.firstTextView);
        tv.clearAnimation();
        tv.startAnimation(a);

        a = AnimationUtils.loadAnimation(this, R.anim.myrotate);
        a.reset();
        tv = (TextView) findViewById(R.id.secondTextView);
        tv.clearAnimation();
        tv.startAnimation(a);

        a = AnimationUtils.loadAnimation(this, R.anim.myscale);
        a.reset();
        tv = (TextView) findViewById(R.id.thirdTextView);
        tv.clearAnimation();
        tv.startAnimation(a);

        a = AnimationUtils.loadAnimation(this, R.anim.mytranslate);
        a.reset();
        tv = (TextView) findViewById(R.id.fourthTextView);
        tv.clearAnimation();
        tv.startAnimation(a);

        a = AnimationUtils.loadAnimation(this, R.anim.myani1);
        a.reset();
        tv = (TextView) findViewById(R.id.fifthTextView);
        tv.clearAnimation();
        tv.startAnimation(a);
    }
}

sample code:

Tutorial_Animation_XML


Leave a Reply