Android動畫學習筆記之補間動畫

本文實例為大家分享了Android補間動畫展示的具體代碼,供大家參考,具體內(nèi)容如下

創(chuàng)新互聯(lián)公司服務項目包括津市網(wǎng)站建設、津市網(wǎng)站制作、津市網(wǎng)頁制作以及津市網(wǎng)絡營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術優(yōu)勢、行業(yè)經(jīng)驗、深度合作伙伴關系等,向廣大中小型企業(yè)、政府機構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,津市網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟效益。目前,我們服務的客戶以成都為中心已經(jīng)輻射到津市省份的部分城市,未來相信會繼續(xù)擴大服務區(qū)域并繼續(xù)獲得客戶的支持與信任!

首先看看補間動畫的共同屬性:

Duration:動畫持續(xù)的時間(單位:毫秒)  
fillAfter:設置為true,動畫轉(zhuǎn)化在動畫被結(jié)束后被應用 
fillBefore:設置為true,動畫轉(zhuǎn)化在動畫開始前被應用 
interpolator:動畫插入器(加速、減速插入器) 
repeatCount:動畫重復的次數(shù) 
repeatMode:順序動畫(restart)/倒序動畫(reverse) 
startOffset:動畫之間時間間隔 

對于動畫的創(chuàng)建一般有兩種方式:

第一種是在res/新建一個anim文件夾,然后在其下面分別建立四種動畫 
第二種方式是通過java代碼的方式創(chuàng)建 

在補間動畫中我們通常有以下四種動畫:

位移動畫

創(chuàng)建方式一:

在anim文件下新建一個translate資源文件

<?xml version="1.0" encoding="utf-8"?>
    <translate xmlns:android="http://schemas.android.com/apk/res/android"

    android:fromXDelta="0" //設置從x的哪個點起,也可以設置為百分比,以控件的寬為基準
    android:toXDelta="100" //設置移動到目標x點
    android:fromYDelta="0" //設置從y的哪個點起,也可以設置為百分比,以控件的高為基準
    android:toYDelta="0" //設置移動到目標y點
    android:repeatCount="2" //動畫重復兩次,實際上你會發(fā)現(xiàn)是重復3次 
                          //(restart模式下執(zhí)行相同方向3次)
                //這里的意思是,在首次執(zhí)行完之后再重復2次
                //所以總的執(zhí)行次數(shù)為 n + 1次
                //如果是reverse模式下,則反序移動也算一次,所以在反序模式下
                //往同一方向只有兩次,加上反序的一次剛好就是3次

    android:repeatMode="restart" //沿當前方向順序移動一次,沒有反序移動,
                  //如果設置為reverse則有一次順序/反序移動的操作,
    android:duration="2000" //完成該動作需要2秒
    >
    </translate>

    //通過以下代碼注冊該動畫
     private void creatTranslateByInflate(){
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.translate);
      mCircle.startAnimation(animation);
    }

創(chuàng)建方式二:(代碼創(chuàng)建)

private void creatTranslateByCode() {
      TranslateAnimation animation = new TranslateAnimation(0, 100, 0, 0);
      animation.setDuration(2000);
      animation.setRepeatMode(Animation.REVERSE);
      animation.setRepeatCount(2);
      mCircle.startAnimation(animation);
    }

旋轉(zhuǎn)動畫

創(chuàng)建方式一:

<?xml version="1.0" encoding="utf-8"?>
    <rotate xmlns:android="http://schemas.android.com/apk/res/android"
      android:fromDegrees="0"http://起始的角度數(shù),
      android:toDegrees="180"http://終止的角度,順時針(起始度 < 終止的角度) 逆時針
                            //(起始度 > 終止的角度)
                  //這里就是順時針的旋轉(zhuǎn)180
      android:pivotX="50%" //旋轉(zhuǎn)中軸的x點,50%表示以控件寬為基準,在控件的中間x點
      android:pivotY="50%" //旋轉(zhuǎn)中軸的y點,50%表示以控件高為基準,在控件的中間y點
      android:duration="2000" 
      android:repeatMode="reverse"
      android:repeatCount="2"

    >
    </rotate>

    //通過以下代碼注冊該動畫
    private void createRotateByInflate(){
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
        mCircle.startAnimation(animation);
    }

