眾所周知,TranslateAnimation是android中重要的一個動畫函數,很多時候我們
都需要使用它來實現更好的UI效果,今天就簡單研究下這個TranslateAnimation。
TranslateAnimation這個位移動畫主要有三個構造函數,對應著三種不同的參數
形式,接下來使用源碼簡單分析,個人見解而已。
(1)第一種構造函數TranslateAnimation(Context context, AttributeSet attrs)
通過源碼我們可以看出,這個構造函數,主要是當你從一個資源文件中獲取一個動畫資源時
進行調用,構造函數會自動幫你解析其中相應的參數,然后賦值給mFromXType,mFromXValue
...等8個參數。其實它就相當于接下來將要介紹的第三個構造函數,只不過這個構造函數
傳入的是context和資源文件對你的動畫屬性的封裝參數attrs。而第二個構造也是第三個
構造函數的一種特殊情況。
(2)第二種構造函數TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
這個構造函數的參數
float fromXDelta:這個參數表示動畫開始的點離當前View X坐標上的差值;
float toXDelta, 這個參數表示動畫結束的點離當前View X坐標上的差值;
float fromYDelta, 這個參數表示動畫開始的點離當前View Y坐標上的差值;
float toYDelta)這個參數表示動畫開始的點離當前View Y坐標上的差值;
也就是說你的TranslateAnimation動畫執行的整個過程,需要你指定它的起始位置,而這個起始位置的確定要
依靠你原來的View的位置為參照,如果你View的位置為(x,y),則動畫開始的位置坐標為(x+fromXDelta, y+fromYDelta)
它的結束位置為(x+toXDelta,y+toYDelta)。再看源碼你會發現構造函數又設置了四個參數為默認的ABSOLUTE,這個參數
表示絕對像素,在接下里的最后一個構造函數中可以由你來自己選定,而這個函數也其實就是下一個構造函數的一種
X方向或者Y方向上的Type選定為Animation.ABSOLUTE的特殊情況。
(3)第三種構造函數TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)?
在這個構造函數中共有8個參數
Animation.ABSOLUTE是絕對位移量,也就是上面的第二種構造函數的情況,當使用這個參數時,這兩種構造函數并無差別
Animation.RELATIVE_TO_SELF
Animation.RELATIVE_TO_PARENT這兩個參數是相對位移量,也就是說相對于這個View自己,或者是相對于這個控件的父控件
的。當使用這兩個Type時,相應的fromXValue,toXValue,fromYValue,toYValue的值是double型的數據,可正也可負。
例如當Type為RELATIVE_TO_SELF,且fromXValue為+1.0時,也就代表著你的動畫的初始位置x坐標在原來視圖X坐標位置的正方向偏移一個自身寬度。而當值為0時
就意味著你這個View的x軸位置相對于你自身原來X軸位置不變。
都需要使用它來實現更好的UI效果,今天就簡單研究下這個TranslateAnimation。
TranslateAnimation這個位移動畫主要有三個構造函數,對應著三種不同的參數
形式,接下來使用源碼簡單分析,個人見解而已。
(1)第一種構造函數TranslateAnimation(Context context, AttributeSet attrs)
/*** Constructor used when a TranslateAnimation is loaded from a resource.* * @param context Application context to use* @param attrs Attribute set from which to read values*/public TranslateAnimation(Context context, AttributeSet attrs) {super(context, attrs);TypedArray a = context.obtainStyledAttributes(attrs,com.android.internal.R.styleable.TranslateAnimation);Description d = Description.parseValue(a.peekValue(com.android.internal.R.styleable.TranslateAnimation_fromXDelta));mFromXType = d.type;mFromXValue = d.value;d = Description.parseValue(a.peekValue(com.android.internal.R.styleable.TranslateAnimation_toXDelta));mToXType = d.type;mToXValue = d.value;d = Description.parseValue(a.peekValue(com.android.internal.R.styleable.TranslateAnimation_fromYDelta));mFromYType = d.type;mFromYValue = d.value;d = Description.parseValue(a.peekValue(com.android.internal.R.styleable.TranslateAnimation_toYDelta));mToYType = d.type;mToYValue = d.value;a.recycle();}
通過源碼我們可以看出,這個構造函數,主要是當你從一個資源文件中獲取一個動畫資源時
進行調用,構造函數會自動幫你解析其中相應的參數,然后賦值給mFromXType,mFromXValue
...等8個參數。其實它就相當于接下來將要介紹的第三個構造函數,只不過這個構造函數
傳入的是context和資源文件對你的動畫屬性的封裝參數attrs。而第二個構造也是第三個
構造函數的一種特殊情況。
(2)第二種構造函數TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
/*** Constructor to use when building a TranslateAnimation from code* * @param fromXDelta Change in X coordinate to apply at the start of the* animation* @param toXDelta Change in X coordinate to apply at the end of the* animation* @param fromYDelta Change in Y coordinate to apply at the start of the* animation* @param toYDelta Change in Y coordinate to apply at the end of the* animation*/public TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta) {mFromXValue = fromXDelta;mToXValue = toXDelta;mFromYValue = fromYDelta;mToYValue = toYDelta;mFromXType = ABSOLUTE;mToXType = ABSOLUTE;mFromYType = ABSOLUTE;mToYType = ABSOLUTE;}
這個構造函數的參數
float fromXDelta:這個參數表示動畫開始的點離當前View X坐標上的差值;
float toXDelta, 這個參數表示動畫結束的點離當前View X坐標上的差值;
float fromYDelta, 這個參數表示動畫開始的點離當前View Y坐標上的差值;
float toYDelta)這個參數表示動畫開始的點離當前View Y坐標上的差值;
也就是說你的TranslateAnimation動畫執行的整個過程,需要你指定它的起始位置,而這個起始位置的確定要
依靠你原來的View的位置為參照,如果你View的位置為(x,y),則動畫開始的位置坐標為(x+fromXDelta, y+fromYDelta)
它的結束位置為(x+toXDelta,y+toYDelta)。再看源碼你會發現構造函數又設置了四個參數為默認的ABSOLUTE,這個參數
表示絕對像素,在接下里的最后一個構造函數中可以由你來自己選定,而這個函數也其實就是下一個構造函數的一種
X方向或者Y方向上的Type選定為Animation.ABSOLUTE的特殊情況。
(3)第三種構造函數TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue)?
/*** Constructor to use when building a TranslateAnimation from code* * @param fromXType Specifies how fromXValue should be interpreted. One of* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or* Animation.RELATIVE_TO_PARENT.* @param fromXValue Change in X coordinate to apply at the start of the* animation. This value can either be an absolute number if fromXType* is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.* @param toXType Specifies how toXValue should be interpreted. One of* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or* Animation.RELATIVE_TO_PARENT.* @param toXValue Change in X coordinate to apply at the end of the* animation. This value can either be an absolute number if toXType* is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.* @param fromYType Specifies how fromYValue should be interpreted. One of* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or* Animation.RELATIVE_TO_PARENT.* @param fromYValue Change in Y coordinate to apply at the start of the* animation. This value can either be an absolute number if fromYType* is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.* @param toYType Specifies how toYValue should be interpreted. One of* Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or* Animation.RELATIVE_TO_PARENT.* @param toYValue Change in Y coordinate to apply at the end of the* animation. This value can either be an absolute number if toYType* is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.*/public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,int fromYType, float fromYValue, int toYType, float toYValue) {mFromXValue = fromXValue;mToXValue = toXValue;mFromYValue = fromYValue;mToYValue = toYValue;mFromXType = fromXType;mToXType = toXType;mFromYType = fromYType;mToYType = toYType;}
在這個構造函數中共有8個參數
Animation.ABSOLUTE是絕對位移量,也就是上面的第二種構造函數的情況,當使用這個參數時,這兩種構造函數并無差別
Animation.RELATIVE_TO_SELF
Animation.RELATIVE_TO_PARENT這兩個參數是相對位移量,也就是說相對于這個View自己,或者是相對于這個控件的父控件
的。當使用這兩個Type時,相應的fromXValue,toXValue,fromYValue,toYValue的值是double型的數據,可正也可負。
例如當Type為RELATIVE_TO_SELF,且fromXValue為+1.0時,也就代表著你的動畫的初始位置x坐標在原來視圖X坐標位置的正方向偏移一個自身寬度。而當值為0時
就意味著你這個View的x軸位置相對于你自身原來X軸位置不變。