標籤:new 自己 代碼 http 也有 xmlns anim bounce cycle
1.代碼實現混合動畫的方式,這裡用到了AnimationSet這個Animation的子類來實現,從底層看該類實現了Animation的大多數方法,建構函式也有好幾個。
AnimationSet animationSet = new AnimationSet(true);//這裡的ture表示所有的動畫都使用同一變速器,也就是在布局檔案中的set裡設定shareInterpolator="true"
AlphaAnimation alphaAnimation = new AlphaAnimation(1, 0);//透明度動畫從1到0
RotateAnimation rotateAnimation = new RotateAnimation(0, 360,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);//旋轉動畫相對於自己的中心位置,旋轉角度從0到360
TranslateAnimation translateAnimation = new TranslateAnimation(0, 100, 0, 100);//位移動畫相對於控制項當前的位置,x座標從0到100,y座標也是從0到100
ScaleAnimation scaleAnimation=new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,
0.5f,0.5f); //縮放動畫相對於中心位置進行擴大
animationSet.setDuration(3000); //設定動畫期間
animationSet.addAnimation(alphaAnimation); //添加到動畫容器中去
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(translateAnimation);
animationSet.addAnimation(scaleAnimation);
image.startAnimation(animationSet); //控制項開始播放動畫
2.xml檔案實現
在activity中的代碼如下:
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.set_animation); //使用AnimationUtils載入布局檔案
image.startAnimation(animation); //開始播放動畫
在布局檔案中的實現:(每種動畫都伴隨著相應的參數資訊,只要按照相應的操作就行了)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:shareInterpolator="true"
android:duration="3000">
<scale
android:fromXScale="0.0"
android:fromYScale="0.0"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1.5"
android:toYScale="1.5" />
<rotate
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" />
<alpha
android:fromAlpha="0"
android:toAlpha="1" />
<translate
android:fromXDelta="0"
android:toXDelta="100"
android:fromYDelta="0"
android:toYDelta="100"
/>
</set>
這裡變速器interpolator預設是accelorateDecelorateInterpolator的類型,如果要設定不同的變速器類型,google公司提供了幾種類型:
AccelerateInterpolator:先加速後勻速AnticipateInterpolator:先慢慢減速後加速AnticipateOvershootInterpolator:變化多樣的變速器BounceInterpolator:有彈起效果的變速器CycleInterpolator:具有周期性的變速器DecelerateInterpolator:減速型的變速器LinearInterpolator:勻速型的變速器
個人源碼地址:https://github.com/ailibin/MyAnimation.git
安卓動畫兩種基本實現方式