Facebook rebond 彈簧功能源碼分析

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

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

(一)rebond的配置

在gradle 進(jìn)行配置:

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

或者maven部署rebond庫(kù)

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

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

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

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

|-- ChoreographerCompat

|-- OrigamiValueConverter

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

|-- Spring

|-- SpringChain

|-- SpringConfig

|-- SpringConfigRegistry

|-- SpringListener (包含Spring 四個(gè)運(yùn)動(dòng)狀態(tài)的方法)

|-- SpringLooper

|-- SpringSystem

|-- SpringSystemListener

|-- SpringUtil

|-- SteppingLooper

|-- SynchronousLooper

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

(二) rebond的每個(gè)類的作用

  • ui文件夾下utils:

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

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

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

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

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

  • SpringConfig

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

fromOrigamiTensionAndFriction(Tension,Friction)

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

  • SpringConfigRegistry

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

  • OrigamiValueConverter

在SpringConfig中提到,設(shè)置張力和摩擦力經(jīng)過(guò)此類進(jìn)行一個(gè)轉(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() ,這個(gè)方法自動(dòng)為我們創(chuàng)建了一個(gè)SpringLooper,我們來(lái)看看這個(gè)方法返回的數(shù)據(jù)<code> return new SpringSystem(AndroidSpringLooperFactory.createSpringLooper());</code>
這也是我們?cè)谑褂脧椈晒δ芮疤幔覀冃枰{(diào)用這個(gè)方法得到一個(gè)彈簧,之后再對(duì)這個(gè)彈簧進(jìn)行基本的設(shè)置

  • AndroidSpringLooperFactory

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

  • SpringLooper

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

  • BaseSpringSystem

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

   //loop的迭代計(jì)算
   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 物理運(yùn)動(dòng)計(jì)算
 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)~ 這就是我們的彈簧啦,在這個(gè)類中詳細(xì)的計(jì)算彈簧運(yùn)動(dòng)的物理計(jì)算 :代碼有點(diǎn)多。我們可以詳細(xì)的了解下彈簧的運(yùn)動(dòng)過(guò)程:

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);
               }
           }

       }
   }

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

  • ChoreographerCompat(可省略)

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

  • SpringChain

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


/**
  * 將一個(gè)彈簧添加到將返回給所提供偵聽(tīng)器的鏈中。
  * @param  監(jiān)聽(tīng)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è)置控制彈簧的索引。此彈簧將帶動(dòng)所有彈簧的位置進(jìn)行運(yùn)動(dòng)
  * 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)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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

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