android游戏开发5-10 补间动画 透明度渐变 旋转 缩放 平移

首先在res文件下新建anim文件夹
新建anim_alpha.xml代码如下:


<set xmlns:android="http://schemas.android.com/apk/res/android">

    <alpha 
        android:duration="2000"
        android:fillAfter="true"
        android:fromAlpha="1"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toAlpha="0"/>

set>

新建anim_translate.xml


<set xmlns:android="http://schemas.android.com/apk/res/android">

<translate 
    android:duration="2000"
    android:fillAfter="true"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:toXDelta="860"
    android:toYDelta="0"
    />

set>

新建anim_rotate.xml


<set xmlns:android="http://schemas.android.com/apk/res/android">


    <rotate 
        android:duration="2000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="720"/>
    <rotate 
        android:duration="2000"
        android:fromDegrees="360"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="2000"
        android:toDegrees="0"/>

set>

新建anim_scale.xml


<set xmlns:android="http://schemas.android.com/apk/res/android">

    <scale 
        android:duration="2000"
        android:fillAfter="true"
        android:fromXScale="1"
        android:fromYScale="1"
        android:interpolator="@android:anim/accelerate_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toXScale="2.0"
        android:toYScale="2.0"/>

set>

然后修改main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/cat"/>

    <LinearLayout
        android:id="@+id/myli2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/alpha"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="透明" />

        <Button
            android:id="@+id/translate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="旋转" />

        <Button
            android:id="@+id/scale"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="缩放" />

        <Button
            android:id="@+id/rotate"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="平移" />
    LinearLayout>

LinearLayout>

MainActivity.java

package com.mz.demo;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
    private ImageView img;
    private Button btn1;
    private Button btn2;
    private Button btn3;
    private Button btn4;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        final Animation animation_alpha = AnimationUtils.loadAnimation(this,
                R.anim.anim_alpha);
        final Animation animation_translate = AnimationUtils.loadAnimation(
                this, R.anim.anim_translate);
        final Animation animation_scale = AnimationUtils.loadAnimation(this,
                R.anim.anim_scale);
        final Animation animation_rotate = AnimationUtils.loadAnimation(this,
                R.anim.anim_rotate);
        img = (ImageView) findViewById(R.id.img);
        btn1 = (Button) findViewById(R.id.alpha);
        btn2 = (Button) findViewById(R.id.translate);
        btn3 = (Button) findViewById(R.id.scale);
        btn4 = (Button) findViewById(R.id.rotate);
        btn1.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                img.startAnimation(animation_alpha);
            }
        });
        btn2.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                img.startAnimation(animation_translate);
            }
        });
        btn3.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                img.startAnimation(animation_scale);
            }
        });
        btn4.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                img.startAnimation(animation_rotate);
            }
        });
    }
}

你可能感兴趣的:(android)