Android View 動畫框架

最近在溫習(xí)一下舊的姿勢(知識)酝锅,順便熟練一下markdown,做個筆記。壕曼。


視圖動畫

  • 透明度動畫

    AlphaAnimation aa = new AlphaAnimation(0,1);
    aa.setDuration(1000);
    view.starttAnimation(aa);
    
  • 旋轉(zhuǎn)動畫

    /**
    * @param  ①旋轉(zhuǎn)的初始角度  ②旋轉(zhuǎn)的終止角度  ③ ④  旋轉(zhuǎn)中心的橫縱坐標(biāo),也可以用自身當(dāng)作參考系
    */
    RotateAnimation ra = new RotateAnimation(0,360,100,100)
    ra.setDuration(1000);
    view.startAnimation(ra);
    
  • 位移動畫

    /**
    *@param   始末位置的橫縱坐標(biāo)
    */
    TranslateAnimation ta = new TranslateAnimation(0,200,0,300);
    ta.setDuration(1000);
    view.startAnimation(ta);
    
  • 縮放動畫

    /*
    * @param   縮放的中心點
    */
    ScaleAnimation sa = new ScaleAnimation(0,2,0,2);
    sa.setDuratuon(1000);
    view.startAnimation(sa);
    
  • 動畫集合

    AnimationSet as = new AnimationSet(true);
    as.addAnimation(animation1);
    as.addAnimation(animation2);
    .....
    view.startAnimation(as)
    

屬性動畫

1. ObjectAnimator

/*
* @param    ①操作的View   ②要操縱的屬性  ③設(shè)置屬性的變化值 
*/
ObjectAnimator animator = ObjectAnimator.ofFloat(view,"translateX",300);
animator.setDuration(1000);
animator.start();
  • 要操縱的屬性必須具有g(shù)et月褥、set方法,不然ObjectAnimator就無法起效
    • translationX translationY
    • rotationrotationX rotationY
    • scaleX scaleY
    • x和y
    • alpha

2. PropertyValuesHolder

類似于視圖動畫中的AniamtionSet

 PropertyValuesHolder translationY = PropertyValuesHolder.ofFloat("translationY", 100f);
        PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("ScaleX", 1f, 0, 1f);
        PropertyValuesHolder sacleY = PropertyValuesHolder.ofFloat("ScaleY", 1f, 0, 1f);
        ObjectAnimator.ofPropertyValuesHolder(mImage,translationY,scaleX,sacleY).setDuration(1000).start();

3.ValueAnimator

本身不提供任何動畫效果屯曹,可以在AnimatorUpdateListener中監(jiān)聽數(shù)值的變換

ValueAnimator valueAnimator = ValueAnimator.ofInt(start, end);

        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                int animatedValue = (int) animation.getAnimatedValue();

            }
        });

4.AnimatorSet

ObjectAnimator translationX = ObjectAnimator.ofFloat(mImage, "translationX", 300f);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(mImage, "ScaleX", 1f, 0, 1f);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(mImage, "ScaleY", 1f, 0, 1f);
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.setDuration(1000);
        animatorSet.playTogether(translationX,scaleX,scaleY);
        animatorSet.start();

5.在XML中使用動畫

<objectAnimator
        android:duration="1000"
        android:propertyName="scaleX"
        android:valueFrom="1.0"
        android:valueTo="2.0"
        android:valueType="floatType"/>
 
Animator animator = AnimatorInflater.loadAnimator(this, R.animator.scale_animator);
        animator.setTarget(mImage);
        animator.start();

6.View的animate方法

mImage.animate()
                .alpha(1)    //設(shè)置透明度
                .y(300)      //設(shè)置 坐標(biāo)
                .setDuration(1000)   //設(shè)置動畫時長
                .withStartAction(new Runnable() {
                    @Override
                    public void run() {

                    }
                })
                .withEndAction(new Runnable() {
                    @Override
                    public void run() {

                    }
                })
                .start();

布局動畫

所謂的布局動畫是指在ViewGroup上狱庇,給ViewGroup增加View時添加一個動畫過渡效果

Android:animateLayoutChanges = "true"

mViewGroup = (LinearLayout) findViewById(R.id.imagegroup);
        //設(shè)置過渡動畫
        ScaleAnimation scaleAnimation = new ScaleAnimation(0, 1, 0, 1);
        scaleAnimation.setDuration(2000);
        //設(shè)置布局動畫的顯示屬性
        LayoutAnimationController lac = new LayoutAnimationController(scaleAnimation, 0.5f);
        lac.setOrder(LayoutAnimationController.ORDER_NORMAL);
        //給ViewGroup設(shè)置布局動畫
        mViewGroup.setLayoutAnimation(lac);
  • LayoutAnimationController.ORDER_NORMAL——順序
  • LayoutAnimationController.ORDER_RANDOM ——隨機(jī)
  • LayoutAnimationController.ORDER_REVERSE——反序

