日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > 屬性動畫資料文件如何編寫?property-animation資源文件 屬性動畫如何自定義TypeEvaluator

屬性動畫資料文件如何編寫?property-animation資源文件 屬性動畫如何自定義TypeEvaluator

來源:程序員人生   發布時間:2017-02-06 08:33:02 閱讀次數:2915次

android動畫分為3種:AnimationDrawable(類)逐幀動畫;Tween補間動畫;property animation屬性動畫;

第1種的類名:AnimationDrawable,在資料文件部份,這類動畫也屬于Drawable的1種,是Drawable的子類;

第2種的類名:Animation,Animation是個抽象類,android提供了幾個具體的實現類如TranslateAnimation,RotateAnimation,AlphaAnimation,ScaleAnimation;

第3種的類名:Animator,Animator也是個抽象類,android提供了ValueAnimator和ObjectAnimator和Animatorset作為具體實現。其中ObjectAnimator繼承了ValueAnimator,前者1般情況下使用起來可以更方便,但有些特殊情況必須使用ValueAnimator.   而Animatorset可以對多個Animator對象包裹。


在這里主要就Animator進行1些記錄:

定義屬性動畫資源文件的格式舉例:


實例-實現不斷漸變的背風景


res文件夾下建立animator文件夾,在該文件夾下建立

color_anim.xml

<?xml version="1.0" encoding="utf⑻"?>
<objectAnimator xmlns:Android="http://schemas.android.com/apk/res/android" 
    android:propertyName="backgroundColor"
    android:duration="3000"
    android:valueFrom="#FF8080"
    android:valueTo="#8080FF"
    android:repeatCount="infinite"
    android:repeatMode="reverse"
    android:valueType="intType">
    
</objectAnimator>

其他文件

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/my_linear"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" 
    android:orientation="vertical">

</LinearLayout>

MainActivity.Java

package com.example.propertyanimation;

import android.os.Bundle;
import android.animation.AnimatorInflater;
import android.animation.ArgbEvaluator;
import android.animation.ObjectAnimator;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout Container=(LinearLayout) super.findViewById(R.id.my_linear);
container.addView(new MyAnimationView(this));
}

public class MyAnimationView extends View{

@SuppressLint("NewApi")
public MyAnimationView(Context context) {
super(context);
//加載動畫資源
ObjectAnimator colorAnim=(ObjectAnimator) AnimatorInflater.loadAnimator(MainActivity.this, R.animator.color_anim);
/**
* 如果想要的動畫類型是Android系統所未知的,那末通過實現TypeEvaluator接口就可以夠創建自己的評價器。
* Android系統已知的類型是int、float或色彩(color),分別有IntEvaluator、FloatEvaluator
* 和ArgbEvaluator類型的評價器所支持。
*/
colorAnim.setEvaluator(new ArgbEvaluator());
//對該View本身利用屬性動畫
colorAnim.setTarget(this);
//開始指定動畫
colorAnim.start();
}
}
}



屬性動畫使用時有時會用到自定義的TypeEvaluator,自定義舉例:

如果想根據某個屬性TYPE來實現動畫,但是這個Type又不是Android系統內置的,這個時候就需要創建1個自己的evaluator來實現了,并且新創建的type必須實現接口TypeEvaluator。Android系統內置的type有int,float和color,他們對應的evaluator是IntEvaluator、FloatEvaluator和ArgbEvaluator。接口TypeEvaluator內只有1個方法,用來計算要實現動畫屬性的值。    

[java] view plain copy
  1. /** 
  2.  * Interface for use with the {@link ValueAnimator#setEvaluator(TypeEvaluator)} function. Evaluators 
  3.  * allow developers to create animations on arbitrary property types, by allowing them to supply 
  4.  * custom evaluators for types that are not automatically understood and used by the animation 
  5.  * system. 
  6.  * 
  7.  * @see ValueAnimator#setEvaluator(TypeEvaluator) 
  8.  */  
  9. public interface TypeEvaluator<T> {  
  10. /** 
  11.      * This function returns the result of linearly interpolating the start and end values, with 
  12.      * <code>fraction</code> representing the proportion between the start and end values. The 
  13.      * calculation is a simple parametric calculation: <code>result = x0 + t * (x1 - x0)</code>, 
  14.      * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>, 
  15.      * and <code>t</code> is <code>fraction</code>. 
  16.      * 
  17.      * @param fraction   The fraction from the starting to the ending values 
  18.      * @param startValue The start value. 
  19.      * @param endValue   The end value. 
  20.      * @return A linear interpolation between the start and end values, given the 
  21.      *         <code>fraction</code> parameter. 
  22.      */  
  23. public T evaluate(float fraction, T startValue, T endValue);  
  24.   
  25. }  
