屬性動畫實現透明,旋轉,平移,縮放,集合動畫
阿新 • • 發佈:2018-12-21
佈局檔案,設定透明,旋轉,平移,縮放,集合動畫的按鈕,再設定一個屬性動畫的imageview
<?xml version="1.0" encoding="utf-8"?>
<ImageButton
android:id="@+id/img"
android:layout_width=“200dp”
android:layout_height=“200dp”
android:src="@drawable/aaa"//這裡自己隨意設定一張圖片就好,用來做動畫
android:layout_gravity=“center”
android:background="@null"/>
MainActvity中實現功能
package com.example.animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;
public class MainActivity extends Activity implements View.OnClickListener{
private Button btnAlpha; private Button btnRotationy; private Button btnScaleX; private Button btnTranslationY; private Button btnAnimatorSet; private ImageButton img; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化控制元件 initView(); } private void initView() {//找控制元件設定點選事件 btnAlpha = findViewById(R.id.btn_alpha); btnRotationy = findViewById(R.id.btn_rotationY); btnScaleX = findViewById(R.id.btn_scaleX); btnTranslationY = findViewById(R.id.btn_translationY); btnAnimatorSet = findViewById(R.id.btn_AnimatorSet); img = findViewById(R.id.img); btnAlpha.setOnClickListener(this); btnRotationy.setOnClickListener(this); btnScaleX.setOnClickListener(this); btnTranslationY.setOnClickListener(this); btnAnimatorSet.setOnClickListener(this); img.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()){
//注意:以下實現屬性動畫的 ObjectAnimator.ofFloat中第二個引數都是跟自己要實現的動畫名稱一樣,否則會出錯。例如透明動畫第二個引數就是它的英文單詞alpha,不能寫錯;
case R.id.btn_alpha://透明動畫
ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
alpha.setDuration(2000);
alpha.setRepeatCount(1);
alpha.setRepeatMode(ObjectAnimator.RESTART);
alpha.start();
break;
case R.id.btn_rotationY://旋轉動畫
ObjectAnimator rotationY = ObjectAnimator.ofFloat(img, "rotationY", new float[]{90f, 180f, 270f, 360f});
rotationY.setDuration(2000);
rotationY.setRepeatCount(1);
rotationY.setRepeatMode(ObjectAnimator.RESTART);
rotationY.start();
break;
case R.id.btn_scaleX://縮放動畫
ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", new float[]{1f, 2f, 3f, 4f, 5f, 6f, 1f});
scaleX.setDuration(2000);
scaleX.setRepeatCount(1);
scaleX.setRepeatMode(ObjectAnimator.RESTART);
scaleX.start();
break;
case R.id.btn_translationY://平移動畫
ObjectAnimator translationY = ObjectAnimator.ofFloat(img, "translationY", new float[]{10f, 20f, 30f, 40f, 60f,80f});
translationY.setDuration(2000);
translationY.setRepeatCount(1);
translationY.setRepeatMode(ObjectAnimator.RESTART);
translationY.start();
break;
case R.id.btn_AnimatorSet://集合動畫
AnimatorSet animatorSet = new AnimatorSet();
ObjectAnimator oa = ObjectAnimator.ofFloat(img, "translationX", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
oa.setDuration(3000);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(img, "translationY", new float[]{-10f, -20f, -30f, -40f, -60f, -80f});
oa2.setDuration(3000);
animatorSet.playTogether(oa, oa2);
animatorSet.start();
break;
case R.id.img://點選動畫
Toast.makeText(this, "你點選了我", Toast.LENGTH_SHORT).show();
break;
}
}
}