简单的四种动画Demo

package com.saiermeng.annimation;

import android.app.Activity;
import android.os.Bundle;
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 iv;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById(R.id.iv_xfj);
    }


    public void alpha(View v){
        // 创建透明度的动画对象
        // fromAlpha 开始透明度的值 0~1.0 由完全透明到完全不透明
        // toAlpha 结束透明度的值
        AlphaAnimation aa = new AlphaAnimation(0, 1.0f);
        //设置动画重复播放的次数
        aa.setRepeatCount(2);
        //设置动画播放的顺序的模式:REVERSE 相反的顺序
        aa.setRepeatMode(AlphaAnimation.REVERSE);
        //设置动画播放的总时间长度
        aa.setDuration(3000);
        //在IMageView上播放动画
        iv.startAnimation(aa);
    }
    public void rotate(View v) {
        // 创建旋转的动画对象

        // fromDegrees 旋转的开始角度 0~360
        // toDegrees 旋转的结束角度
        // pivotXType 在水平方向相对旋转的类型,如 RELATIVE_TO_SELF:相对自己,相对父级窗体
        // pivotXValue 相对的位置,如自己宽度的0.5f
        // pivotYType 在垂直方向相对选装的类型,如 RELATIVE_TO_SELF:相对自己,相对父级窗体
        // pivotYValue 相对的位置,如自己高度的0.5f
        RotateAnimation ra = new RotateAnimation(0, 360,
                1, 0.5f,
                1, 0.5f);
        // 设置动画重复播放的次数
        ra.setRepeatCount(2);
        // 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
        ra.setRepeatMode(AlphaAnimation.REVERSE);
        // 设置动画重复播放的总时间长度
        ra.setDuration(3000);
        // 在imageview上播放动画
        iv.startAnimation(ra);

    }
    public void scale(View v) {
        // 创建缩放的动画对象

        // fromX 开始缩放时宽度的比例 最小为0,放大一倍为1.0f
        // toX 结束缩放时宽度的比例
        // fromY 开始缩放时高度开始缩放时宽度的比例 最小为0,放大一倍为1.0f
        // toY 结束缩放时高度的比例的比例 最小为0,放大一倍为1.0f
        // pivotXType 在水平方向相对缩放的类型,如 RELATIVE_TO_SELF:相对自己,相对父级窗体
        // pivotXValue 相对的位置,如自己宽度的0.5f
        // pivotYType 在垂直方向相对缩放的类型,如 RELATIVE_TO_SELF:相对自己,相对父级窗体
        // pivotYValue 相对的位置,如自己高度的0.5f
        ScaleAnimation sa = new ScaleAnimation(0, 2.0f, 0, 2.0f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        // 设置动画重复播放的次数
        sa.setRepeatCount(2);
        // 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
        sa.setRepeatMode(AlphaAnimation.REVERSE);
        // 设置动画重复播放的总时间长度
        sa.setDuration(3000);
        // 在imageview上播放动画
        iv.startAnimation(sa);

    }
    public void trans(View v) {
        // 创建平移的动画对象

        // fromXType 在水平方向平移时相对的类型,
        // fromXValue 平移的开始位置
        // toXType 在水平方向平移时相对的类型,
        // toXValue 在水平方向平移倍数
        // fromYType 在垂直方向平移时相对的类型,
        // fromYValue 平移的开始位置
        // toYType 在垂直方向平移时相对的类型,
        // toYValue 在垂直方向平移倍数
        TranslateAnimation ta = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 3.0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 3.0f);
        // 设置动画重复播放的次数
        ta.setRepeatCount(2);
        // 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
        ta.setRepeatMode(AlphaAnimation.REVERSE);
        // 设置动画重复播放的总时间长度
        ta.setDuration(3000);
        // 在imageview上播放动画
        iv.startAnimation(ta);

    }

    public void set(View v) {

        // 创建动画集合
        // shareInterpolator 插筒, true 表示一个动画接一个的播放动画 false会把几种动画总到一起播放
        AnimationSet set =new AnimationSet(false);

        AlphaAnimation aa = new AlphaAnimation(0, 1.0f);
        // 设置动画重复播放的次数
        aa.setRepeatCount(2);
        // 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
        aa.setRepeatMode(AlphaAnimation.REVERSE);
        // 设置动画重复播放的总时间长度
        aa.setDuration(3000);

        set.addAnimation(aa);

        RotateAnimation ra = new RotateAnimation(0, 360,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f);
        // 设置动画重复播放的次数
        ra.setRepeatCount(2);
        // 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
        ra.setRepeatMode(AlphaAnimation.REVERSE);
        // 设置动画重复播放的总时间长度
        ra.setDuration(3000);
        set.addAnimation(ra);

        ScaleAnimation sa = new ScaleAnimation(0, 2.0f, 0, 2.0f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f);
        // 设置动画重复播放的次数
        sa.setRepeatCount(2);
        // 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
        sa.setRepeatMode(AlphaAnimation.REVERSE);
        // 设置动画重复播放的总时间长度
        sa.setDuration(3000);
        set.addAnimation(sa);

        TranslateAnimation ta = new TranslateAnimation(
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 3.0f,
                TranslateAnimation.RELATIVE_TO_SELF, 0,
                TranslateAnimation.RELATIVE_TO_SELF, 3.0f);
        // 设置动画重复播放的次数
        ta.setRepeatCount(2);
        // 设置动画重复播放的顺序的模式:REVERSE 相反的顺序
        ta.setRepeatMode(AlphaAnimation.REVERSE);
        // 设置动画重复播放的总时间长度
        ta.setDuration(3000);

        set.addAnimation(ta);


        iv.startAnimation(set);

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的FGUI开宝箱动画demo: 1. 首先,在FairyGUI编辑器中,创建一个容器组件,命名为"ChestContainer",并将宝箱和动画元素添加到该容器中。 2. 为动画元素添加Animation组件,并创建一个AnimationClip动画剪辑。在动画剪辑中,添加两个关键帧,一个表示宝箱关闭状态,一个表示宝箱打开状态。在每个关键帧中,设置动画元素的位置和旋转角度,以实现开启和关闭动画。 3. 在代码中,通过以下方式获取容器组件和动画元素的引用: ```csharp GComponent chestContainer = this.GetChild("ChestContainer").asCom; GObject chest = chestContainer.GetChild("Chest"); GObject animationElement = chestContainer.GetChild("AnimationElement"); ``` 4. 调用动画元素的Play方法,触发宝箱的开启和关闭动画: ```csharp Animation animation = animationElement.asCom.GetTransition("ChestTransition"); animation.Play(onComplete: () => { Debug.Log("Chest animation completed!"); }); ``` 完整的代码示例: ```csharp using UnityEngine; using FairyGUI; public class ChestDemo : MonoBehaviour { private GComponent chestContainer; private GObject chest; private GObject animationElement; void Start() { // 获取容器组件和动画元素的引用 chestContainer = this.GetChild("ChestContainer").asCom; chest = chestContainer.GetChild("Chest"); animationElement = chestContainer.GetChild("AnimationElement"); // 注册点击事件,触发宝箱开启动画 chest.onClick.Add(() => { Animation animation = animationElement.asCom.GetTransition("ChestTransition"); animation.Play(onComplete: () => { Debug.Log("Chest animation completed!"); }); }); } } ``` 这样,当点击宝箱时,就可以触发开启动画了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值