我怎么能停止这个动画?

时间:2017-02-16 00:25:04

标签: android animation android-animation imagebutton runnable

我创建了一个彩色背景动画,它从按钮点击开始(它一遍又一遍地平滑地改变背景颜色)。 我的问题是我无法停止动画(如果我再次点击该按钮会增加动画速度)。我已经尝试创建一个while循环但是动画再也没有了。我的目标是,如果按下图像Btn,动画就会启动,如果我再次按下它,它应该会停止。

代码:

imageButton_info.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v){

                // Handler and runnable to run the animation in devices sdk below honeycomb.
                final Handler handler = new Handler();

                Runnable ChangeBackgroundRunnable = new Runnable() {

                    @Override
                    public void run() {
                        number++;
                        float[] hsvColor = {0, 1, 1};
                        hsvColor[0] = 360f * number / 100;
                        color.setBackgroundColor(Color.HSVToColor(hsvColor));

                        handler.postDelayed(this, 80);
                        if (number >=100)
                            number = 1;
                    }
                };
                number = 0;
                handler.removeCallbacks(ChangeBackgroundRunnable);
                handler.postDelayed(ChangeBackgroundRunnable, 0);

        }
    });

1 个答案:

答案 0 :(得分:0)

再次查看您的代码,removeCallbacks方法正在传递尚未运行的Runnable实例。

您需要将Click Runnable的实例存储在Click Listener之外。我有一个快速的例子,它显示了我的想法:

public class MainActivity extends AppCompatActivity {

    TextView textView;
    Button startBtn;
    int counter = 0;
    boolean running = false;

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


        textView = (TextView) findViewById(R.id.tvCounter);
        startBtn = (Button) findViewById(R.id.startBtn);

        final Handler counterHandler = new Handler(Looper.getMainLooper()); //Just to be specific
        final Runnable myRunnable = new Runnable() {
            @Override
            public void run() {
                textView.setText(String.format("Count is %s", counter ++));
                counterHandler.postDelayed(this, 100);
            }
        };


        startBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(running) {
                    counterHandler.removeCallbacks(myRunnable);
                    running = false;
                } else {
                    counterHandler.post(myRunnable);
                    running = true;
                }


            }
        });

    }

}

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="example.com.test.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    android:id="@+id/tvCounter"/>

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
            android:id="@+id/startBtn" android:text="Start Stop Button"/>
</RelativeLayout>