Android实现动画的实现方式

Android实现动画的方式有很多,其中第一种方式是
建立一个xml的文件写在里面

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- fromAlpha和toAlpha是起始透明度和结束时透明度 -->
    <alpha
        android:duration="500"
        android:fromAlpha="1.0"
        android:startOffset="500"
        android:toAlpha="0.0" />
    <!--旋转
       fromDegrees:开始的角度
       toDegrees:结束的角度,+表示是正的
       pivotX:用于设置旋转时的x轴坐标
       例(1)当值为"50",表示使用绝对位置定位(2)当值为"50%",表示使用相对于控件本身定位(3)当值为"50%p",表示使用相对于控件的父控件定位
       pivotY:用于设置旋转时的y轴坐标
     -->
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="+360" />

    <!-- 渐变尺寸缩小
         fromXScale 起始x轴坐标
         toXScale 止x轴坐标
         fromYScale 始y轴坐标
         toYScale 止y轴坐标
         toXScale 以X轴的坐标为轴
         toYScale 以Y轴的坐标为轴-->
    <scale
        android:duration="1000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.0"
        android:toYScale="0.0" />

    <!--  移动动画
         fromXDelta 始x轴坐标
         toXDelta 止x轴坐标
         fromYDelta 始y轴坐标
         toYDelta 止y轴坐标
     -->
    <translate
        android:fromXDelta="0%"
        android:toXDelta="100%"
        android:fromYDelta="0%"
        android:toYDelta="100%"
        android:duration="2000"/>
</set>
/第一个参数Context为程序的上下文    
//第二个参数id为动画XML文件的引用
//例子:
Animation myAnimation= AnimationUtils.loadAnimation(this,R.anim.my_anim);
//使用AnimationUtils类的静态方法loadAnimation()来加载XML中的动画XML文件

第二种方式

  //缩放
         从哪里开始
         item_gv_popupwindow.setPivotX(300);
                 item_gv_popupwindow.setPivotY(100);

//                ObjectAnimator
//                        .ofFloat(item_gv_popupwindow, "pivotX", this.getWallpaperDesiredMinimumWidth() / 2)//
//                        .setDuration(0)//  
//                        .start();
//                ObjectAnimator
//                        .ofFloat(item_gv_popupwindow, "scaleX", 0.0F, 1.0F)//
//                        .setDuration(3000)//  
//                        .start();
//                ObjectAnimator
//                        .ofFloat(item_gv_popupwindow, "scaleY", 0.0F, 1.0F)//
//                        .setDuration(3000)//  
                        .start();

                //旋转
//                ObjectAnimator
//                        .ofFloat(item_gv_popupwindow, "pivotX", this.getWallpaperDesiredMinimumWidth() / 4)//
//                        .setDuration(0)//  
//                        .start();
//                ObjectAnimator
//                        .ofFloat(item_gv_popupwindow, "rotationY", 0.0F, 360.0F)//
//                        .setDuration(3000)//  
//                        .start();

                //透明
//                ObjectAnimator
//                        .ofFloat(item_gv_popupwindow, "alpha", 0.0F, 1.0F)//
//                        .setDuration(3000)//  
//                        .start();
                //移动
//                ObjectAnimator
//                        .ofFloat(item_gv_popupwindow, "translationX", 100.0F, 0.0f,-50f,300.0F)//
//                        .setDuration(3000)//  
//                        .start();



//多种动画渐变写法
  PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("alpha", 0f, 1f);
                PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("scaleX", 0, 1f);
                PropertyValuesHolder pvhh = PropertyValuesHolder.ofFloat("pivotX", 300,400); 
                PropertyValuesHolder pvhi = PropertyValuesHolder.ofFloat("pivotY", 200,100);
                PropertyValuesHolder pvhZ = PropertyValuesHolder.ofFloat("scaleY", 0, 1f);
                ObjectAnimator.ofPropertyValuesHolder(item_gv_popupwindow, pvhX, pvhh,pvhi,pvhY, pvhZ).setDuration(3000).start();



//  同时执行动画
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",  
                1.0f, 2f);  
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",  
                1.0f, 2f);  
        AnimatorSet animSet = new AnimatorSet();  
        animSet.setDuration(2000);  
        animSet.setInterpolator(new LinearInterpolator());  
        //两个动画同时执行  
        animSet.playTogether(anim1, anim2);  
        animSet.start();  


//      anim1,anim2,anim3同时执行 anim4接着执行 
    float cx = mBlueBall.getX();  

        ObjectAnimator anim1 = ObjectAnimator.ofFloat(mBlueBall, "scaleX",  
                1.0f, 2f);  
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(mBlueBall, "scaleY",  
                1.0f, 2f);  
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(mBlueBall,  
                "x",  cx ,  0f);  
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(mBlueBall,  
                "x", cx);  

        /** 
         * anim1,anim2,anim3同时执行 
         * anim4接着执行 
         */  
        AnimatorSet animSet = new AnimatorSet();  
        animSet.play(anim1).with(anim2);  
        animSet.play(anim2).with(anim3);  
        animSet.play(anim4).after(anim3);  
        animSet.setDuration(1000);  
        animSet.start();  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值