Android 补间动画

来源:互联网 发布:蛙5火箭知乎 编辑:程序博客网 时间:2024/05/16 14:29

直接上代码(来自 android范例大全这本书)

布局文件 4 个

//anim_alpha.xml<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><alpha android:fromAlpha="1"    android:toAlpha="0"    android:fillAfter="true"    android:repeatMode="reverse"    android:repeatCount="1"    android:duration="2000"/></set>

//anim_rotate.xml<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><rotate     android:interpolator="@android:anim/accelerate_interpolator"    android:fromDegrees="0"    android:toDegrees="720"    android:pivotX="50%"    android:pivotY="50%"    android:duration="2000"></rotate><rotate     android:interpolator="@android:anim/accelerate_interpolator"    android:startOffset="2000"    android:fromDegrees="360"    android:toDegrees="0"    android:pivotX="50%"    android:pivotY="50%"    android:duration="2000"></rotate></set>

//anim_scale.xml<?xml version="1.0" encoding="utf-8"?><set  xmlns:android="http://schemas.android.com/apk/res/android"><scale android:fromXScale="1"    android:interpolator="@android:anim/decelerate_interpolator"    android:fromYScale="1"    android:toXScale="2.0"    android:toYScale="2.0"    android:pivotX="50%"    android:pivotY="50%"    android:fillAfter="true"    android:repeatCount="1"    android:repeatMode="reverse"    android:duration="2000"/></set>



//anim_translate<?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android"><translate     android:fromXDelta="0"    android:toXDelta="860"    android:fromYDelta="0"    android:toYDelta="0"    android:fillAfter="true"    android:repeatMode="reverse"    android:repeatCount="1"    android:duration="2000"></translate> </set>

// 入口activity


package com.myapplicatio10.wheee.myapplication10;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.view.animation.Animation;import android.view.animation.AnimationUtils;import android.widget.Button;import android.widget.ImageView;public class MainActivity extends Activity {    /**     * Called when the activity is first created.     */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        final Animation rotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);    //获取“旋转”动画资源        final Animation translate = AnimationUtils.loadAnimation(this, R.anim.anim_translate);    //获取“平移”动画资源        final Animation scale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);    //获取“缩放”动画资源        final Animation alpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);    //获取“透明度变化”动画资源        final ImageView iv = (ImageView) findViewById(R.id.imageView1);    //获取要应用动画效果的ImageView        final  Button button1 = (Button) findViewById(R.id.button1);    //获取“旋转”按钮        button1.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                button1 .startAnimation(translate);    //播放“平移”动画                iv.startAnimation(scale);    //播放“缩放”动画            }        });        Button button2 = (Button) findViewById(R.id.button2);    //获取“平移”按钮        button2.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                iv.startAnimation(translate);    //播放“平移”动画            }        });        Button button3 = (Button) findViewById(R.id.button3);    //获取“缩放”按钮        button3.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                iv.startAnimation(scale);    //播放“缩放”动画            }        });        Button button4 = (Button) findViewById(R.id.button4);    //获取“透明度渐变”按钮        button4.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                iv.startAnimation(alpha);    //播放“透明度渐变”动画            }        });    }}//        FrameLayout frameLayout = (FrameLayout) findViewById(R.id.f_1);//        MyClass myClass = new MyClass(this);//        frameLayout.addView(myClass);

即可实现。
在此记录










0 0
原创粉丝点击