屬性動畫(基礎)
阿新 • • 發佈:2018-12-21
其中包含平移,圖片旋轉,改變事件等
一、自定義一個類進行屬性動畫配置並在main佈局中引用+組合動畫
package com.gj.animator; import android.animation.ObjectAnimator; import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.AppCompatImageView; import android.util.AttributeSet; import android.view.View; import android.view.animation.Animation; import android.view.animation.LinearInterpolator; public class MusicButton extends AppCompatImageView implements View.OnClickListener { private ObjectAnimator objectAnimator; public static final int STATE_PLAYING =1;//正在播放 public static final int STATE_PAUSE =2;//暫停 public int state; public MusicButton(Context context) { super(context); init(); } public MusicButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public MusicButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { //第一個引數:Object物件 //第二個引數:屬性:旋轉:rotation;平移:translationX/translationY //縮放:scaleX/scaleY;透明:alpha //第三個引數:起始值 //第四個引數:結束值 objectAnimator = ObjectAnimator.ofFloat( this,"rotation",0,360); //持續1秒 objectAnimator.setDuration(1000); //設定重複次數無限次 ObjectAnimator.INFINITE objectAnimator.setRepeatCount(ObjectAnimator.INFINITE); //設定為勻速 setInterpolator objectAnimator.setInterpolator(new LinearInterpolator()); //設定動畫開始 objectAnimator.start(); state = STATE_PLAYING; //設定點選事件 setOnClickListener(this); } @Override public void onClick(View v) { if(state==STATE_PAUSE){ //動畫重新開始 objectAnimator.resume(); state = STATE_PLAYING; }else{ objectAnimator.pause();//動畫暫停 state = STATE_PAUSE; } }
}
二、Main層(button的倒計時 組合動畫)
package com.gj.animator; import android.animation.AnimatorSet; import android.animation.ObjectAnimator; import android.animation.ValueAnimator; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.view.animation.AnimationSet; import android.view.animation.LinearInterpolator; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { private ImageView goodsImage; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button button = findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //設定從0到10 數字value的動畫 int型別 ValueAnimator valueAnimator = ValueAnimator.ofInt(60,0); //設定持續時間 valueAnimator.setDuration(60*1000); //設定數字勻速 valueAnimator.setInterpolator(new LinearInterpolator()); //監聽每次改變時的值 只有一個監聽器addUpdateListener valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { @Override public void onAnimationUpdate(ValueAnimator animation) { //獲取值 int value = (Integer) animation.getAnimatedValue(); //賦值 button.setText(value+""); } }); //開始動畫 valueAnimator.start(); } }); goodsImage = findViewById(R.id.goods_image); goodsImage.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //設定動畫 第一個引數本控制元件(平移) ObjectAnimator translationX = ObjectAnimator.ofFloat(goodsImage, "translationX", 0, 300f); ObjectAnimator translationY = ObjectAnimator.ofFloat(goodsImage, "translationY", 0, 500f); //設定旋轉 ObjectAnimator rotation = ObjectAnimator.ofFloat(goodsImage, "rotation", 0, 360); //設定組合動畫 AnimatorSet animatorSet = new AnimatorSet(); //總的意思呢就是在執行平移Y動畫之前執行平移X+旋轉 animatorSet.play(translationX).with(rotation).before(translationY); //設定動畫 同時實現 // animatorSet.playTogether(translationX,translationY,rotation); animatorSet.setDuration(3000);//設定動畫時間 animatorSet.start();//啟動 } }); }
}
三、佈局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:orientation="vertical" 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" tools:context=".MainActivity"> <com.gj.animator.MusicButton android:id="@+id/musicButton" android:layout_width="80dp" android:layout_height="80dp" app:srcCompat="@drawable/music"/> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Button"/> <ImageView android:id="@+id/goods_image" android:layout_width="30dp" android:layout_height="30dp" app:srcCompat="@mipmap/ic_launcher_round"/>