Android属性动画 旋转





    

    

    


上面是布局文件  在资源文件里面设置自己的图片资源 

接下来直接上代码  废话不多说

package com.example.dell.myapplication;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.ViewGroup;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private ImageView imageView;
    private ImageView imageView2;
    private ImageView imageView3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.item_jiudian);
        initView();
        //设置时长
        creatAndStartAnimator(imageView,5000);
        creatAndStartAnimator(imageView2,3000);
        creatAndStartAnimator(imageView3,2000);
    }

    private void initView() {
        imageView = (ImageView) findViewById(R.id.imageView);
        imageView2 = (ImageView) findViewById(R.id.imageView2);
        imageView3 = (ImageView) findViewById(R.id.imageView3);
    }
    private void creatAndStartAnimator(final ImageView view,long duration){
        final ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) view.getLayoutParams();
        //设置view得参数
        final ValueAnimator animator = ValueAnimator.ofFloat(layoutParams.circleAngle + 360,layoutParams.circleAngle);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            // 添加动画的监听事件    动画师更新监听器
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                //实现动画更新
                layoutParams.circleAngle= (float) animator.getAnimatedValue();
                view.setLayoutParams(layoutParams);
                //设置参数
            }
        });
        //设置时长
        animator.setDuration(duration);
        //设置内插器  烦卡顿
        animator.setInterpolator(new LinearInterpolator());
        //设置重复计数          无线循环
        animator.setRepeatCount(ValueAnimator.INFINITE);
        //启动
        animator.start();
    }
}

之后直接运行!如果报错可能是自己依赖没导的原因!

 

implementation 'com.android.support.constraint:constraint-layout:1.1.2'

 

你可能感兴趣的:(a's')