屬性動畫是Google在3.0之后才提出的新動畫框架榨婆,相比傳統(tǒng)動畫Animation只是系統(tǒng)不斷調用onDraw方法重繪界面以實現(xiàn)動畫效果。屬性動畫顧名思義是調用get、set方法真實改變屬性逞力。
傳統(tǒng)Animation有很大的局限性:
1.只是重繪了動畫,事件響應位置卻沒有改變糠爬,因此它不適用于具有交互動畫的效果寇荧,只能做顯示效果;
2.不斷調用onDraw方法重繪很浪費資源执隧;
3.位移揩抡,縮放户侥,旋轉,位移四種動畫峦嗤,組合可以實現(xiàn)豐富效果但仍不如屬性蕊唐。
因此,我們有必要好好學習屬性動畫烁设。
看看Google的API Demos替梨,有許多經(jīng)典實用的小例子。(Google的心血)
是時候看一波代碼了:
ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();
讓對象image_iv_main在X軸從0平移到200装黑,時間持續(xù)1000ms副瀑,go!
如果這樣會發(fā)生什么恋谭?
ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();
ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F).setDuration(1000).start();
ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F).setDuration(1000).start();
對象會同時發(fā)生3個動畫糠睡,X軸從0平移到200,Y軸從0平移到200疚颊,旋轉360°狈孔,哈,你猜對了嗎材义?
類似的效果還可以通過以下代碼實現(xiàn)均抽,有點代碼復用的感覺。復用p1其掂,p2到忽,p3:
PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0F,360F);
PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY",0F,200F);
PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationX",0F,200F);
ObjectAnimator.ofPropertyValuesHolder(image_iv_main,p1,p2,p3).setDuration(2000).start();
進階玩法,玩組合:
ObjectAnimator animator1 = ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F);
AnimatorSet set = new AnimatorSet();
set.playTogether(animator1,animator2,animator3);
set.setDuration(2000);
set.start();
可以一起玩清寇,還可以順序玩:
set.playSequentially(animator1,animator2,animator3);
還可以調順序:
set.play(animator1).with(animator2);
set.play(animator3).after(animator2);
更多玩法,等我們去探索~
最后送上完整源代碼护蝶,high起來:
package com.example.quan.quanstudy.objectAnimator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
import com.example.quan.quanstudy.R;
import com.example.quan.quanstudy.base.BaseActivity;
/**
* Created by xingquan.he on 2017/3/15.
* Mr.Quan
* 屬性動畫第一課
* 項目實戰(zhàn):淺談屬性動畫(1)-探索新玩法 http://blog.csdn.net/hxqneuq2012/article/details/52301791
*/
public class FirstClassActivity extends BaseActivity {
private ImageView mImageAnimator;
@Override
public int getLayoutId() {
return R.layout.activity_first_animator;
}
@Override
public void initView() {
mImageAnimator = (ImageView) findViewById(R.id.click_iv_animator);
}
public void click(View view){
Toast.makeText(this,"Clickd.",Toast.LENGTH_SHORT).show();
}
public void move(View view){
// ObjectAnimator.ofFloat(image_iv_main,"translationX",0F,200F).setDuration(1000).start();
// ObjectAnimator.ofFloat(image_iv_main,"translationY",0F,200F).setDuration(1000).start();
// ObjectAnimator.ofFloat(image_iv_main,"rotation",0F,360F).setDuration(1000).start();
// PropertyValuesHolder p1 = PropertyValuesHolder.ofFloat("rotation",0F,360F);
// PropertyValuesHolder p2 = PropertyValuesHolder.ofFloat("translationY",0F,200F);
// PropertyValuesHolder p3 = PropertyValuesHolder.ofFloat("translationX",0F,200F);
// ObjectAnimator.ofPropertyValuesHolder(image_iv_main,p1,p2,p3).setDuration(2000).start();
ObjectAnimator animator1 = ObjectAnimator.ofFloat(mImageAnimator,"translationX",0F,200F);
ObjectAnimator animator2 = ObjectAnimator.ofFloat(mImageAnimator,"translationY",0F,200F);
ObjectAnimator animator3 = ObjectAnimator.ofFloat(mImageAnimator,"rotation",0F,360F);
AnimatorSet set = new AnimatorSet();
//set.playTogether(animator1,animator2,animator3);
//set.playSequentially(animator1,animator2,animator3);
set.play(animator1).with(animator2);
set.play(animator3).after(animator2);
set.setDuration(1000);
set.start();
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<ImageView
android:layout_width="100dp"
android:layout_height="56dp"
android:src="@mipmap/quan_shijiazhuang"
android:onClick="click"
android:id="@+id/click_iv_animator"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="move"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="50dp"
android:onClick="move"
android:id="@+id/move_btn_animator"
/>
</RelativeLayout>
原創(chuàng)不易华烟,轉載請注明出處哈。
權興權意
產(chǎn)品可以更優(yōu)雅~
項目實戰(zhàn):淺談屬性動畫(1)-探索新玩法 - hxqneuq2012的專欄 - 博客頻道 - CSDN.NET http://blog.csdn.net/hxqneuq2012/article/details/62216824
項目源代碼持灰,歡迎提建議(star)盔夜。
https://github.com/HXQWill/QuanStudy/tree/master/app/src/main/java/com/example/quan/quanstudy/objectAnimator