Rebound動畫框架簡單介紹

如今主要是來分析下Rebound的源碼,看看裏面到底怎麼走的,用法很簡單(這是最簡單的用法),就三句話:java





SpringSystem springSystem = SpringSystem.create(); spring = springSystem.createSpring(); spring.addListener(new SpringListener() {}

最近在接手了一個老項目,發現裏面動畫框架用的是facebook中的Rebound框架,因爲之前沒據說過,放假時閒得蛋痛,看看了源碼,就順手寫這一篇吧。 
寫了一個小Demo,具體效果以下: 
這是圖片啊 android

代碼很簡單,這是xml佈局:spring

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/image" android:src="@drawable/a1" /> </RelativeLayout>

這是MainActivity:app

package com.micro.mytest_button; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MotionEvent; import android.view.View; import android.view.View.OnTouchListener; import android.widget.ImageView; import com.facebook.rebound.Spring; import com.facebook.rebound.SpringListener; import com.facebook.rebound.SpringSystem; import com.nineoldandroids.view.ViewHelper; public class MainActivity extends Activity { private ImageView image; private Spring spring; private final float mScale = 1.0f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.image); SpringSystem springSystem = SpringSystem.create(); spring = springSystem.createSpring(); spring.addListener(new SpringListener() { @Override public void onSpringUpdate(Spring spring) { float value = (float) spring.getCurrentValue(); float scale = 1f - (value * mScale); System.out.println("the value is " + value + "--the scale is --" + scale); ViewHelper.setScaleX(image, scale); ViewHelper.setScaleY(image, scale); } @Override public void onSpringEndStateChange(Spring spring) { } @Override public void onSpringAtRest(Spring spring) { } @Override public void onSpringActivate(Spring spring) { } }); image.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: spring.setEndValue(1.0f); break; case MotionEvent.ACTION_UP: case MotionEvent.ACTION_CANCEL: spring.setEndValue(0.0f); break; } return true; } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

 

如今主要是來分析下Rebound的源碼,看看裏面到底怎麼走的,用法很簡單(這是最簡單的用法),就三句話:框架

SpringSystem springSystem = SpringSystem.create(); spring = springSystem.createSpring(); spring.addListener(new SpringListener() {}

主要是建立了三個對象,SpringSystem/spring/SpringListener,對於動畫的效果,就是這三個玩意搞出來的。具體的模式以下: 
SpringSystem:繼承自BaseSpringSystem,其中包含了Spring對象引用的容器,控制Spring對象的操做,存在一個SpringLooper(是一個抽象方法,只有start()/stop()方法),這也是facebook自定義的類,與android.os.Looper無關,只是模擬了android.os.Looper的方法,內存start(),end()方法。其構造方法:ide

public static SpringSystem create() { return new SpringSystem(AndroidSpringLooperFactory.createSpringLooper()); }

 

打開SpringSystem(SpringLooper sl)源碼:函數

public BaseSpringSystem(SpringLooper springLooper) { if (springLooper == null) { throw new IllegalArgumentException("springLooper is required"); } mSpringLooper = springLooper; mSpringLooper.setSpringSystem(this); }

 

如今再看AndroidSpringLooperFactory.createSpringLooper()源碼:oop

public static SpringLooper createSpringLooper() { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) { return ChoreographerAndroidSpringLooper.create(); } else { return LegacyAndroidSpringLooper.create(); } }

 

能夠看到爲了兼容JDK,高低版本建立的SpringLooper容器不經相同,這個不是考慮的重點。佈局

如今來看第二句:動畫

spring = springSystem.createSpring();

 

翻開SpringSytem.createSpring()源碼:

public Spring createSpring() { Spring spring = new Spring(this); registerSpring(spring); return spring; }

 

能夠看到Spring類是一個對立的Java類,持有SpringSystem的引用,來看看Spring(SpringSystem ss)構造函數:

Spring(BaseSpringSystem springSystem) {
    if (springSystem == null) { throw new IllegalArgumentException("Spring cannot be created outside of a BaseSpringSystem"); } mSpringSystem = springSystem; mId = "spring:" + ID++; setSpringConfig(SpringConfig.defaultConfig); }

 

再看setSpringConfig(SpringConfig.defaultConfig)源碼:

public Spring setSpringConfig(SpringConfig springConfig) { if (springConfig == null) { throw new IllegalArgumentException("springConfig is required"); } mSpringConfig = springConfig; return this; }

 

也只是初始化一些參數,並無有咱們想要的源碼(這裏的意思就是我點擊圖片時,它爲何會有動畫的意思),再看第三個方法吧:

spring.addListener(new SpringListener() {}

 

添加監聽器,用的多的人就會有感受,這個也不會有關鍵代碼能夠使咱們圖片有縮放效果,那麼問題來了,看到facebook寫的三句話,咱們並無找到動畫縮放的想過函數啊,是否是有配置文件或者靜態/動態代碼塊呢??找了好久,沒有啊。那只有接下來再看image的代碼了,在看到Image的綁定的onTouch事件上,看到了這句話:

spring.setEndValue(1.0f);

啥也不說,進去看看再說吧:

相關文章
相關標籤/搜索