Android 动画详解之View动画

为了让用户的使用更舒适所以有些情况使用动画是很有必要的,Android在3.0以前支持俩种动画Tween动画以及Frame动画。Tween动画支持简单的平移,缩放,旋转,渐变,Frame动画就像Gif图通过一系列图片来模拟动画效果,而在Android 3.0以后引入了新的动画就是属性动画(property animation), Android 分享一个简单有趣的动画效果 就是利用了属性动画。


今天我们主要来学习Tween动画也就是View动画。

View 动画只能应用于View对象,而且只支持一部分属性,而且对于View 动画,它只是改变了View对象绘制的位置,而没有改变View对象本身,比如当前有一个button的坐标是(200,200)通过平移动画移动到(200,500),但是你点击移动后的button是没有任何效果,如下图:


知道了这个大前提我们就开始了解View动画的基本用法吧,动画可以用java代码写也可以用xml写

1,平移动画

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true"
    android:fromXDelta="0%"
    android:fromYDelta="0%"
    android:repeatCount="10"
    android:repeatMode="restart"
    android:toXDelta="0%"
    android:toYDelta="70%" >

</translate>
duration动画时间,fillAfter保持动画结束后状态,fromXDelta起始X位置,fromYDelta起始Y位置,repeatCount重复次数,repeatMode重复模式 restart为正序 reverse为倒序,在java代码中用

Animation animation =AnimationUtils.loadAnimation(this, R.anim.tran_btn);

view.startAnimation(animation);


<span style="font-size:18px;">Animation animation2 = new TranslateAnimation(0, 20, 0, 0);
		animation2.setDuration(2000);
		animation2.setRepeatCount(10);
		animation2.setRepeatMode(Animation.RESTART);
		button.startAnimation(animation2);</span>
new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta)
看参数相信大家也都知道意思了


2,旋转动画

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fromDegrees="0"
    android:toDegrees="360" 
    android:pivotX="50%"
    android:pivotY="50%"
    >
</rotate>

大致同上,pivot旋转的中心点.

	Animation animation2 = new RotateAnimation(0, 360, 0, 0);
		animation2.setDuration(2000);
		button.startAnimation(animation2);



3,渐变动画

<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromAlpha="1.0"
    android:toAlpha="0.0" >

</alpha></span>
其实看通一个剩下的都是触类旁通
<span style="font-size:18px;">Animation animation = new AlphaAnimation(1, 0);
		animation.setDuration(3000);
		button.startAnimation(animation);</span>


4,缩放动画

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fromXScale="0.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="1.0" >

</scale>



ok,这下四种基本动画都简单的结束了一下,但是我们有时可能会有一些特殊的需求,比如让播放一组动画,这时我们可以使用set

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

    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:toAlpha="0.0" />

</set>

这是旋转于渐变同时播放,如果依次播放的话只需加上startOffset

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

    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="360" />

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:startOffset="2000"
        android:toAlpha="0.0" />

</set>

这样就可以依次播放,但有时我们想要一下特殊的效果比如说动画的加速度,这时候我们可以用上interpolator,animation.setInterpolator(new AccelerateInterpolator());

 AccelerateDecelerateInterpolator 在动画开始与结束的地方速率改变比较慢,在中间的时候加速

 AccelerateInterpolator  在动画开始的地方速率改变比较慢,然后开始加速

 AnticipateInterpolator 开始的时候向后然后向前甩

 AnticipateOvershootInterpolator 开始的时候向后然后向前甩一定值后返回最后的值

 BounceInterpolator   动画结束的时候弹起

 CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线

 DecelerateInterpolator 在动画开始的地方快然后慢

 LinearInterpolator   以常量速率改变

 OvershootInterpolator    向前甩一定值后再回到原来位置


谢谢耐心的看完,不积跬步无以至千里,有什么疑问的话也可以留言。。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值