Facebook rebond 彈簧功能源碼分析

相信大家在項目上或多或少的用到了彈簧功能,比較常用和方便的就是facebook的開源庫rebond,想要深入的了解這個開源庫的使用方法就需要我們源碼分析(read the fucking source code?。┱麄€開源庫的功能實現(xiàn)。我自己也通過rebond寫了個demo 有興趣的可以下載了解下(代碼都有注釋 適合菜鳥初學(xué)者了解 大神略過)

廢話不多說,下面我們就開始分析源代碼(very fucking detailed?。?/p>

(一)rebond的配置

在gradle 進行配置:

dependencies {
compile 'com.facebook.rebound:rebound:0.3.8'
}
 

或者maven部署rebond庫

<dependency>
  <groupId>com.facebook.rebound</groupId>
  <artifactId>rebound</artifactId>
  <version>0.3.8</version>
</dependency>

這時候我們可以看到rebond的目錄結(jié)構(gòu):

|-- ui
| |-- utils (layout的工具類 提供create四個不同參數(shù)的layout布局 例如:寬高自適應(yīng)父類布局等)
| |-- SpringConfiguratorView(彈簧(Spring) view 配置器Spring 配置信息 包含Spring張力值0-200 摩擦力0-50)
|-- AndroidSpringLooperFactory

|-- AnimationQueue
|-- BaseSpringSystem
|-- BuildConfig (包含了開源庫rebond的版本信息等)

|-- ChoreographerCompat

|-- OrigamiValueConverter

|-- SimpleSpringListener (實現(xiàn)了SpringLsitener接口)

|-- Spring

|-- SpringChain

|-- SpringConfig

|-- SpringConfigRegistry

|-- SpringListener (包含Spring 四個運動狀態(tài)的方法)

|-- SpringLooper

|-- SpringSystem

|-- SpringSystemListener

|-- SpringUtil

|-- SteppingLooper

|-- SynchronousLooper

整體的結(jié)構(gòu)就是這樣的,下面我們來詳細(xì)的介紹下每個類的作用和功能 以及rebond是怎么運作的

(二) rebond的每個類的作用

  • ui文件夾下utils:

這個不用多說 此類提供創(chuàng)建四種視圖布局分別為

createMatchParams();
createWrapParams();
createWrapMatchParams();
createMatchWrapParams();
這個類的作用也就是demo中給定的動畫展示所用到的視圖適配。

  • ui文件夾下SpringConfiguratorView(可省略)

    這個我仔細(xì)看了下 發(fā)現(xiàn)可能是原demo有個手動滑動設(shè)置張力摩擦力的seekbar 這里不需要用到 所以忽略掉

    然而重要的是我們要清楚在Spring中,彈簧的運動軌跡遵循胡克定律 要我們的彈簧能夠彈起來需要我們手動設(shè)置張力(TENSION)和摩擦力(FRICTION)

  • SpringConfig

這里是設(shè)置彈簧張力和拉力的類,類中聲明Spring的張力和摩擦力,在這個類中我們可以通過調(diào)用

fromOrigamiTensionAndFriction(Tension,Friction)

方法來設(shè)置我們彈簧的張力和拉力,而這會return一個參數(shù)經(jīng)過OrigamiValueConverter轉(zhuǎn)換的SpringConfig對象,假如你想用默認(rèn)的參數(shù)也可以通過直接調(diào)用暴露給的靜態(tài)方法
SpringConfig.defaultConfig,返回的是<code>fromOrigamiTensionAndFriction(40.0D, 7.0D)</code> 既默認(rèn)40 70

  • SpringConfigRegistry

放置大批量SpringConfig的類庫。在SpringConfigRegistry中 聲明了一個SpringConfig的map集合,主要的作用就是add和remove SpringConfig 在下文SpringChain會提到

  • OrigamiValueConverter

在SpringConfig中提到,設(shè)置張力和摩擦力經(jīng)過此類進行一個轉(zhuǎn)換,
這里代碼如下:

public OrigamiValueConverter() {
}

public static double tensionFromOrigamiValue(double oValue) {
    return oValue == 0.0D?0.0D:(oValue - 30.0D) * 3.62D + 194.0D;
}

public static double origamiValueFromTension(double tension) {
    return tension == 0.0D?0.0D:(tension - 194.0D) / 3.62D + 30.0D;
}

public static double frictionFromOrigamiValue(double oValue) {
    return oValue == 0.0D?0.0D:(oValue - 8.0D) * 3.0D + 25.0D;
}

public static double origamiValueFromFriction(double friction) {
    return friction
  • SpringSystem

SpringSystem 繼承于BaseSpringSystem 內(nèi)部隱藏了它的構(gòu)造方法,我的需要使用它的靜態(tài)方法create() ,這個方法自動為我們創(chuàng)建了一個SpringLooper,我們來看看這個方法返回的數(shù)據(jù)<code> return new SpringSystem(AndroidSpringLooperFactory.createSpringLooper());</code>
這也是我們在使用彈簧功能前提,我們需要調(diào)用這個方法得到一個彈簧,之后再對這個彈簧進行基本的設(shè)置

  • AndroidSpringLooperFactory

在SpringSystem 我們提到,使用rebond時,自動為我們creat一個SpringLooper,此create方法根據(jù)API是否>16(4.0) 為界限,自動返回不同的AndroidSpringLooper實例,不同的類又共同繼承SpringLooper,也就是說 ,在這個類中,主要的作用就是根據(jù)api不同創(chuàng)建不同的AndroidSpringLooper對不同版本進行適配,AndroidSpringLooper 也就是looper,調(diào)用的是BaseSpringSystem的 對looper進行的迭代計算器,做的就是不斷的更新SpringSystem的狀態(tài)。

  • SpringLooper

上面說到以4.0為界限分別返回繼承SpringLooper的不同的實例,在SpringLooper這個抽象類中,有兩個抽象方法start()和stop(),子類根據(jù)自身代碼來操作Looper的開始和結(jié)束需要做的事情,其實主要是調(diào)用了BaseSpringSystem的loop方法。

  • BaseSpringSystem

在BaseSpringSystem中維護了一個SpringSystemListener數(shù)組,可以進行addListener或者removelistner的操作, 并且此類提供了對Spring的注冊及初始化,對彈簧運動進行迭代計算,以及l(fā)oop的迭代計算,可以說 這個類是維護彈簧持續(xù)運動計算的一個類 ,概括來說 這個類為我們創(chuàng)建了一個彈簧該有的東西,彈簧的運動監(jiān)聽,彈簧的物理運動, 主要代碼如下:

   //loop的迭代計算
   public void loop(double ellapsedMillis) {
       Iterator i$ = this.mListeners.iterator();

       SpringSystemListener listener;
       while(i$.hasNext()) {
           listener = (SpringSystemListener)i$.next();
           listener.onBeforeIntegrate(this);
       }

       this.advance(ellapsedMillis);
       if(this.mActiveSprings.isEmpty()) {
           this.mIdle = true;
       }

       i$ = this.mListeners.iterator();

       while(i$.hasNext()) {
           listener = (SpringSystemListener)i$.next();
           listener.onAfterIntegrate(this);
       }

       if(this.mIdle) {
           this.mSpringLooper.stop();
       }
       
       // Spring 物理運動計算
 void advance(double deltaTime) {
       Iterator i$ = this.mActiveSprings.iterator();
       while(i$.hasNext()) {
           Spring spring = (Spring)i$.next();
           if(spring.systemShouldAdvance()) {
               spring.advance(deltaTime / 1000.0D);
           } else {
               this.mActiveSprings.remove(spring);
           }

  • Spring

當(dāng)當(dāng)當(dāng)當(dāng)~ 這就是我們的彈簧啦,在這個類中詳細(xì)的計算彈簧運動的物理計算 :代碼有點多。我們可以詳細(xì)的了解下彈簧的運動過程:

void advance(double realDeltaTime) {
       boolean isAtRest = this.isAtRest();
       if(!isAtRest || !this.mWasAtRest) {
           double adjustedDeltaTime = realDeltaTime;
           if(realDeltaTime > 0.064D) {
               adjustedDeltaTime = 0.064D;
           }

           this.mTimeAccumulator += adjustedDeltaTime;
           double tension = this.mSpringConfig.tension;
           double friction = this.mSpringConfig.friction;
           double position = this.mCurrentState.position;
           double velocity = this.mCurrentState.velocity;
           double tempPosition = this.mTempState.position;

           double dvdt;
           double tempVelocity;
           for(tempVelocity = this.mTempState.velocity; this.mTimeAccumulator >= 0.001D; velocity += dvdt * 0.001D) {
               this.mTimeAccumulator -= 0.001D;
               if(this.mTimeAccumulator < 0.001D) {
                   this.mPreviousState.position = position;
                   this.mPreviousState.velocity = velocity;
               }

               double aAcceleration = tension * (this.mEndValue - tempPosition) - friction * velocity;
               tempPosition = position + velocity * 0.001D * 0.5D;
               tempVelocity = velocity + aAcceleration * 0.001D * 0.5D;
               double bVelocity = tempVelocity;
               double bAcceleration = tension * (this.mEndValue - tempPosition) - friction * tempVelocity;
               tempPosition = position + tempVelocity * 0.001D * 0.5D;
               tempVelocity = velocity + bAcceleration * 0.001D * 0.5D;
               double cVelocity = tempVelocity;
               double cAcceleration = tension * (this.mEndValue - tempPosition) - friction * tempVelocity;
               tempPosition = position + tempVelocity * 0.001D;
               tempVelocity = velocity + cAcceleration * 0.001D;
               double dAcceleration = tension * (this.mEndValue - tempPosition) - friction * tempVelocity;
               double dxdt = 0.16666666666666666D * (velocity + 2.0D * (bVelocity + cVelocity) + tempVelocity);
               dvdt = 0.16666666666666666D * (aAcceleration + 2.0D * (bAcceleration + cAcceleration) + dAcceleration);
               position += dxdt * 0.001D;
           }

           this.mTempState.position = tempPosition;
           this.mTempState.velocity = tempVelocity;
           this.mCurrentState.position = position;
           this.mCurrentState.velocity = velocity;
           if(this.mTimeAccumulator > 0.0D) {
               this.interpolate(this.mTimeAccumulator / 0.001D);
           }

           if(this.isAtRest() || this.mOvershootClampingEnabled && this.isOvershooting()) {
               if(tension > 0.0D) {
                   this.mStartValue = this.mEndValue;
                   this.mCurrentState.position = this.mEndValue;
               } else {
                   this.mEndValue = this.mCurrentState.position;
                   this.mStartValue = this.mEndValue;
               }

               this.setVelocity(0.0D);
               isAtRest = true;
           }

           boolean notifyActivate = false;
           if(this.mWasAtRest) {
               this.mWasAtRest = false;
               notifyActivate = true;
           }

           boolean notifyAtRest = false;
           if(isAtRest) {
               this.mWasAtRest = true;
               notifyAtRest = true;
           }

           Iterator i$ = this.mListeners.iterator();

           while(i$.hasNext()) {
               SpringListener listener = (SpringListener)i$.next();
               if(notifyActivate) {
                   listener.onSpringActivate(this);
               }

               listener.onSpringUpdate(this);
               if(notifyAtRest) {
                   listener.onSpringAtRest(this);
               }
           }

       }
   }

同時,創(chuàng)建一個Spring需要調(diào)用SpringSystem的createSpring( )方法。
這里面詳細(xì)的定義了彈簧運動的各種東西,比如詳細(xì)的記錄彈簧運動到某個階段的值(彈簧運動的物理狀態(tài)), 運動到某個階段的彈簧的長度等等 。

  • ChoreographerCompat(可省略)

貌似是舞蹈者 舞蹈者就是控制圖形動畫和ui的類 詳細(xì)可以看這篇文章,這里詳細(xì)的介紹android舞蹈者的作用
這個類根據(jù)Api是否》=16 (4.0) 控制不同api延遲或者立即 post和remove Choreographer的Callback 這里的運用貌似是在AnimationQuee中用到,但是AnimationQuee 在實際的代碼中也并未用到,所以這里可以省略不談 看別人說好像用AnimationQuee應(yīng)該是有什么坑,我覺得應(yīng)該是適配的坑,,。AnimationQuee的介紹也省略

  • SpringChain

SpringChain 顧名思義,Spring連鎖(也就是多個Spring的連鎖)。如果你想多個view設(shè)置彈簧功能的需求,就可以用到SpringChain,SpringChain會從第一個圖片開始一個一個得帶動下一個圖片的運動(如果是單個的話用Spring就可以),在這個類里,給我們提供了一個oncreat()的靜態(tài)方法供我們使用,參數(shù)依次為主拉力,主摩擦力,輔助拉力,輔助摩擦力,之后我們給每個view通過springChain.addSpring添加到隊列中,并且設(shè)置SpringListener,最后通過springChain.setControlSpringIndex(0).getControlSpring().setEndValue(0);設(shè)置剛開始的彈簧的index 比如一個4個view 第一個先動的是4 那么最后一個就是0 讓我們來看看具體的代碼:


/**
  * 將一個彈簧添加到將返回給所提供偵聽器的鏈中。
  * @param  監(jiān)聽SpringChain中的Spring 并且通知更新它
  * @return this SpringChain for chaining(返回SpringChain的鏈接)
  */
 public SpringChain addSpring(final SpringListener listener) {
   // We listen to each spring added to the SpringChain and dynamically chain the springs together
   // whenever the control spring state is modified.
   Spring spring = mSpringSystem
       .createSpring()
       .addListener(this)
       .setSpringConfig(mAttachmentSpringConfig);
   mSprings.add(spring);
   mListeners.add(listener);
   return this;
 }

 /**
/ /設(shè)置控制彈簧的索引。此彈簧將帶動所有彈簧的位置進行運動
  * Set the index of the control spring. This spring will drive the positions of all the springs
  * before and after it in the list when moved.
  * @param i the index to use for the control spring(指針i 用于控制彈簧)
  * @return this SpringChain
  */
 public SpringChain setControlSpringIndex(int i) {
   mControlSpringIndex = i;
   Spring controlSpring = mSprings.get(mControlSpringIndex);
   if (controlSpring == null) {
     return null;
   }
   for (Spring spring : mSpringSystem.getAllSprings()) {
     spring.setSpringConfig(mAttachmentSpringConfig);
   }
   getControlSpring().setSpringConfig(mMainSpringConfig);
   return this;
 }
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,511評論 19 139
  • Spring Boot 參考指南 介紹 轉(zhuǎn)載自:https://www.gitbook.com/book/qbgb...
    毛宇鵬閱讀 47,256評論 6 342
  • 第108個大叔閱讀 180評論 0 0
  • 文/07 翱翔雄鷹任憑空 羨得懦人嘆無究 余生偷得荒寂處 誤了年少壯志雄 17.12.10.中午閑作
    723edf844d12閱讀 200評論 5 23
  • 今天看到支教同事發(fā)在朋友圈的照片,忽然有點懷念在泰國的日子,那段近似閉關(guān)修行的難忘經(jīng)歷。 離開坦亞布里皇家理工大學(xué)...
    嫘妮閱讀 425評論 4 3

友情鏈接更多精彩內(nèi)容