創(chuàng)建方式二:(代碼創(chuàng)建)

private void createRotateByCode() {
      float pivotX = mCircle.getWidth() / 2.0f;
      float pivotY = mCircle.getHeight() / 2.0f;
      RotateAnimation animation = new RotateAnimation(0, 180, pivotX, pivotY);
      animation.setDuration(2000);
      animation.setRepeatMode(Animation.REVERSE);
      animation.setRepeatCount(2);
      mCircle.startAnimation(animation);
    }

縮放動畫

創(chuàng)建方式一:

<?xml version="1.0" encoding="utf-8"?>
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
      android:fromXScale="0.1"http://
      android:toXScale="1.0" //
      android:fromYScale="0.1"
      android:toYScale="1.0"
      android:pivotY="50%"
      android:pivotX="50%"
      android:duration="2000"
      android:repeatMode="reverse"
      android:repeatCount="2">
    </scale>

    //通過以下代碼注冊該動畫
    private void createScaleByInflate(){
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.scale);
      mCircle.startAnimation(animation);
    }

創(chuàng)建方式二:(代碼創(chuàng)建)

private void createScaleByCode() {
      //創(chuàng)建動畫Animation.RELATIVE_TO_PARENT 以父容器為參照物 
      //Animation.RELATIVE_TO_SELF 以自己為參照物, 如果以父容器為參照物會導致控件移動
      float pivotX = mCircle.getWidth() / 2.0f;
      float pivotY = mCircle.getHeight() / 2.0f;
      ScaleAnimation animation = new ScaleAnimation(0.1f, 1.0f, 0.1f, 1.0f, pivotX,pivotY);
      animation.setDuration(2000);
      animation.setRepeatMode(Animation.REVERSE);
      animation.setRepeatCount(2);
      mCircle.startAnimation(animation);
    }

漸變動畫

創(chuàng)建方式一:

<?xml version="1.0" encoding="utf-8"?>
    <alpha xmlns:android="http://schemas.android.com/apk/res/android"
      android:fromAlpha="0.1"
      android:toAlpha="1.0"
      android:repeatMode="reverse"
      android:repeatCount="2"
      android:duration="2000">
    </alpha>

    //通過以下代碼注冊該動畫
    private void createAlphaByInflate(){
      Animation animation = AnimationUtils.loadAnimation(this, R.anim.alpha);
      mCircle.startAnimation(animation);
    }

創(chuàng)建方式二:(代碼創(chuàng)建)

private void createAlphaByCode() {
      AlphaAnimation animation = new AlphaAnimation(0.1f, 1.0f);
      animation.setDuration(2000);
      animation.setRepeatMode(Animation.REVERSE);
      animation.setRepeatCount(2);
      mCircle.startAnimation(animation);
    }

以上的四種可以單獨使用也可以結(jié)合起來使用,如果要結(jié)合起來使用的話,直接在anim文件夾下創(chuàng)建set集合,然后將需要結(jié)合的動畫

放在一起即可

如下示例:

<?xml version="1.0" encoding="UTF-8"?>

    <set xmlns:android="http://schemas.android.com/apk/res/android">

      <rotate android:toDegrees="3600" android:pivotY="50%" 
      android:pivotX="50%" android:fromDegrees="0"
      android:duration="2000"/>

      <translate android:duration="3000" android:toYDelta="100%" 
      android:toXDelta="150%"  
      android:fromYDelta="-150%" android:fromXDelta="-100%"/>

      <alpha android:duration="5000" android:toAlpha="1.0" 
      android:repeatMode="reverse" 
      android:repeatCount="3" android:fromAlpha="0.5"/>

 </set>

基本的創(chuàng)建方式,以及基本屬性都在這里了,至于如何實現(xiàn)一個具有美感的效果圖,那就看個人的設計感了。

最后看看運行結(jié)果圖

Android動畫學習筆記之補間動畫

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)頁標題:Android動畫學習筆記之補間動畫
分享地址:http://bm7419.com/article42/pcgjhc.html

成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信公眾號、響應式網(wǎng)站、動態(tài)網(wǎng)站靜態(tài)網(wǎng)站、企業(yè)網(wǎng)站制作、ChatGPT

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都網(wǎng)站建設