安卓旋转动画播放暂停

转自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

<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>
发布了104 篇原创文章 · 获赞 24 · 访问量 4万+
展开阅读全文

安卓动画如何停止?clearAnimation无效

10-10

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); img1 = (ImageView) findViewById(R.id.point1); img2 = (ImageView) findViewById(R.id.point2); img3 = (ImageView) findViewById(R.id.point3); img4 = (ImageView) findViewById(R.id.point4); img5 = (ImageView) findViewById(R.id.point5); img6 = (ImageView) findViewById(R.id.point6); img7 = (ImageView) findViewById(R.id.point7); mTextView = (TextView) findViewById(R.id.loading); textView = (TextView) findViewById(R.id.stop); final ObjectAnimator img1xstart = ObjectAnimator.ofFloat(img1, "scaleX", 1.0f, 1.5f); final ObjectAnimator img1ystart = ObjectAnimator.ofFloat(img1, "scaleY", 1.0f, 1.5f); final ObjectAnimator img1xend = ObjectAnimator.ofFloat(img1, "scaleX", 1.5f, 1.0f); final ObjectAnimator img1yend = ObjectAnimator.ofFloat(img1, "scaleY", 1.5f, 1.0f); final ObjectAnimator img2xstart = ObjectAnimator.ofFloat(img2, "scaleX", 1.0f, 1.5f); final ObjectAnimator img2ystart = ObjectAnimator.ofFloat(img2, "scaleY", 1.0f, 1.5f); final ObjectAnimator img2xend = ObjectAnimator.ofFloat(img2, "scaleX", 1.5f, 1.0f); final ObjectAnimator img2yend = ObjectAnimator.ofFloat(img2, "scaleY", 1.5f, 1.0f); final ObjectAnimator img3xstart = ObjectAnimator.ofFloat(img3, "scaleX", 1.0f, 1.5f); final ObjectAnimator img3ystart = ObjectAnimator.ofFloat(img3, "scaleY", 1.0f, 1.5f); final ObjectAnimator img3xend = ObjectAnimator.ofFloat(img3, "scaleX", 1.5f, 1.0f); final ObjectAnimator img3yend = ObjectAnimator.ofFloat(img3, "scaleY", 1.5f, 1.0f); mTextView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { animSet = new AnimatorSet(); animSet.play(img1xstart).with(img1ystart); animSet.setDuration(500); animSet.start(); animSet.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { animSet2 = new AnimatorSet(); animSet2.play(img2xstart).with(img2ystart); animSet2.play(img1xend).with(img1yend); animSet2.setDuration(500); animSet2.start(); animSet2.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { animSet3 = new AnimatorSet(); animSet3.play(img3xstart).with(img3ystart); animSet3.play(img2xend).with(img2yend); animSet3.setDuration(500); animSet3.start(); animSet3.addListener(new AnimatorListenerAdapter() { @Override public void onAnimationEnd(Animator animation) { animSet.start(); animSet4 = new AnimatorSet(); animSet4.play(img3xend).with(img3yend); animSet4.setDuration(500); animSet4.start(); } }); } }); } }); } }); textView.setOnClickListener(new View.OnClickListener() {// @TargetApi(Build.VERSION_CODES.KITKAT) @Override public void onClick(View v) { /* animSet.pause(); animSet2.pause(); animSet3.pause(); animSet4.pause();*/ /* animSet.end(); animSet2.end(); animSet3.end();*/ // animSet4.end(); // img1.clearAnimation(); img2.clearAnimation(); img3.clearAnimation(); /* animSet.cancel(); animSet2.cancel(); animSet3.cancel(); animSet4.cancel();*/ } }); } ``` ``` 这是自己实验的小圆点循环加载的程序 是很麻烦 也还在学习中。。想请教一下这里我调用什么方法才能吧这个动画循环停止掉啊?求方法。。 自己试了很多都不行 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览