Android 3.0之前已有動(dòng)畫框架Animation(詳見:Android之視圖動(dòng)畫Animation)邪驮,但存在一些局限性,當(dāng)某個(gè)元素發(fā)生視圖動(dòng)畫后网梢,其響應(yīng)事件位置還在動(dòng)畫前的地方履澳。于是3.0之后,Google提出了屬性動(dòng)畫哭廉。
ObjectAnimator
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "translationX", 300);
objectAnimator1.setInterpolator(new AccelerateInterpolator());
objectAnimator1.setDuration(2000);
objectAnimator.setRepeatCount(ValueAnimator.INFINITE);//Animation.INFINITE 表示重復(fù)多次
objectAnimator.setRepeatMode(ValueAnimator.RESTART);//RESTART表示從頭開始脊僚,REVERSE表示從末尾倒播
objectAnimator1.start();
第一個(gè)參數(shù):操縱的view
第二個(gè)參數(shù):操縱的動(dòng)畫屬性值
第三個(gè)參數(shù):可變數(shù)組參數(shù)
動(dòng)畫屬性值
translationX和translationY:增量控制view從它布局容器左上角坐標(biāo)偏移
ObjectAnimator.ofFloat(imageView, "translationX", 300f);
rotation、rotationX遵绰、rotationY:控制view繞支點(diǎn)進(jìn)行2D或3D旋轉(zhuǎn)
ObjectAnimator.ofFloat(imageView, "rotation", 360);
scaleX辽幌、scaleY:控制view繞支點(diǎn)進(jìn)行2D縮放
ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0.5f,1f);
alpha:控制view透明度椿访,默認(rèn)是1(不透明)乌企,0完全透明(不可見)
ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.5f);
x和y:描述view在容器最終位置
可變數(shù)組參數(shù)
可以有一個(gè)到N個(gè),如果是一個(gè)值的話默認(rèn)這個(gè)值是動(dòng)畫過渡值的結(jié)束值成玫。如果有N個(gè)值加酵,動(dòng)畫就在這N個(gè)值之間過渡。
動(dòng)畫監(jiān)聽
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);
objectAnimator1.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
一般我們只關(guān)心onAnimationEnd哭当,所以Android提供了AnimatorListenerAdapter:
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 0.5f, 1f);
objectAnimator1.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
ValueAnimator
ValueAnimator 本身不提供任何動(dòng)畫效果猪腕,像個(gè)數(shù)值 發(fā)生器,用來產(chǎn)生具有一點(diǎn)規(guī)律數(shù)字钦勘。
ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 100);
valueAnimator.setTarget(imageView);
valueAnimator.setDuration(2000).start();
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Int value = (Integer) animation.getAnimatedValue();
//TODO use the value
Toast.makeText(getApplicationContext(), "value=" + value, Toast.LENGTH_LONG).show();
}
});
PropertyValuesHolder
針對(duì)同一個(gè)對(duì)象多個(gè)屬性陋葡,同時(shí)作用多種動(dòng)畫
PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofFloat("translationX", 300f);
PropertyValuesHolder propertyValuesHolder2 = PropertyValuesHolder.ofFloat("alpha", 1f, 0.5f);
PropertyValuesHolder propertyValuesHolder3 = PropertyValuesHolder.ofFloat("scaleX", 1f, 0, 1f);
PropertyValuesHolder propertyValuesHolder4 = PropertyValuesHolder.ofFloat("scaleY", 1f, 0, 1f);
ObjectAnimator.ofPropertyValuesHolder(imageView, propertyValuesHolder1, propertyValuesHolder2, propertyValuesHolder3, propertyValuesHolder4)
.setDuration(5000).start();
AnimatorSet
與PropertyValuesHolder類似,但AnimatorSet多了playTogether(同時(shí)執(zhí)行)彻采、playSequentially(順序執(zhí)行)腐缤、play(objectAnimator1).with(objectAnimator2)、before肛响、after這些方法協(xié)同工作岭粤。
ObjectAnimator objectAnimator1 = ObjectAnimator.ofFloat(imageView, "alpha", 1f, 0.5f);
ObjectAnimator objectAnimator2 = ObjectAnimator.ofFloat(imageView, "translationY", 300);
ObjectAnimator objectAnimator3 = ObjectAnimator.ofFloat(imageView, "scaleX", 1f, 0, 1f);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(5000);
animatorSet.playTogether(objectAnimator1, objectAnimator2,objectAnimator3);
animatorSet.start();
xml使用屬性動(dòng)畫
res下建立animator文件夾,然后建立res/animator/set_animator.xml
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="2000"
android:propertyName="alpha"
android:valueFrom="0.1"
android:valueTo="1.0"
android:valueType="floatType" />
調(diào)用:
Animator animator = AnimatorInflater.loadAnimator(getApplicationContext(), R.animator.set_animator);
animator.setTarget(imageView);
animator.start();
動(dòng)畫組合
set標(biāo)簽终惑,有一個(gè)orderring屬性設(shè)置為together绍在,還有另一個(gè)值:sequentially(表示一個(gè)接一個(gè)執(zhí)行)。
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:ordering="together">
<objectAnimator
android:duration="1000"
android:propertyName="scaleX"
android:valueFrom="1"
android:valueTo="0.5" />
<objectAnimator
android:duration="1000"
android:propertyName="scaleY"
android:valueFrom="1"
android:valueTo="0.5" />
</set>
View的animate方法
Android 3.0后,谷歌給View增加animate方法直接驅(qū)動(dòng)屬性動(dòng)畫偿渡。
imageView.animate()
.alpha(0.5f)
.y(300)
.setDuration(2000)
//api min is 16
.withStartAction(new Runnable() {
@Override
public void run() {
}
})
//api min is 16
.withEndAction(new Runnable() {
@Override
public void run() {
}
})
.start();
布局動(dòng)畫
設(shè)置子View過渡動(dòng)畫
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="@dimen/activity_vertical_margin">
<ImageView
android:id="@+id/imageMove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@mipmap/ic_launcher" />
</LinearLayout>
LinearLayout parentLayout = (LinearLayout) findViewById(R.id.parentLayout);
ScaleAnimation scaleAnimation=new ScaleAnimation(0,1,0,1);
scaleAnimation.setDuration(2000);
LayoutAnimationController layoutAnimationController=new LayoutAnimationController(scaleAnimation,0.5f);
layoutAnimationController.setOrder(LayoutAnimationController.ORDER_NORMAL);
parentLayout.setLayoutAnimation(layoutAnimationController);
關(guān)于作者
微信公眾號(hào)
我的微信公眾號(hào):吳小龍同學(xué)
不止于技術(shù)分享臼寄,不取悅別人,寫給懂的人看~
歡迎微信掃一掃關(guān)注