帧动画代码实现

来源:互联网 发布:看完就毁软件 编辑:程序博客网 时间:2024/06/13 02:00

package com.example.day06_code;

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;

public class MainActivity extends Activity {

private ImageView imageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {    super.onCreate(savedInstanceState);    setContentView(R.layout.activity_main);    imageView = (ImageView) findViewById(R.id.imageView);}// 透明public void alpha(View v) {    AlphaAnimation animation = new AlphaAnimation(0f, 1.5f);    // 持续时间    animation.setDuration(2000);    // 设置重复次数    animation.setRepeatCount(2);    // 重复的模式    animation.setRepeatMode(Animation.REVERSE);    imageView.startAnimation(animation);}// 缩放public void scale(View v) {    // ScaleAnimation animation=new ScaleAnimation(fromX, toX, fromY, toY,    // pivotXType, pivotXValue, pivotYType, pivotYValue)    ScaleAnimation animation = new ScaleAnimation(    1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_PARENT, 0.5f,            Animation.RELATIVE_TO_PARENT, 0.5f);    // 持续时间    animation.setDuration(2000);    // 设置重复次数    animation.setRepeatCount(2);    // 重复的模式    animation.setRepeatMode(Animation.REVERSE);    // 运行完的保持状态    animation.setFillAfter(true);    imageView.startAnimation(animation);}// 位移public void translate(View v) {    // TranslateAnimation animation=new TranslateAnimation(    // fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue,    // toYType, toYValue)    // 前四个参数都是设置x轴,后四个参数都是设置y轴    // 参数一:x轴起始点的类型(相对于自己,相对于父亲,绝对点)参数二:x轴起始点(当相对于自己和相对于父亲,都是倍数关系,当相对类型是绝对点时,代表的是像素);    // 参数三:x轴终点的类型,参数四:x轴终点的位置    TranslateAnimation animation = new TranslateAnimation(    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2,            Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0);    // 持续时间    animation.setDuration(2000);    // 设置重复次数    animation.setRepeatCount(2);    // 重复的模式    animation.setRepeatMode(Animation.REVERSE);    // 运行完的保持状态    animation.setFillAfter(true);    imageView.startAnimation(animation);}// 旋转public void rotate(View v) {    // 参数一:起始角度    // 参数二:结束角度    // 参数三:x轴原点的相对类型    // 参数四:x轴原点的位置    // 参数五:y轴原点的相对类型    // 参数六:y轴原点的位置    // 使用相对位置时,位置都倍数关系    RotateAnimation animation = new RotateAnimation(0, 360,            Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_SELF,            0.5f);    // 持续时间    animation.setDuration(2000);    // 设置重复次数    animation.setRepeatCount(2);    // 重复的模式    animation.setRepeatMode(Animation.REVERSE);    // 运行完的保持状态    animation.setFillAfter(true);    imageView.startAnimation(animation);}// 集合public void set(View v) {    AnimationSet animationSet = new AnimationSet(false);    // /////////////////////渐变////////////////////////////////////////////    AlphaAnimation alphaAnimation = new AlphaAnimation(0f, 1.5f);    // 持续时间    alphaAnimation.setDuration(2000);    // 设置重复次数    alphaAnimation.setRepeatCount(2);    // 重复的模式    alphaAnimation.setRepeatMode(Animation.REVERSE);    // /////////////////////放缩////////////////////////////////////////////////    ScaleAnimation scaleAnimation = new ScaleAnimation(    1.0f, 2.0f, 1.0f, 2.0f, Animation.RELATIVE_TO_PARENT, 0.5f,            Animation.RELATIVE_TO_PARENT, 0.5f);    // 持续时间    scaleAnimation.setDuration(2000);    // 设置重复次数    scaleAnimation.setRepeatCount(2);    // 重复的模式    scaleAnimation.setRepeatMode(Animation.REVERSE);    // 运行完的保持状态    scaleAnimation.setFillAfter(true);    // //////////位移/////////////    TranslateAnimation translateAnimation = new TranslateAnimation(    Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 2,            Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 0);    // 持续时间    translateAnimation.setDuration(2000);    // 设置重复次数    translateAnimation.setRepeatCount(2);    // 重复的模式    translateAnimation.setRepeatMode(Animation.REVERSE);    // 运行完的保持状态    translateAnimation.setFillAfter(true);    // ///////旋转//////    RotateAnimation rotateAnimation = new RotateAnimation(0, 360,            Animation.RELATIVE_TO_PARENT, 0.5f, Animation.RELATIVE_TO_SELF,            0.5f);    // 持续时间    rotateAnimation.setDuration(2000);    // 设置重复次数    rotateAnimation.setRepeatCount(2);    // 重复的模式    rotateAnimation.setRepeatMode(Animation.REVERSE);    // 运行完的保持状态    rotateAnimation.setFillAfter(true);    //添加动画到动画集合    animationSet.addAnimation(alphaAnimation);    animationSet.addAnimation(scaleAnimation);    animationSet.addAnimation(translateAnimation);    animationSet.addAnimation(rotateAnimation);    imageView.startAnimation(animationSet);}

}

0 0