Android 开始前序列动画冻结
Sequence animation freezing before start in Android
我有两个序列动画(xml 文件)。我想在第一个动画停止时开始第二个动画。这是我的源代码:
mFingerprintIcon.setImageResource(0);
mFingerprintIcon.setBackgroundResource(R.drawable.finger_print_first_animation);
final AnimationDrawable animFirst = (AnimationDrawable) mFingerprintIcon.getBackground();
mFingerprintStatus.setTextColor(ContextCompat.getColor(getActivity(), R.color.success_color));
mFingerprintStatus.setText(getResources().getString(R.string.fingerprint_success));
int iDuration = 0;
for (int i = 0; i < animFirst.getNumberOfFrames(); i++) {
iDuration += animFirst.getDuration(i);
}
animFirst.start();
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
animFirst.stop();
mFingerprintIcon.setBackgroundResource(R.drawable.finger_print_second_animation);
AnimationDrawable animSecond = (AnimationDrawable) mFingerprintIcon.getBackground();
animSecond.setOneShot(false);
animSecond.start();
}
}, iDuration);
此代码有效,但有一个问题。第二个动画停顿了几秒钟然后开始。
我怎样才能编写代码,使两个动画都在不冻结的情况下进行动画处理?
使用 AnimationListener :
public class YourClass extends Activity implements Animation.AnimationListener {
...
animFirst.setListener(this);
并在 onAnimationEnd 方法中开始你的第二个动画。
或者在匿名内部 class :
animFirst.setListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
animSecond.start();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
});
正确 Answer.I 自己测试
使用 Sequence 动画的最佳方法是创建动画 programmatically.for 这样的示例
animSecond = new AnimationDrawable();
animFirst = new AnimationDrawable();
animFirst.setOneShot(true);
animFirst.addFrame(
getResources().getDrawable(R.drawable.fingerprint_00001),
50);
animFirst.addFrame(
getResources().getDrawable(R.drawable.fingerprint_00002),
50);
int iDuration = 0;
for (int i = 0; i < animFirst.getNumberOfFrames(); i++) {
iDuration += animFirst.getDuration(i);
}
mFingerprintIcon.setImageDrawable(animFirst);
animFirst.start();
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
animFirst.stop();
if (animFadeOut != null) {
mFingerprintStatus.startAnimation(animFadeOut);
mFingerprintStatus.setVisibility(View.INVISIBLE);
}
mFingerprintIcon.setImageDrawable(animSecond);
animSecond.start();
}
}, iDuration);
我有两个序列动画(xml 文件)。我想在第一个动画停止时开始第二个动画。这是我的源代码:
mFingerprintIcon.setImageResource(0);
mFingerprintIcon.setBackgroundResource(R.drawable.finger_print_first_animation);
final AnimationDrawable animFirst = (AnimationDrawable) mFingerprintIcon.getBackground();
mFingerprintStatus.setTextColor(ContextCompat.getColor(getActivity(), R.color.success_color));
mFingerprintStatus.setText(getResources().getString(R.string.fingerprint_success));
int iDuration = 0;
for (int i = 0; i < animFirst.getNumberOfFrames(); i++) {
iDuration += animFirst.getDuration(i);
}
animFirst.start();
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
animFirst.stop();
mFingerprintIcon.setBackgroundResource(R.drawable.finger_print_second_animation);
AnimationDrawable animSecond = (AnimationDrawable) mFingerprintIcon.getBackground();
animSecond.setOneShot(false);
animSecond.start();
}
}, iDuration);
此代码有效,但有一个问题。第二个动画停顿了几秒钟然后开始。
我怎样才能编写代码,使两个动画都在不冻结的情况下进行动画处理?
使用 AnimationListener :
public class YourClass extends Activity implements Animation.AnimationListener {
...
animFirst.setListener(this);
并在 onAnimationEnd 方法中开始你的第二个动画。
或者在匿名内部 class :
animFirst.setListener(new AnimationListener() {
public void onAnimationEnd(Animation animation) {
animSecond.start();
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationStart(Animation animation) {
}
});
正确 Answer.I 自己测试 使用 Sequence 动画的最佳方法是创建动画 programmatically.for 这样的示例
animSecond = new AnimationDrawable();
animFirst = new AnimationDrawable();
animFirst.setOneShot(true);
animFirst.addFrame(
getResources().getDrawable(R.drawable.fingerprint_00001),
50);
animFirst.addFrame(
getResources().getDrawable(R.drawable.fingerprint_00002),
50);
int iDuration = 0;
for (int i = 0; i < animFirst.getNumberOfFrames(); i++) {
iDuration += animFirst.getDuration(i);
}
mFingerprintIcon.setImageDrawable(animFirst);
animFirst.start();
Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
animFirst.stop();
if (animFadeOut != null) {
mFingerprintStatus.startAnimation(animFadeOut);
mFingerprintStatus.setVisibility(View.INVISIBLE);
}
mFingerprintIcon.setImageDrawable(animSecond);
animSecond.start();
}
}, iDuration);