Interpolators(插值器)

定義動畫的變換速率,類似于物理中的加速度

android:interpolator="@android:anim/accelerate_interpolator" 設(shè)置動畫為加速動畫(動畫播放中越來越快)  
  
android:interpolator="@android:anim/decelerate_interpolator" 設(shè)置動畫為減速動畫(動畫播放中越來越慢)  
  
android:interpolator="@android:anim/accelerate_decelerate_interpolator" 設(shè)置動畫為先加速在減速(開始速度最快 逐漸減慢)  
  
android:interpolator="@android:anim/anticipate_interpolator" 先反向執(zhí)行一段恶耽,然后再加速反向回來(相當(dāng)于我們彈簧密任,先反向壓縮一小段,然后在加速彈出)  
  
android:interpolator="@android:anim/anticipate_overshoot_interpolator" 同上先反向一段偷俭,然后加速反向回來浪讳,執(zhí)行完畢自帶回彈效果(更形象的彈簧效果)  
  
android:interpolator="@android:anim/bounce_interpolator" 執(zhí)行完畢之后會回彈跳躍幾段(相當(dāng)于我們高空掉下一顆皮球,到地面是會跳動幾下)  
  
android:interpolator="@android:anim/cycle_interpolator" 循環(huán)涌萤,動畫循環(huán)一定次數(shù)淹遵,值的改變?yōu)橐徽液瘮?shù):Math.sin(2* mCycles* Math.PI* input)  
  
android:interpolator="@android:anim/linear_interpolator" 線性均勻改變  
  
android:interpolator="@android:anim/overshoot_interpolator" 加速執(zhí)行,結(jié)束之后回彈 

自定義動畫

繼承Animation 重寫initialize () 以及 applyTransformation()方法

 @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        //設(shè)置默認(rèn)時長
        setDuration(5000);
        //動畫結(jié)束后保留狀態(tài)
        setFillAfter(true);
        //設(shè)置默認(rèn)的插值器
        setInterpolator(new BounceInterpolator());

        this.mCenterWidth = width/2;
        this.mCenterHeight = height/2;
    }
 @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        super.applyTransformation(interpolatedTime, t);
        this.mCamera = new Camera();

        Matrix matrix = t.getMatrix();
        mCamera.save();
        //使用Camera設(shè)置旋轉(zhuǎn)的角度
        mCamera.rotateX(20*interpolatedTime);
        //將旋轉(zhuǎn)變換作用到Metrix上
        mCamera.getMatrix(matrix);
        mCamera.restore();
        //通過pre 方法設(shè)置矩陣作用前的的偏移量來改變旋轉(zhuǎn)中心
        matrix.preTranslate(mCenterWidth,mCenterHeight);
        matrix.postTranslate(-mCenterWidth,-mCenterHeight);

    }

SVG (Scalable Vector Graphics ) 5.0

path

使用<path>標(biāo)簽創(chuàng)建SVG 常用的指令

  • L 繪制直線
  • M 移動到某一坐標(biāo)
  • A 繪制弧線
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:height = "200dp"
    android:width = "200dp"
    android:viewportHeight = "100"
    android:viewportWidth = "100">

    <group
        android:name="test"
        android:pivotX="36"
        android:pivotY="36"
        android:rotation="0">
        <path
            android:name="v"
            android:fillColor="@color/colorAccent"
            android:pathData="M 25 50 a 25,25 0 1,0 50,0 L 25 80"
            android:strokeColor="@color/colorPrimary"
            android:strokeWidth="2"
            />
    </group>
</vector>

