安卓旋转动画播放暂停

本文介绍如何使用Android的属性动画ObjectAnimator实现按钮的旋转动画效果。通过自定义ImagePlayButton控件,结合ObjectAnimator实现旋转动画,支持播放、暂停和停止状态切换。代码示例包括ImagePlayButton和MainActivity的实现,以及XML布局文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

转自https://blog.youkuaiyun.com/lly347705530/article/details/78671696#commentBox
使用自安卓3.0开始支持的属性动画ObjectAnimator实现这个效果,从ImageView派生出来,调用ObjectAnimator的方法即可。
在这里插入图片描述
ImagePlayButton.java

/**
 * 属性动画 ObjectAnimator
 * https://blog.youkuaiyun.com/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

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <com.example.kw.rotatetest.ImagePlayButton
        android:id="@+id/cross"
        android:src="@drawable/cross"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />

    <Button
        android:id="@+id/bt1"
        android:text="开始"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />
    <Button
        android:id="@+id/bt2"
        android:text="停止"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        />


</LinearLayout>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值