先看看Android系統內置FloatEvaluator是怎樣弄的:
[java] view plain copy
  1. public class FloatEvaluator implements TypeEvaluator<Number> {  
  2. /** 
  3.      * This function returns the result of linearly interpolating the start and end values, with 
  4.      * <code>fraction</code> representing the proportion between the start and end values. The 
  5.      * calculation is a simple parametric calculation: <code>result = x0 + t * (v1 - v0)</code>, 
  6.      * where <code>x0</code> is <code>startValue</code>, <code>x1</code> is <code>endValue</code>, 
  7.      * and <code>t</code> is <code>fraction</code>. 
  8.      * 
  9.      * @param fraction   The fraction from the starting to the ending values 
  10.      * @param startValue The start value; should be of type <code>float</code> or 
  11.      *                   <code>Float</code>  
  12. * @param endValue   The end value; should be of type <code>float</code> or <code>Float</code> 
  13. * @return A linear interpolation between the start and end values, given the 
  14.      *         <code>fraction</code> parameter. 
  15.      */  
  16. public Float evaluate(float fraction, Number startValue, Number endValue) {  
  17. float startFloat = startValue.floatValue();  
  18.         return startFloat + fraction * (endValue.floatValue() - startFloat);  
  19. }  
  20. }  
還是舉個例子來測試1下,先看下面圖中的效果:

這個動畫在Android-Property Animation(屬性動畫)中就實現過了,當時是這么實現的:
[java] view plain copy
  1. private void startValueAnimation(){  
  2.     if(mValueAnimator == null){  
  3.         mValueAnimator = ValueAnimator.ofFloat(0500);  
  4.     }  
  5.     mValueAnimator.setInterpolator(new AnticipateInterpolator());  
  6.     mValueAnimator.setTarget(mImageView);  
  7.     mValueAnimator.setDuration(3000);  
  8.     mValueAnimator.setRepeatCount(1);  
  9.     mValueAnimator.start();  
  10.     mValueAnimator.addUpdateListener(new AnimatorUpdateListener() {  
  11.         @Override  
  12.         public void onAnimationUpdate(ValueAnimator animation) {  
  13.         //同時設置X,Y 兩個屬性  
  14.             mImageView.setTranslationX((Float) animation.getAnimatedValue());  
  15.             mImageView.setTranslationY((Float) animation.getAnimatedValue());  
  16.         }  
  17.     });  
  18. }  
