Android 中Animations的使用 xml篇

Animations是Android的UI实现动态效果的API。

主要包含两类,一类是Tweened Animations, 一类是是frame-by-frame Animations。

第一类是对一个view的处理,而第二类是对一个drawable序列的处理。

这两类效果既可以在代码中实现,也可以在xml中实现。

这里介绍下在xml中的实现。

Tweened Animations提供了rotate(旋转)、alpha(淡入淡出)、translate(移动)、Scale(缩放)四种效果。

在new xml中RT选择Tween Animation,选择set新建。



rotate:
< set xmlns:android= "http://schemas.android.com/apk/res/android" >

    <!-- 动画时间 -->
    <!-- 开始角度 -->
    <!-- 结束角度 -->
    <!-- 逆时针旋转270度 -->
    <rotate
        android:duration= "2000"
        android:fromDegrees= "0"
        android:toDegrees= "270" />

</ set>

alpha:
< set xmlns:android= "http://schemas.android.com/apk/res/android" >

    <!-- 动画时间 -->
    <!-- 开始透明度 -->
    <!-- 结束透明度 -->
   
    <alpha
        android:duration= "2000"
        android:fromAlpha= "1"
        android:toAlpha= "0.1" />

</ set>

scale:
< set xmlns:android= "http://schemas.android.com/apk/res/android" >

    <!-- 动画时间 -->
    <!-- x,y轴起始压缩 -->
    <!-- x,y轴结束压缩 -->
   
    <scale
        android:duration= "2000"
        android:fromXScale= "0"
        android:fromYScale= "1"
        android:toXScale= "1"
        android:toYScale= "1" />

</ set>

translate:
< set xmlns:android= "http://schemas.android.com/apk/res/android" >
   
    <!-- x,y轴起始坐标 -->
    <!-- x,y轴结束坐标 -->
    <!-- 动画时间 -->
   
    <translate
        android:fromXDelta= "0%"
        android:toXDelta= "100%"
        android:fromYDelta= "0%"
        android:toYDelta= "100%"
        android:duration= "2000" />

</ set>

在代码中引用:
View view = findViewById(R.id. img);
Animation animation = AnimationUtils.loadAnimation(MainActivity. this, R.anim. alpha);
view.setAnimation(animation);

frame-by-frame Animations,可以用于显示gif图,直接使用第三方库+gif图,难免会报OOM。

在new xml中RT选择Drawable,选择animation-list新建mylist。


< animation-list xmlns:android ="http://schemas.android.com/apk/res/android" >
    <item android:duration = "40" android:drawable ="@drawable/icon_000" />
      <item android:duration = "40" android:drawable ="@drawable/icon_001" />
      <item android:duration = "40" android:drawable ="@drawable/icon_002" />
      <item android:duration = "40" android:drawable ="@drawable/icon_003" />
      <item android:duration = "40" android:drawable ="@drawable/icon_004" />
      <!-- 只要有帧图,就还可以继续往下写 -->
 
</ animation-list>

引用:
ImageView img = (ImageView) findViewById(R.id.img);
img.setBackgroundResource(R.anim. mylist);
AnimationDrawable animationDrawable = (AnimationDrawable) img.getBackground();
animationDrawable.start();


ctrlz presents!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值