動畫(一)

1.View動畫是通過平移,縮放,旋轉,透明度改變圖片的一種漸進式的動畫

2.幀動畫類似於放電影一樣切換一張張準備好的圖片而形成的動畫

3.屬性動畫是通過改變對象每個時段的屬性值而形成的動畫

注意:動畫中儘量使用dp做單位,使用硬件加速


一。View動畫

XML方式:

在res/anim/filename.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@[package:]anim/interpolator_resource"   //插值器
android:shareInterpolator="true" | "false"      //集合中動畫是否和集合共享插值器,若爲false則不共享
android:duration=“integer” //動畫持續時間
android:fillAfter="true|false" //動畫結束後是否停留在結束位置
 >         
<alpha                            //透明度
        android:fromAlpha="float"
        android:toAlpha="float" />
    <scale                            //縮放
        android:fromXScale="float"
        android:toXScale="float"
        android:fromYScale="float"
        android:toYScale="float"
        android:pivotX="float"
        android:pivotY="float" />
    <translate                         //位移
        android:fromXDelta="float"
        android:toXDelta="float"
        android:fromYDelta="float"
        android:toYDelta="float" />
    <rotate                           //旋轉
        android:fromDegrees="float"
        android:toDegrees="float"
        android:pivotX="float"
        android:pivotY="float" />
    <set>
        ...
    </set>
</set>

使用上面的動畫:

TextView v = (TextView)findViewById(R.id.test);
Animation a = AnimationUtils.loadAnimation(this,R.anim.filename);
v.startAnimation(a);


代碼方式:

TextView v = (TextView)findViewById(R.id.test);
AlphaAnimation a = new AlphaAnimation(0,1);
a.setDuration(300);
v.startAnimation(a);
TranslateAnimation/ScaleAnimation/RotateAnimation用法同上


注意:View使用setVisbility(View.GONE)失效時,可以使用view.clearAnimation清除


可以通過setAnimationListener方法設置AnimationListener來對動畫進行監聽,在動畫開始,結束,和重複播放時進行操作

a.setAnimationListener(new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(Animation animation) {
        
    }

    @Override
    public void onAnimationEnd(Animation animation) {

    }

    @Override
    public void onAnimationRepeat(Animation animation) {

    }
});


當以上動畫不能滿足要求時,我們可以通過繼承Animation進行動畫自定義

需要重寫initialize和applyTransformation兩個方法

public class a extends Animation{

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        //完成一些初始化的工作:如mCamera = new Camera();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
           //當前值 = 初始值 + (最終值 - 初始值) * interpolatedTime;
           Matrix matrix = t.getMatrix();
         //通過camera進行一些矩陣操作,最後對matrix進行變化
    }
}


二。幀動畫

在res/drawable/filename.xml:

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot=["true" | "false"] >
    <item
        android:drawable="@[package:]drawable/drawable_resource_name"
        android:duration="integer" />
</animation-list>

使用以上動畫:

TextView v = (TextView)findViewById(R.id.test);
v.setBackgroundResource(R.drawable.filename);
AnimationDrawable a = (AnimationDrawable)v.getBackground();
a.start();
注意:圖片數量多且圖片較大容易出現OOM,儘量避免使用幀動畫


三。LayoutAnimation

xml形式:

res/anim/finename.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
android:delay="float"  
android:animationOrder="normal|reverse|random”//子元素按什麼順序播放動畫   正序/逆序/隨機
android:animation="@anim/~"/>      //動畫樣式
在ViewGroup的xml文件中指定android:layoutAnimation = "@anim/filename"

則ViewGroup入場時,他的item就會開始播放動畫


代碼形式:

ListView v = (ListView)findViewById(R.id.listview);
Anaimation a = AnimationUtils.loadAnimation(this,R.anim.a);
LayoutAnimationController con = new LayoutAnimationController(a);
con.setDelay(0.5f);
con.setOrder(LayoutAnimationController.ORDER_REVERSE);
v.setLayoutAnimation(con);

四.屬性動畫

ObjectAnimator實現屬性動畫

ObjectAnimator.ofFloat(Object,"alpha",1,0,1).start();

ValueAnimator實現屬性動畫:

ValueAnimator anim = ObjectAnimator.ofInt(this,"backgroundColor",0xffffffff,0x00000000);
anim.setDuration(1000);
anim.setEvaluator(new ArgbEvalutor);    //顏色估值器
anim.setRepeatCount(ValueAnimator.INFINITE);
anim.setRepeatMode(ValueAnimator.REVERSE);
anim.start();

屬性動畫的監聽器AnimatorListener,AnimatorUpdateListener.
AnimatorUpdateListener:動畫每播放一幀,調用一次
AnimatorListener:監聽動畫開始,結束,取消以及重複播放
注意:屬性動畫有一類無限循環動畫在Activity退出時要讓它停止
否則會讓Activity無法釋放而造成內存泄露


動畫集合:
xml形式:

<set
android:ordering=["together" | "sequentially"]>      //集合中的動畫是同時播放還是依次播放
<objectAnimator
android:propertyName="string"      //屬性名
android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
android:startOffset="int"
android:repeatCount="int"           //重複次數
android:repeatMode=["repeat" | "reverse"]      
android:valueType=["intType" | "floatType"]/>
<animator
 android:duration="int"
android:valueFrom="float | int | color"
android:valueTo="float | int | color"
 android:startOffset="int"
android:repeatCount="int"
 android:repeatMode=["repeat" | "reverse"]
android:valueType=["intType" | "floatType"]/>
<set>
...
</set>
</set>
使用以上動畫:
TextView v = (TextView)findViewById(R.id.textview);
AnimatorSet set = (AnimatorSet)AnimatorInflater.loadAnimator(this,R.anim.filename);
set.setTarget(v);
set.start();


代碼形式:
//利用AnimatorSet和ObjectAnimator實現縮放動畫
final AnimatorSet animatorSet = new AnimatorSet();
image.setPivotX(image.getWidth()/2);
image.setPivotY(image.getHeight()/2);
animatorSet.playTogether(ObjectAnimator.ofFloat(image, "scaleX", 1, 0).setDuration(5000),
ObjectAnimator.ofFloat(image, "scaleY", 1, 0).setDuration(5000));
animatorSet.start();


參考:Android的View動畫和屬性動畫


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章