安卓第十八天筆記--簡單動畫,安卓第十八天--

來源:互聯網
上載者:User

安卓第十八天筆記--簡單動畫,安卓第十八天--

title:動畫動畫1.補間動畫2.平移
fromXType  水平播放的類型fromXValue 從哪開始,0表示現在的位置toXType,   到什麼位置的類型toXValue 到什麼座標fromYType 垂直播放的類型fromYValue 從哪開始,0表示現在的位置toYType 到什麼位置的類型toYValue 到什麼座標
TranslateAnimation translate = new TranslateAnimation(            Animation.RELATIVE_TO_SELF, -2,             Animation.RELATIVE_TO_SELF, 2,             Animation.RELATIVE_TO_SELF, 0,            Animation.RELATIVE_TO_SELF, 1);    //設定顯示時間    translate.setDuration(3000);    //播放資源為無限迴圈    translate.setRepeatCount(Animation.INFINITE);    //播放模式,反轉播放,先順著播完,就反著播放    translate.setRepeatMode(Animation.REVERSE);    //哪個組件在播放,設定    imageView.setAnimation(translate);    //開始播放    translate.start();

 

3.旋轉
/*     * fromDegrees, 從什麼角度開始 0 從現在的 toDegrees, 270度 pivotXType, X中心點類型     * Animation.RELATIVE_TO_SELF自身 pivotXValue, 中心點值 0.5表示這個圖片的一半置     * pivotYType, Y中心點類型Animation.RELATIVE_TO_SELF自身     *  pivotYValue 0.5f      */    RotateAnimation rotate = new RotateAnimation(0, 360,            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,            3);    //播放顯示時間    rotate.setDuration(5000);    rotate.setRepeatCount(Animation.INFINITE);    rotate.setRepeatMode(Animation.RESTART);    imageView.setAnimation(rotate);    rotate.start();

 

4.縮放
    /*     * fromX, toX, 從座標什麼地址開始到什麼座標,0,表示當前 fromY, toY,      * pivotXType,  旋轉的中心點     * pivotXValue, 類型, pivotYType, pivotYValue     */    ScaleAnimation scale = new ScaleAnimation(1, 2, 1, -2,            Animation.RELATIVE_TO_SELF, -2, Animation.RELATIVE_TO_SELF, 3);    // 播放顯示時間    scale.setDuration(5000);    scale.setRepeatCount(Animation.INFINITE);    scale.setRepeatMode(Animation.RESTART);    imageView.setAnimation(scale);    scale.start();

 

5.透明度
    /*     * fromAlpha,  從什麼透明度開始     * toAlpha 到什麼透明度結束     */    AlphaAnimation alpha = new AlphaAnimation(1, 0);    // 播放顯示時間    alpha.setDuration(2000);    alpha.setRepeatCount(Animation.INFINITE);    alpha.setRepeatMode(Animation.REVERSE);            imageView.setAnimation(alpha);            alpha.start();}

 

6.集合
就是建立一個
AnimationSet set = new AnimationSet();set.addAnimation(scale);    set.addAnimation(rotate);    set.addAnimation(translate);    set.start();

 

就可以播放多個動畫
7. XML配置
在res/目錄下建立anim檔案目錄
 <!--         如果平移的數值是數字,那麼表示的是絕對值        如果是百分比,那麼表明是自己的寬高的倍數        如果是百分比加上 p,那麼表明是自己的父元素的寬高的倍數         --><?xml version="1.0" encoding="utf-8"?><set xmlns:android="http://schemas.android.com/apk/res/android" ><translate     android:fromXDelta="200%"    android:toXDelta="200%"    android:fromYDelta="-100%"    android:toYDelta="200%"    android:duration="4000"    android:repeatCount="infinite"    android:repeatMode="reverse"/>    <rotate       android:fromDegrees="0"       android:toDegrees="360"       android:toYScale="0.0"       android:pivotX="50%"       android:pivotY="50%"       android:duration="4000"        android:repeatCount="infinite"       android:repeatMode="reverse"/></set>

 

