安卓旋转动画播放暂停

转自https://blog.csdn.net/lly347705530/article/details/78671696#commentBox
使用自安卓3.0开始支持的属性动画ObjectAnimator实现这个效果,从ImageView派生出来,调用ObjectAnimator的方法即可。

ImagePlayButton.java

/**
 * 属性动画 ObjectAnimator
 * https://blog.csdn.net/lly347705530/article/details/78671696#commentBox
 */
package com.example.kw.rotatetest;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;
import android.view.animation.LinearInterpolator;

import java.util.jar.Attributes;

public class ImagePlayButton extends AppCompatImageView {
    private ObjectAnimator objectAnimator;
    public static final int STATE_PLAYING = 1; // 播放
    public static final int STATE_PAUSE = 2; // 暂停
    public static final int STATE_STOP = 3; // 停止
    public int state;

    public ImagePlayButton(Context context){
        super(context);
        init();
    }

    public ImagePlayButton(Context context, AttributeSet attrs){
        super(context, attrs);
        init();
    }

    public ImagePlayButton(Context context, AttributeSet attrs, int defStyleAttr){
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        state = STATE_STOP;
        objectAnimator = ObjectAnimator.ofFloat(this, "rotation", 0f, 360f);//添加旋转动画,旋转中心默认为控件中点
        objectAnimator.setDuration(3000);
        objectAnimator.setInterpolator(new LinearInterpolator());//动画时间线性渐变(匀速)
        objectAnimator.setRepeatCount(ObjectAnimator.INFINITE);//循环
        objectAnimator.setRepeatMode(ObjectAnimator.RESTART);
    }

    public void playImage(){
        if (state == STATE_STOP){
            objectAnimator.start();
            state = STATE_PLAYING;
        }
        else if (state == STATE_PAUSE){
            objectAnimator.resume();
            state = STATE_PLAYING;
        }
        else if (state == STATE_PLAYING){
            objectAnimator.pause();
            state = STATE_PAUSE;
        }
    }

    public void stopImage(){
        objectAnimator.end();
        state = STATE_STOP;
    }
}

MainActivity.java

package com.example.kw.rotatetest;

import android.os.Handler;
import android.support.v4.widget.ImageViewCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatImageView;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final ImagePlayButton imagePlayButton = (ImagePlayButton) findViewById(R.id.cross);
        final Button bt1 = findViewById(R.id.bt1);
        Button bt2 = findViewById(R.id.bt2);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (imagePlayButton.state == ImagePlayButton.STATE_PLAYING){
                    bt1.setText("继续");
                } else if (imagePlayButton.state == ImagePlayButton.STATE_PAUSE){
                    bt1.setText("暂停");
                } else if (imagePlayButton.state == ImagePlayButton.STATE_STOP) {
                    bt1.setText("暂停");
                }

                imagePlayButton.playImage();
            }
        });
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imagePlayButton.stopImage();
                bt1.setText("开始");
            }
        });


    }
}

xml



    

    

你可能感兴趣的:(安卓)