Android : 屬性動畫
阿新 • • 發佈:2018-12-20
先在res寫一個animator資料夾
建立一個set檔案
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:ordering="sequentially"> <set android:ordering="together"> <objectAnimator android:duration="2000" android:propertyName="translationX" android:valueFrom="0" android:valueTo="360" android:valueType="floatType"></objectAnimator> <objectAnimator android:duration="3000" android:propertyName="rotation" android:valueFrom="0" android:valueTo="720" android:valueType="floatType"></objectAnimator> </set> <set android:ordering="sequentially"> <objectAnimator android:duration="1500" android:propertyName="alpha" android:valueFrom="0" android:valueTo="1" android:valueType="floatType"></objectAnimator> <objectAnimator android:duration="1500" android:propertyName="alpha" android:valueFrom="1" android:valueTo="0" android:valueType="floatType"></objectAnimator> </set> </set>
xml佈局檔案
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity"> <Button android:id="@+id/Start_Anim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="開始吧" /> <Button android:id="@+id/Object_Anim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="ObjectAnimator開始吧" /> <Button android:id="@+id/XML_Anim" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="XMl開始吧" /> </LinearLayout>
acativity裡面的程式碼
package soexample.umeng.com.shuxingdonghua; import android.animation.AnimatorInflater; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends AppCompatActivity implements View.OnClickListener { private Button Start_Anim; private Button Object_Anim; private Button XML_Anim; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); } private void initView() { Start_Anim = (Button) findViewById(R.id.Start_Anim); Object_Anim = (Button) findViewById(R.id.Object_Anim); XML_Anim = (Button) findViewById(R.id.XML_Anim); Start_Anim.setOnClickListener(this); Object_Anim.setOnClickListener(this); XML_Anim.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.Start_Anim: valueAnimStart(); Toast.makeText(this, "呂小帥", Toast.LENGTH_SHORT).show(); break; case R.id.Object_Anim: //引數1:給某個物件設定動畫,引數2:是什麼動畫呢 // ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(Start_Anim, "alpha", 1f, 0f, 1f); // objectAnimator.setDuration(2000); // objectAnimator.start(); // 步驟1:設定需要組合的動畫效果 ObjectAnimator translation = ObjectAnimator.ofFloat(Start_Anim, "translationX", 0, 300, 500); // 平移動畫 ObjectAnimator rotate = ObjectAnimator.ofFloat(Start_Anim, "rotation", 0f, 360f); // 旋轉動畫 ObjectAnimator alpha = ObjectAnimator.ofFloat(Start_Anim, "alpha", 1f, 0f, 1f); // 透明度動畫 // 步驟2:建立組合動畫的物件 AnimatorSet animSet = new AnimatorSet(); // 步驟3:根據需求組合動畫 // AnimatorSet.play(Animator anim) :播放當前動畫 // AnimatorSet.after(long delay) :將現有動畫延遲x毫秒後執行 // AnimatorSet.with(Animator anim) :將現有動畫和傳入的動畫同時執行 // AnimatorSet.after(Animator anim) :將現有動畫插入到傳入的動畫之後執行 // AnimatorSet.before(Animator anim) : 將現有動畫插入到傳入的動畫之前執行 animSet.play(translation).with(rotate).before(alpha); animSet.setDuration(5000); // 步驟4:啟動動畫 animSet.start(); break; case R.id.XML_Anim: AnimatorSet set = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.set_animator); set.setTarget(Object_Anim); set.start(); break; } } private void valueAnimStart() { //第一步建立物件 都是通過類名呼叫即可 //ofFloat float...可變引數 ValueAnimator valueAnimator = ValueAnimator.ofInt(Start_Anim.getLayoutParams().width, 500); valueAnimator.setDuration(3000); valueAnimator.setStartDelay(500); valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //移動的單位 得值是跟你賦值物件的時候那個方法要一致 int currentValue = (Integer) animation.getAnimatedValue(); Start_Anim.getLayoutParams().width = currentValue; Log.e("currentValue", currentValue + "單位"); //重新整理自定義View的方法 // invalidate(); // postInvalidate() //RequestLayout 三個方法都會執行 Start_Anim.requestLayout(); } }); valueAnimator.start(); } }
這就是一個簡單動畫,程式碼也很簡單.