AnimatedVectorDrawable

  1. 在res的drawable文件夾下通過<animated-vector>標(biāo)簽來聲明對AnimatedVectorDrawable的使用形葬,并指定其作用的path或group.

    <animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:drawable="@drawable/yuanhu"
        tools:targetApi="lollipop">
    
        <target
            android:animation="@anim/rotate_animator"
            android:name="test"/>
    </animated-vector>
    
  2. 對應(yīng)的vector即為靜態(tài)的VectorDrawable.

    注意:target 中name的屬性和vector中需要作用的name屬性保持一致

  3. 通過animation屬性合呐,將一個動畫作用到了對應(yīng)name的元素上,objectAnimator的代碼如下:

    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <objectAnimator
            android:duration="4000"
            android:propertyName="rotation"
            android:valueFrom="0"
            android:valueTo="360" />
    </set>
    
  4. 將AnimatrdVectorDrawable XML 文件設(shè)置給一個ImageView 作為背景顯示

    <ImageView
                android:id="@+id/vectorimage"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:src="@drawable/animator_yuanhu"/>
    
  5. 在代碼中控制SVG動畫

    mVector = (ImageView) findViewById(R.id.vectorimage);
    ((Animatable) mVector.getDrawable()).start();
    
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末笙以,一起剝皮案震驚了整個濱河市淌实,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌猖腕,老刑警劉巖拆祈,帶你破解...
    沈念sama閱讀 219,110評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異倘感,居然都是意外死亡放坏,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,443評論 3 395
  • 文/潘曉璐 我一進(jìn)店門老玛,熙熙樓的掌柜王于貴愁眉苦臉地迎上來淤年,“玉大人钧敞,你說我怎么就攤上這事◆锪福” “怎么了溉苛?”我有些...
    開封第一講書人閱讀 165,474評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長弄诲。 經(jīng)常有香客問我愚战,道長,這世上最難降的妖魔是什么齐遵? 我笑而不...
    開封第一講書人閱讀 58,881評論 1 295
  • 正文 為了忘掉前任寂玲,我火速辦了婚禮,結(jié)果婚禮上梗摇,老公的妹妹穿的比我還像新娘拓哟。我一直安慰自己,他們只是感情好伶授,可當(dāng)我...
    茶點故事閱讀 67,902評論 6 392
  • 文/花漫 我一把揭開白布彰檬。 她就那樣靜靜地躺著,像睡著了一般谎砾。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上捧颅,一...
    開封第一講書人閱讀 51,698評論 1 305
  • 那天景图,我揣著相機(jī)與錄音,去河邊找鬼碉哑。 笑死挚币,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的扣典。 我是一名探鬼主播妆毕,決...
    沈念sama閱讀 40,418評論 3 419
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼贮尖!你這毒婦竟也來了笛粘?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,332評論 0 276
  • 序言:老撾萬榮一對情侶失蹤湿硝,失蹤者是張志新(化名)和其女友劉穎薪前,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體关斜,經(jīng)...
    沈念sama閱讀 45,796評論 1 316
  • 正文 獨居荒郊野嶺守林人離奇死亡示括,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,968評論 3 337
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了痢畜。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片垛膝。...
    茶點故事閱讀 40,110評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡鳍侣,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出吼拥,到底是詐尸還是另有隱情倚聚,我是刑警寧澤,帶...
    沈念sama閱讀 35,792評論 5 346
  • 正文 年R本政府宣布扔罪,位于F島的核電站秉沼,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏矿酵。R本人自食惡果不足惜唬复,卻給世界環(huán)境...
    茶點故事閱讀 41,455評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望全肮。 院中可真熱鬧敞咧,春花似錦、人聲如沸辜腺。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,003評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽评疗。三九已至测砂,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間百匆,已是汗流浹背砌些。 一陣腳步聲響...
    開封第一講書人閱讀 33,130評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點兒被人妖公主榨干…… 1. 我叫王不留加匈,地道東北人存璃。 一個月前我還...
    沈念sama閱讀 48,348評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像雕拼,于是被迫代替她去往敵國和親纵东。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 45,047評論 2 355

推薦閱讀更多精彩內(nèi)容

  • 原理: 繪制view時會調(diào)用viewGroup中的drawChild函數(shù)時啥寇,獲取view的Animation的Tr...
    xbase閱讀 1,324評論 0 1
  • 上一章 書中的示例代碼:github 本章主要介紹的是Android動畫機(jī)制和使用技巧 1.Android視圖動畫...
    青藤綠閱讀 1,663評論 3 32
  • 1 背景 不能只分析源碼呀偎球,分析的同時也要整理歸納基礎(chǔ)知識,剛好有人微博私信讓全面說說Android的動畫辑甜,所以今...
    未聞椛洺閱讀 2,711評論 0 10
  • 人海漂浮甜橱,你說很向往《愛在黎明前》里的那對傳奇情侶,在黎明破曉前相愛栈戳,在日落之前重逢岂傲。 你們也相遇相愛了,在黎明破...
    cheryl喲閱讀 690評論 6 5
  • 夢想早安7 神看顧美好的一天開始了子檀!有美好的事發(fā)生我身上镊掖! 我的夢想是想擁有自己的車乃戈!我一定要幫...
    梁杏儀閱讀 253評論 0 0