標籤:property animation android
Property Animation 屬性動畫,這個是在Android 3.0中才引進的。
Property Animation其改變的是對象屬性對應的值,應用於任何對象,而Tween Animation更改的是繪畫的效果,其屬性值是沒有變化的。
ObjectAnimator:更改對象的屬性值
使用方法:
ObjectAnimator translationRight = ObjectAnimator.ofFloat(m_tv, "X",width);
translationRight.setDuration(1500);
translationRight.start();
這是一個平移的效果,ObjectAnimator.ofFloat(m_tv, "X",width);第一個參數是:對象 第二個參數:該對象的屬性,屬性有必須有對應的getX()和setX()方法,第三個參數:一個可變參數的值
如果唯寫了一個值,那麼就預設從當前的值變為填寫的值,如果填寫了多個,就按照填寫的順序做相應的變化,
setDuration():設定啟動並執行時間,start():開啟
AnimatorSet:將多個ObjectAnimator的效果一直執行或則按照先後順序執行
ObjectAnimator translationRight = ObjectAnimator.ofFloat(m_tv, "X",width);
ObjectAnimator translationLeft = ObjectAnimator.ofFloat(m_tv, "X", 0f);
ObjectAnimator translationDown = ObjectAnimator.ofFloat(m_tv, "Y",height);
ObjectAnimator translationUp = ObjectAnimator.ofFloat(m_tv, "Y", 0);
AnimatorSet as = new AnimatorSet();
as.play(translationRight).before(translationLeft);
as.play(translationRight).with(translationDown);
as.play(translationLeft).with(translationUp);
paly() with() before() after() 設定執行的順序 start()開啟
下面代碼的介紹請參考: http://www.open-open.com/lib/view/open1329994048671.html
ValueAnimator,TypeEvalutors,TimeInterplator,Keyframes
部分範例程式碼:
<span style="font-family:SimSun;font-size:18px;"><?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/image" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/girl" /> <EditText android:id="@+id/edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="5dp" /></LinearLayout></span>
實現:
<span style="font-family:SimSun;font-size:18px;">private void initImageAnimationSet() {ImageView image = (ImageView) findViewById(R.id.image);ObjectAnimator obOutX = ObjectAnimator.ofFloat(image, "X", 200);ObjectAnimator obOutY = ObjectAnimator.ofFloat(image, "Y", 200);ObjectAnimator obInX = ObjectAnimator.ofFloat(image, "X", 0);ObjectAnimator obInY = ObjectAnimator.ofFloat(image, "Y", 0);final AnimatorSet set = new AnimatorSet();set.play(obOutX).with(obOutY);set.play(obInX).with(obInY);set.play(obOutX).before(obInX);image.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {set.start();}});}/** * ObjectAnimator使用 */private void initEditObjectAnimator() {EditText edt = (EditText) findViewById(R.id.edit_text);final ObjectAnimator translationRight = ObjectAnimator.ofFloat(edt,"X", 0, 50, -10, 40, -5, 30, -3, 20, -1, 10, 0);translationRight.setDuration(1500);edt.addTextChangedListener(new TextWatcher() {@Overridepublic void onTextChanged(CharSequence s, int start, int before,int count) {}@Overridepublic void beforeTextChanged(CharSequence s, int start, int count,int after) {}@Overridepublic void afterTextChanged(Editable s) {if (TextUtils.isEmpty(s.toString())) {translationRight.start();}}});}</span>
案例代碼下載:
:
android動畫-Property Animation