載入使用AnimationUtils.load方法載入

Animation animation = AnimationUtils.loadAnimation(this, R.anim.animation_set);    imageView.startAnimation(animation);

 

8.屬性動畫

一般都用代碼控制

/** * 水平 *  * @param v */public void translate(View v) {    /**     * 參數一: 誰去播放這個動畫 參數二: 屬性名稱字 想要改變x方向的移動 參數三:動畫執行的數值     */    ObjectAnimator objectAnimatrX = ObjectAnimator.ofFloat(imageView,            "translationX", -50, 50);    ObjectAnimator objectAnimatrY = ObjectAnimator.ofFloat(imageView,            "translationY", 0, 60);    objectAnimatrX.setDuration(2000);    objectAnimatrX.setRepeatCount(ObjectAnimator.INFINITE);    objectAnimatrX.setRepeatMode(ObjectAnimator.REVERSE);    objectAnimatrX.start();}/** * 轉換 *  * @param v */public void rotate(View v) {    ObjectAnimator rotateX = ObjectAnimator.ofFloat(imageView, "rotationX", 0,360);    ObjectAnimator rotateY = ObjectAnimator.ofFloat(imageView, "rotationY", 0,360);    rotateX.setDuration(3000);    rotateX.setRepeatMode(Animation.REVERSE);    //rotateX.setRepeatCount(Animation.INFINITE);    rotateY.setDuration(3000);    rotateY.setRepeatMode(Animation.REVERSE);    //rotateY.setRepeatCount(Animation.INFINITE);    AnimatorSet set  = new AnimatorSet();    set.playSequentially(rotateX,rotateY);    set.start();}/** * 綻放 *  * @param v */public void scale(View v) {    ObjectAnimator scaleX = ObjectAnimator.ofFloat(imageView, "scaleX", 2.0f,5f);    ObjectAnimator scaleY = ObjectAnimator.ofFloat(imageView, "scaleY", 2.0f,5f);    scaleX.setDuration(3000);    scaleX.setRepeatMode(Animation.REVERSE);    scaleX.setRepeatCount(Animation.INFINITE);    scaleY.setDuration(3000);    scaleY.setRepeatMode(Animation.REVERSE);    scaleY.setRepeatCount(Animation.INFINITE);    AnimatorSet set = new AnimatorSet();    set.playTogether(scaleX,scaleY);    set.start();}/** * 透明 *  * @param v */public void alpha(View v) {    ObjectAnimator alpha = ObjectAnimator.ofFloat(imageView, "alpha", 1.0f,0f);    alpha.setDuration(3000);    alpha.setRepeatMode(Animation.REVERSE);    alpha.setRepeatCount(Animation.INFINITE);    alpha.start();}/** * 集合 *  * @param v */public void set(View v) {    /**     * 參數一: 誰去播放這個動畫 參數二: 屬性名稱字 想要改變x方向的移動 參數三:動畫執行的數值     */    ObjectAnimator objectAnimatrX = ObjectAnimator.ofFloat(imageView,            "translationX", -50, 100);    ObjectAnimator objectAnimatrY = ObjectAnimator.ofFloat(imageView,            "translationY", -70, 100);    objectAnimatrX.setDuration(2000);    //objectAnimatrX.setRepeatCount(ObjectAnimator.INFINITE);    objectAnimatrX.setRepeatMode(ObjectAnimator.REVERSE);    objectAnimatrY.setDuration(2000);    //objectAnimatrY.setRepeatCount(ObjectAnimator.INFINITE);    objectAnimatrY.setRepeatMode(ObjectAnimator.REVERSE);    AnimatorSet set = new AnimatorSet();    //set.playTogether(objectAnimatrX,objectAnimatrY);    set.playSequentially(objectAnimatrX,objectAnimatrY);    set.start();}

 

如果需要 用XML控制可以在res\下建立 animator,即可, 在這個目錄建立XML檔案如果 下方法匯入

AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(myContext,R.anim.property_animator);set.setTarget(myObject);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.