在動畫update的時候同時更新View的X和Y屬性。另外,使用AnimatorSet也能夠實現這類動畫效果。
除這兩種以外還有無呢?就是自定義1個TypeEvaluator,主要代碼以下:
[java] view plain copy
  1. private void startObjectAnimation() {  
  2.     ViewXYHolder viewXYHolder = new ViewXYHolder(mImageView);  
  3.     XYHolder startXY = new XYHolder(0f, 0f);  
  4.     XYHolder endXY = new XYHolder(500f, 500f);  
  5.     ObjectAnimator objectAnimator = ObjectAnimator.ofObject(viewXYHolder, "xY"new XYmEvaluator(), startXY, endXY);  
  6.     objectAnimator.setInterpolator(new LinearInterpolator());  
  7.     objectAnimator.setDuration(3000);  
  8.     objectAnimator.start();  
  9. }  
  10.   
  11. public class XYmEvaluator implements TypeEvaluator {  
  12.     public Object evaluate(float fraction, Object startValue, Object endValue) {  
  13.             XYHolder startXY = (XYHolder) startValue;  
  14.             XYHolder endXY = (XYHolder) endValue;  
  15.         return new XYHolder(startXY.getX() + fraction * (endXY.getX() - startXY.getX()),  
  16.             startXY.getY() + fraction * (endXY.getY() - startXY.getY()));  
  17.     }  
  18. }  
  19.   
  20. public class XYHolder{  
  21.     private float mX;  
  22.     private float mY;  
  23.   
  24.     public XYHolder(float x, float y) {  
  25.     mX = x;  
  26.     mY = y;  
  27. }  
  28.   
  29. public float getX() {  
  30.     return mX;  
  31. }  
  32.   
  33. public void setX(float x) {  
  34.     mX = x;  
  35. }  
  36.   
  37. public float getY() {  
  38.     return mY;  
  39. }  
  40.   
  41. public void setY(float y) {  
  42.     mY = y;  
  43.     }  
  44. }  
  45.   
  46. public class ViewXYHolder{  
  47.     private View imageView;  
  48.     public ViewXYHolder(View view){  
  49.         imageView = view;  
  50.     }  
  51.   
  52. //看到這個是否是感覺跟第1種方法1樣的感腳,只是封裝的不同  
  53. public void setXY(XYHolder xyHolder) {  
  54.     imageView.setX(xyHolder.getX());  
  55.     imageView.setY(xyHolder.getY());  
  56. }  
  57.   
  58. public XYHolder getXY() {  
  59.     return new XYHolder(imageView.getX(), imageView.getY());  
  60. }  
  61. }  
而其實,對這個例子,使用的對象是1個View,Android系統中有封裝View屬性動畫的1個類:ViewPropertyAnimator,其簡單使用方式以下:
[html] view plain copy
  1. private void startViewPropertyAnimation(){  
  2.     ViewPropertyAnimator viewPropertyAnimator;  
  3.     viewPropertyAnimator = mImageView.animate().x(500).y(500);  
  4.     viewPropertyAnimator.setDuration(3000);  
  5.     viewPropertyAnimator.start();  
  6. }  
一樣也能實現上面的動畫效果,只是只局限于View,不管方法怎樣變,但是終究原理都是1樣的,都是要同時改變X 和 Y兩個屬性的值。
復雜1點的例子還是直接參考ApiDemo比較好,ApiDemo里面有個Custom Evaluator,值得學習! 最后引文作者提到了ViewPropertyAnimator類,這個類源碼解釋的意思大概是:這個類可以自動并優化view對象所觸及的某些屬性的動畫,與用單純的Animator控制view屬性動畫不同的是: Animator更合適于只對view對象的1兩個屬性進行操作,當view的很多個屬性都需要動畫操作時用ViewPropertyAnimator比較好,緣由在于后者可以將多個屬性操作放在1次invalidate履行,即多個屬性的變化可以是調用1次刷新。 而單純Animator每個屬性的變化都要單獨調用1次invalidate進行刷新。

生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美porn| 日韩精品一区二区三区av | 亚洲一区二区三区精品视频 | 一级肉体全黄裸片8822tv | 欧美 亚洲 日本 | 久久久久久久久久美女 | 香蕉视频在线播放 | 欧美久久一区二区三区 | www.国产一区 | 欧洲一区 | 久久精品国产一区 | 日韩一区二区三区在线观看 | 岛国大片在线观看 | 少妇精品久久久一区二区三区 | 精品久久久中文字幕 | 99精品视频在线观看 | 热久久久| 国产亚洲视频在线 | 可以在线观看的av网站 | 精品久久9| 98色花堂最新地址网址 | 国产精品2区 | 国产精品久久久久久 | 在线看日韩av | 久久99精品久久久 | 久久精品成人 | 牛牛影视一区二区三区免费看 | 91精品成人久久 | 成人一区二区视频 | 久久精品毛片 | 久久久国产一区 | 成人97| 久久大 | 三级电影免费观看 | 日韩欧美国产精品 | 麻豆久久 | 日韩不卡一区二区 | 91精品国产91久久综合桃花 | 亚洲九九| 久久久国产精品免费 | 日韩欧美在线视频观看 |