- 首页 > it技术 > >
首先我们要在res下创建一个包animator
把 set改成objectanimator
布局
package com.example.animation;
import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {private Button alpha_bt;
private Button translationY_bt;
private Button scaleX_bt;
private Button rotationY_bt;
private Button AnimatorSet_bt;
private ImageButton animation_iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
Animator Xmlanimator = AnimatorInflater.loadAnimator(this, R.animator.object);
//把要做动画控件对象放进去.Animator.setTarget(View对象);
222
Xmlanimator.setTarget(animation_iv);
//开启动画.Animator.start.222
Xmlanimator.start();
}public void setImage(View view) {
System.out.println("巴啦啦能量");
}private void initView() {
alpha_bt = (Button) findViewById(R.id.alpha_bt);
translationY_bt = (Button) findViewById(R.id.translationY_bt);
scaleX_bt = (Button) findViewById(R.id.scaleX_bt);
rotationY_bt = (Button) findViewById(R.id.rotationY_bt);
AnimatorSet_bt = (Button) findViewById(R.id.AnimatorSet_bt);
animation_iv = (ImageButton) findViewById(R.id.animation_iv);
alpha_bt.setOnClickListener(this);
translationY_bt.setOnClickListener(this);
scaleX_bt.setOnClickListener(this);
rotationY_bt.setOnClickListener(this);
AnimatorSet_bt.setOnClickListener(this);
animation_iv.setOnClickListener(this);
}@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.alpha_bt:
//做透明动画,参数1:View,代表你要修改那个控件的属性. 参数2:propertyName代表实现什么样子的动画:"alpha",String类型.
//参数3:float... values,控件修改的参数,new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f}
ObjectAnimator alpha = ObjectAnimator.ofFloat(animation_iv, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
//设置动画执行时长.setDuration
alpha.setDuration(2000);
//设置动画执行的模式setRepeatMode,参数用ObjectAnimator引用.
alpha.setRepeatMode(ObjectAnimator.RESTART);
//设置动画执行的次数.setRepeatCount
alpha.setRepeatCount(1);
//使用ObjectAnimator对象开启动画.
alpha.start();
break;
case R.id.translationY_bt:
//做旋转动画,"rotationY".rotationX,rotationnew float[]{90f, 180f, 270f, 360f}
ObjectAnimator rotationY = ObjectAnimator.ofFloat(animation_iv, "rotationY", new float[]{90f, 180f, 270f, 360f});
rotationY.setDuration(2000);
rotationY.setRepeatMode(ObjectAnimator.RESTART);
rotationY.setRepeatCount(1);
rotationY.start();
break;
case R.id.scaleX_bt:
//做缩放动画,scaleX,scaleYnew float[]{1f, 2f, 3f, 4f, 5f, 6f,1f}
ObjectAnimator scaleX = ObjectAnimator.ofFloat(animation_iv, "scaleX", new float[]{1f, 2f, 3f, 4f, 5f, 6f, 1f});
scaleX.setDuration(2000);
scaleX.setRepeatMode(ObjectAnimator.RESTART);
scaleX.setRepeatCount(1);
scaleX.start();
break;
case R.id.rotationY_bt:
//做平移动画,translationY,translationX new float[]{10f, 20f, 30f, 40f, 60f, 80f}
ObjectAnimator translationY = ObjectAnimator.ofFloat(animation_iv, "translationY", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
translationY.setDuration(2000);
translationY.setRepeatMode(ObjectAnimator.RESTART);
translationY.setRepeatCount(1);
translationY.start();
break;
case R.id.AnimatorSet_bt:
//做动画集合AnimatorSet,分别创建两个动画对象.注意playTogether(动画对象...)和playSequentially的区别.最后开启动画.start
AnimatorSet set = new AnimatorSet();
ObjectAnimator oa = ObjectAnimator.ofFloat(animation_iv, "translationX", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
oa.setDuration(3000);
ObjectAnimator oa2 = ObjectAnimator.ofFloat(animation_iv, "translationY", new float[]{-10f, -20f, -30f, -40f, -60f, -80f});
oa2.setDuration(3000);
set.playTogether(oa, oa2);
set.start();
break;
case R.id.animation_iv:break;
}
}
}
推荐阅读