Android開發學習——動畫,android開發動畫

來源:互聯網
上載者:User

Android開發學習——動畫,android開發動畫

幀動畫
> 一張張圖片不斷的切換,形成動畫效果

* 在drawable目錄下定義xml檔案,子節點為animation-list,在這裡定義要顯示的圖片和每張圖片的顯示時間長度    
        <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false">
            <item android:drawable="@drawable/g1" android:duration="200" />
            <item android:drawable="@drawable/g2" android:duration="200" />
            <item android:drawable="@drawable/g3" android:duration="200" />
        </animation-list>
* 在螢幕上播放幀動畫
        ImageView iv = (ImageView) findViewById(R.id.iv);
        //把動畫檔案設定為imageView的背景
        iv.setBackgroundResource(R.drawable.animations);
        AnimationDrawable ad = (AnimationDrawable) iv.getBackground();
        //播放動畫        
        ad.start();

 

補間動畫
* 原形態變成新形態時為了過渡變形過程,產生的動畫就叫補間動畫

位移:
* 參數10指的是X的起點座標,但不是指螢幕x座標為10的位置,而是imageview的 真實X + 10
* 參數150指的是X的終點座標,它的值是imageview的 真實X + 150
    
        //建立為位移動畫對象,設定動畫的初始位置和結束位置
        TranslateAnimation ta = new TranslateAnimation(10, 150, 20, 140);
* x座標的起點位置,如果相對於自己,傳0.5f,那麼起點座標就是 真實X + 0.5 * iv寬度
* x座標的終點位置,如果傳入2,那麼終點座標就是 真實X + 2 * iv的寬度
* y座標的起點位置,如果傳入0.5f,那麼起點座標就是 真實Y + 0.5 * iv高度
* y座標的終點位置,如果傳入2,那麼終點座標就是 真實Y + 2 * iv高度
    
  TranslateAnimation ta = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2,      Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 2);

* 動畫播放相關的設定
        //設定動畫期間
        ta.setDuration(2000);
        //動畫重複播放的次數
        ta.setRepeatCount(1);
        //動畫重複播放的模式
        ta.setRepeatMode(Animation.REVERSE);
        //動畫播放完畢後,組件停留在動畫結束的位置上
        ta.setFillAfter(true);
        //播放動畫
        iv.startAnimation(ta);
縮放:
* 參數0.1f表示動畫的起始寬度是真實寬度的0.1倍
* 參數4表示動畫的結束寬度是真實寬度的4倍
* 縮放的中心點在iv左上方
        ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4);
* 參數0.1f和4意義與上面相同
* 改變縮放的中心點:傳入的兩個0.5f,類型都是相對於自己,這兩個參數改變了縮放的中心點
* 中心點x座標 = 真實X + 0.5 * iv寬度
* 中心點Y座標 = 真實Y + 0.5 * iv高度
    
    ScaleAnimation sa = new ScaleAnimation(0.1f, 4, 0.1f, 4, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
透明:
* 0為完全透明,1為完全不透明

        AlphaAnimation aa = new AlphaAnimation(0, 0.5f);

旋轉:
* 20表示動畫開始時的iv的角度
* 360表示動畫結束時iv的角度
* 預設旋轉的圓心在iv左上方
        RotateAnimation ra = new RotateAnimation(20, 360);
* 20,360的意義和上面一樣
* 指定圓心座標,相對於自己,值傳入0.5,那麼圓心的x座標:真實X + iv寬度 * 0.5
* 圓心的Y座標:真實Y + iv高度 * 0.5

        RotateAnimation ra = new RotateAnimation(20, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
#####所有動畫一起飛
    
        //建立動畫集合
        AnimationSet set = new AnimationSet(false);
        //往集合中添加動畫
        set.addAnimation(aa);
        set.addAnimation(sa);
        set.addAnimation(ra);
        iv.startAnimation(set);


屬性動畫
* 補間動畫,只是一個動畫效果,組件其實還在原來的位置上,xy沒有改變
###位移:
* 第一個參數target指定要顯示動畫的組件
* 第二個參數propertyName指定要改變組件的哪個屬性
* 第三個參數values是可變參數,就是賦予屬性的新的值
* 傳入0,代表x起始座標:當前x + 0
* 傳入100,代表x終點座標:當前x + 100
        //具有get、set方法的成員變數就稱為屬性
        ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 100) ;

###縮放:
* 第三個參數指定縮放的比例
* 0.1是從原本高度的十分之一開始
* 2是到原本高度的2倍結束
        ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "scaleY", 0.1f, 2);
###透明:
* 透明度,0是完全透明,1是完全不透明      
        ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "alpha", 0.1f, 1);
###旋轉
* rotation指定是順時針旋轉
* 20是起始角度
* 270是結束角度
        ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotation", 20, 270);
* 屬性指定為rotationX是豎直翻轉
* 屬性指定為rotationY是水平翻轉
        ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "rotationY", 20, 180);

###可變參數
* 第三個參數可變參數可以傳入多個參數,可以實現往回位移(旋轉、縮放、透明)
        ObjectAnimator oa = ObjectAnimator.ofFloat(bt, "translationX", 0, 70, 30, 100) ;

###所有動畫一起飛
        //建立動畫師集合
        AnimatorSet set = new AnimatorSet();
        //設定要播放動畫的組件
        set.setTarget(bt);
        //所有動畫有先後順序的播放
        //set.playSequentially(oa, oa2, oa3, oa4);
        //所有動畫一起播放
        set.playTogether(oa, oa2, oa3, oa4);
        set.start();

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.