三种动画的简单用法——学习笔记

AlphaAnimation——透明度渐变动画
ScaleAnimation ——缩放动画
RotateAnimation——旋转动画

①ScaleAnimation动画相关方法的参数:
ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

参数说明:

float fromX 动画起始时 X坐标上的伸缩尺寸
float toX 动画结束时 X坐标上的伸缩尺寸
float fromY 动画起始时Y坐标上的伸缩尺寸
float toY 动画结束时Y坐标上的伸缩尺寸
int pivotXType 动画在X轴相对于物件位置类型
float pivotXValue 动画相对于物件的X坐标的开始位置
int pivotYType 动画在Y轴相对于物件位置类型
float pivotYValue 动画相对于物件的Y坐标的开始位置

②RotateAnimation动画相关方法的参数
RotateAnimation (float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

参数说明:

float fromDegrees:旋转的开始角度。
float toDegrees:旋转的结束角度。
int pivotXType:X轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
float pivotXValue:X坐标的伸缩值。
int pivotYType:Y轴的伸缩模式,可以取值为ABSOLUTE、RELATIVE_TO_SELF、RELATIVE_TO_PARENT。
float pivotYValue:Y坐标的伸缩值。

以下是一个简单的例子

布局界面代码为:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/splash_bg"
    android:id="@+id/splash_root">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_laugh"
        android:layout_centerInParent="true"/>

</RelativeLayout>

Activity代码为:

public class SplashActivity extends AppCompatActivity {
    private RelativeLayout splash_rl_root;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash);
        splash_rl_root = (RelativeLayout) findViewById(R.id.splash_root);

        //动画
        AlphaAnimation aa = new AlphaAnimation(0, 1); //渐变动画
        //aa.setDuration(500); //持续播放时间
        aa.setFillAfter(true); //动画结束后保持状态

        ScaleAnimation sa = new ScaleAnimation(0, 1, 0, 1, ScaleAnimation.RELATIVE_TO_SELF, 0.5f,
                ScaleAnimation.RELATIVE_TO_SELF, 0.5f); //缩放动画
        //sa.setDuration(500);
        sa.setFillAfter(true);

        RotateAnimation ra = new RotateAnimation(0, 360, RotateAnimation.RELATIVE_TO_SELF, 0.5f,
                RotateAnimation.RELATIVE_TO_SELF, 0.5f); //旋转动画
       // ra.setDuration(500);
        ra.setFillAfter(true);

        AnimationSet set = new AnimationSet(false); //是否加速
        //添加三个动画没有先后顺序
        set.addAnimation(aa);
        set.addAnimation(sa);
        set.addAnimation(ra);
        set.setDuration(2000); //同时设置播放时间

        splash_rl_root.startAnimation(set); //同时播放三个动画
        set.setAnimationListener(new MyAnimationListener()); //给动画设置一个监听

    }

    class MyAnimationListener implements Animation.AnimationListener{

        /*
        当动画开始播放的时候调用
         */
        @Override
        public void onAnimationStart(Animation animation) {

        }

        /*
        当动画播放结束的时候调用
         */
        @Override
        public void onAnimationEnd(Animation animation) {
            Toast.makeText(SplashActivity.this, "动画播放完成!", Toast.LENGTH_SHORT).show();
        }

        /*
        当动画重复播放的时候调用
         */
        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    }
}

下面是运行图片
这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值