我怎样才能停止这个动画?
How can I STOP this animation?
我创建了一个在单击按钮时开始的彩色背景动画(它一遍又一遍地平滑地改变背景颜色)。 我的问题是我无法停止动画(如果我再次单击该按钮会加快动画速度)。我已经尝试创建一个 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);
}
});
(再次)查看您的代码,您的 removeCallbacks
方法正在传递您的 Runnable
的实例,该实例尚未成为 运行。
您需要将 Runnable
的实例存储在点击监听器之外。我刚刚举了一个简单的例子来展示我的想法:
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>
我创建了一个在单击按钮时开始的彩色背景动画(它一遍又一遍地平滑地改变背景颜色)。 我的问题是我无法停止动画(如果我再次单击该按钮会加快动画速度)。我已经尝试创建一个 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);
}
});
(再次)查看您的代码,您的 removeCallbacks
方法正在传递您的 Runnable
的实例,该实例尚未成为 运行。
您需要将 Runnable
的实例存储在点击监听器之外。我刚刚举了一个简单的例子来展示我的想法:
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>