动画

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

private Button btn_1;
private Button btn_2;
private Button btn_3;
private Button btn_4;
private Button btn_5;
private ImageView img;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    init();

// 引用资源
Animator Dh = AnimatorInflater.loadAnimator(this, R.animator.objectanimator);
// 把要做动画控件对象放进去
Dh.setTarget(img);
// 开启动画.
Dh.start();
}

private void init() {
    btn_1 = findViewById(R.id.btn_1);
    btn_2 = findViewById(R.id.btn_2);
    btn_3 = findViewById(R.id.btn_3);
    btn_4 = findViewById(R.id.btn_4);
    btn_5 = findViewById(R.id.btn_5);
    img = findViewById(R.id.img);
    //添加点击事件
    btn_1.setOnClickListener(this);
    btn_2.setOnClickListener(this);
    btn_3.setOnClickListener(this);
    btn_4.setOnClickListener(this);
    btn_5.setOnClickListener(this);
}

@Override
public void onClick(View v) {
     switch (v.getId()){
         //渐变动画
         case R.id.btn_1:
             ObjectAnimator alpha = ObjectAnimator.ofFloat(img, "alpha", new float[]{0.0f, 0.2f, 0.4f, 0.6f, 0.8f, 1.0f});
             alpha.setDuration(2000);
             alpha.setRepeatMode(ObjectAnimator.RESTART);
             alpha.setRepeatCount(1);
             alpha.start();
             break;

             //旋转动画
         case R.id.btn_2:
             ObjectAnimator rotationY = ObjectAnimator.ofFloat(img, "rotationY", new float[]{90f, 180f, 270f, 360f});
             rotationY.setDuration(4000);
             rotationY.setRepeatMode(ObjectAnimator.RESTART);
             rotationY.setRepeatCount(1);
             rotationY.start();
             break;

         //做缩放动画
         case R.id.btn_3:
             ObjectAnimator scaleX = ObjectAnimator.ofFloat(img, "scaleX", new float[]{1f, 3f, 4f, 6f, 7f, 6f, 1f});
             scaleX.setDuration(4000);
             scaleX.setRepeatMode(ObjectAnimator.RESTART);
             scaleX.setRepeatCount(3);
             scaleX.start();
             break;
             //平移动画
         case R.id.btn_4:
             ObjectAnimator translationY = ObjectAnimator.ofFloat(img, "translationY", new float[]{10f, 20f, 30f, 40f, 60f, 80f});
             translationY.setDuration(2000);
             translationY.setRepeatMode(ObjectAnimator.RESTART);
             translationY.setRepeatCount(2);
             translationY.start();
             break;
             //动画集
         case R.id.btn_5:
             AnimatorSet set = new AnimatorSet();
             ObjectAnimator pp1 = ObjectAnimator.ofFloat(img, "translationX", new float[]{40f, 50f, 90f, 100f, 110f, 120f});
             pp1.setDuration(3000);
             ObjectAnimator PP2 = ObjectAnimator.ofFloat(img, "translationY", new float[]{-10f, -20f, -30f, -40f, -60f, -80f});
             PP2.setDuration(3000);
            // set.playTogether(pp1, PP2);
             set.playSequentially(pp1,PP2);
             set.start();
             break;

     }
}

}
res文件夹下animator里面
objectanimator文件
{objectAnimator xmlns:android=“http://schemas.android.com/apk/res/android
android:propertyName=“rotationX”
android:duration=“3000”
android:repeatCount=“1”
android:repeatMode=“reverse”
android:startOffset=“0”
android:valueFrom=“360.0”
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值