动画

Android的三种动画

View Animation(视图动画)
Drawable Animation(帧动画)
Property Animation(属性动画)

帧动画

顺序播放一组图片 用AnimationDrawable使用帧动画
帧动画如果帧数过多,容易引起OOM

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">
<!--   oneshot 是否执行一次-->

   <item android:drawable="@drawable/r1" android:duration="50"/>
   <item android:drawable="@drawable/r2" android:duration="50"/>
   <item android:drawable="@drawable/r3" android:duration="50"/>
   <item android:drawable="@drawable/r4" android:duration="50"/>
   <item android:drawable="@drawable/r5" android:duration="50"/>
   <item android:drawable="@drawable/r6" android:duration="50"/>
   <item android:drawable="@drawable/r7" android:duration="50"/>
   <item android:drawable="@drawable/r8" android:duration="50"/>
   <item android:drawable="@drawable/r9" android:duration="50"/>
   <item android:drawable="@drawable/r10" android:duration="50"/>
   <item android:drawable="@drawable/r11" android:duration="50"/>
   <item android:drawable="@drawable/r12" android:duration="50"/>
   <item android:drawable="@drawable/r13" android:duration="50"/>
   <item android:drawable="@drawable/r14" android:duration="50"/>
   <item android:drawable="@drawable/r15" android:duration="50"/>
   <item android:drawable="@drawable/r16" android:duration="50"/>

</animation-list>

MainActicity:

   ImageView imageView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageView=findViewById(R.id.imageview);
        
        AnimationDrawable background = (AnimationDrawable) imageView.getBackground();
        background.start();
    }


视图动画

视图动画的操作对象是view,支持四种效果,透明、旋转、缩放、位移。
set可以当作容器,把动画效果放在一起,也可以单个动画。
XML:

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

<!--    透明-->
    <alpha
        android:duration="4000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0"
        android:repeatCount="2"
        />
<!--   旋转-->
    <rotate
        android:duration="4000"
        android:fromDegrees="0"
        android:toDegrees="720"
        android:pivotX="50"
        android:pivotY="50%"
        />
<!--    缩放-->
    <scale
        android:duration="4000"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:toXScale="0.0"
        android:toYScale="0.0"
        android:pivotY="50%"
        android:pivotX="50%"
        />
<!--    位移-->
    <translate
        android:duration="4000"
        android:fromXDelta="0"
        android:toXDelta="100"
        android:fromYDelta="0"
        android:toYDelta="100"
        android:fillAfter="true"
        />
</set>

MainActivity:

//设置到view上
    Animation animation = AnimationUtils.loadAnimation(this,R.anim.a);
    textView.startAnimation(animation);

动画监听:

 Animation.addListener(new AnimatorListener() {
          @Override
          public void onAnimationStart(Animation animation) {
              //动画开始时执行
          }

           @Override
          public void onAnimationRepeat(Animation animation) {
              //动画重复时执行
          }

         @Override
          public void onAnimationCancel()(Animation animation) {
              //动画取消时执行
          }

          @Override
          public void onAnimationEnd(Animation animation) {
              //动画结束时执行
          }
      });

xml属性Java方法说明:
android:detachWallpaper setDetachWallpaper(boolean)是否在壁纸上运行
android:duration setDuration(long)动画的运行时间(以毫秒为单位);必须设置
android:fillAfter setFillAfter(boolean)动画结束时是否保持动画最后的状态;默认为false,优先于fillBefore
android:fillBefore setFillBefore(boolean)动画结束时是否还原到开始动画前的状态;默认为true
android:fillEnabled setFillEnabled(boolean)是否应用fillBefore的值,对fillAfter无影响;默认为true
android:interpolator setInterpolator(Interpolator)设定插值器(指定的动画效果,譬如回弹等)android:repeatCount setRepeatCount(int)重复次数
android:repeatMode setRepeatMode(int)重复类型有两个值,reverse表示倒序回放,restart表示从头播放
android:startOffset setStartOffset(long)调用start函数之后等待开始运行的时间,单位为毫秒android:zAdjustment setZAdjustment(int)表示被设置动画的内容运行时在Z轴上的位置(top/bottom/normal),默认为normal

属性动画

public void click(View view) {
        ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1, 0.5f, 0.5f, 1);//透明
        ObjectAnimator rotationY = ObjectAnimator.ofFloat(imageView, "rotationY", 360, 0, 0, 360); //旋转
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 0, 2, 2, 0); //缩放
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 0, 2, 2, 0);
        ObjectAnimator translationX = ObjectAnimator.ofFloat(imageView, "translationX", 0, 500); //位移

        //无线循环
        alpha.setRepeatCount(-1);
        rotationY.setRepeatCount(-1);
        scaleY.setRepeatCount(-1);
        scaleX.setRepeatCount(-1);
        translationX.setRepeatCount(-1);


        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(alpha).with(rotationY).with(rotationY).with(scaleX).with(scaleY).with(translationX); //依次播放效果
        animatorSet.setDuration(7000); //播放时长
        animatorSet.start(); 

    }

插值器

用在视图动画xml文件里

    android:interpolator="@android:anim/bounce_interpolator"
XML 资源ID说明
@android:anim/accelerate_decelerate_interpolator先加速再减速
@android:anim/accelerate_interpolator持续加速
@android:anim/anticipate_interpolator先退后再加速前进
@android:anim/anticipate_overshoot_interpolator先退后再加速前进,超出终点后再回终点
@android:anim/bounce_interpolator结束时弹球效果
@android:anim/cycle_interpolator周期运动
@android:anim/decelerate_interpolator减速
@android:anim/linear_interpolator匀速
@android:anim/overshoot_interpolator周期运动
@android:anim/cycle_interpolator向前弹出一定值之后回到原来位置(快速完成动画,超出再回到结束样式)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值