屬性動畫 ValueAnimator 和 ObjectAnimator 之間的區別
阿新 • • 發佈:2019-01-27
ValueAnimator
是 對 值的平滑過渡動畫。什麼意思呢。就是對數值在一定時間內進行平滑過渡。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ValueAnimator animator = ValueAnimator.ofFloat(0 f, 100f);
animator.setDuration(5 * 1000);//設定動畫的持續時間
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.i("hhhd", "value is"+animation.getAnimatedValue());
}
});
animator.start();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
這裡註冊了一個監聽器的回撥,每次動畫的狀態發生改變,都會回撥這個方法。我們這裡打印出了animation.getAnimatedValue的值,實際上就是fraction的值,也就是完成度,一個動畫從開始到結束,完成的百分比。當然fraction值為0-1。一部分日誌列印為
ObjectAnimator
與ValueAnimator不同的是,ObjectAnimator是對 物件的屬性 進行平滑過渡。
package com.example.administrator.myanimator;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by hd on 2015/12/19.
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button alpha;
private Button rotation;
private Button translation;
private Button scale;
private TextView textView;
ObjectAnimator animator;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
alpha = (Button) findViewById(R.id.alphaBtn);
rotation = (Button) findViewById(R.id.rotationBtn);
translation = (Button) findViewById(R.id.translationBtn);
scale = (Button) findViewById(R.id.scale);
alpha.setOnClickListener(this);
rotation.setOnClickListener(this);
translation.setOnClickListener(this);
scale.setOnClickListener(this);
textView = (TextView) findViewById(R.id.tvTest);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.alphaBtn:
//透明度動畫,值範圍為0-1,0表示完全透明,1表示完全不透明
animator = ObjectAnimator.ofFloat(textView, "alpha", 1, 0, 1);
animator.setDuration(5 * 1000);
break;
case R.id.rotationBtn:
//旋轉動畫,第一個數為初始狀態,值可正可負
animator = ObjectAnimator.ofFloat(textView, "rotation", 0f, 360f);
animator.setDuration(5 * 1000);
break;
case R.id.translationBtn:
//獲取當前物件在螢幕中的X座標
float curTranslationx = textView.getTranslationX();
//X軸方向平移動畫,500f表示在curTranslation位置向右平移半屏,因為上下距離都預設為1000,-500f表示移動到curTranlation位置的左半螢幕位置,最後移回原位。
animator = ObjectAnimator.ofFloat(textView, "translationX", curTranslationx, 500f, -500f,curTranslationx);
animator.setDuration(5 * 1000);
break;
case R.id.scale:
//比例動畫,這裡把物件的比例擴大或者縮小的動畫
animator = ObjectAnimator.ofFloat(textView, "scaleY", 1f, 5f, 4f, 3f, 1f);
animator.setDuration(5 * 1000);
break;
}
animator.start();
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentTop="true">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/alphaBtn"
android:text="alpha"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/rotationBtn"
android:text="totation"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/translationBtn"
android:text="translation"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:id="@+id/scale"
android:text="scale"/>
</LinearLayout>
<TextView
android:id="@+id/tvTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Hello World!"
android:textSize="30dp" />
</RelativeLayout>