Java Android-在更改ImageView时连续循环N帧动画
我想创建一个简单的“3-2-1”倒计时,使用三个在屏幕上旋转和缩放的动画图像视图,由一个按钮触发。我对单个ImageView的动画没有问题,但我无法连续执行3。。。然后2。。。然后是1帧。有人能帮忙吗? 以下是MainActivity的简化布局XMLJava Android-在更改ImageView时连续循环N帧动画,java,android,animation,imageswitcher,Java,Android,Animation,Imageswitcher,我想创建一个简单的“3-2-1”倒计时,使用三个在屏幕上旋转和缩放的动画图像视图,由一个按钮触发。我对单个ImageView的动画没有问题,但我无法连续执行3。。。然后2。。。然后是1帧。有人能帮忙吗? 以下是MainActivity的简化布局XML <Button android:id="@+id/go_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layo
<Button
android:id="@+id/go_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/rotate"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="120dp"
android:text="GO!" />
<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="220dp" />
好的。。。所以我把它分类了。我用了一个安卓倒计时
private void initialiseCountdownTimer(long timerDuration, long timerInterval){
countDownTimer = new CountDownTimer(timerDuration, timerInterval) {
@Override
public void onTick(long millisUntilFinished) {
if(!countdownImageStack.empty()){
countdownImage.setImageResource(countdownImageStack.pop());
countdownImage.startAnimation(countdownAnimationSet);
}
}
我使用了一个包含图像引用的堆栈(用作动画中的关键帧)。每次勾选时,我都会弹出堆栈来切换图像文件。我通过onClick()方法启动了倒计时
结果如何?没有令人讨厌的并发问题-但我仍然使用动画集,以便同时旋转和缩放 好的,这是一个相当复杂的方法,对于一些看起来相当简单的东西。第一:没什么。为什么需要
图像切换器
?显示getCountdownAnimationSet()
的代码。
public class MainActivity extends ActionBarActivity {
private Button animateCountdown;
private ImageSwitcher myImageSwitcher;
private static final int[] IMAGES = { R.drawable.icon_3, R.drawable.icon_2, R.drawable.icon_1};
private int currentIMAGESIndex = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myImageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
myImageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
return imageView;
}
});
animateCountdown = (Button) findViewById(R.id.go_button);
animateCountdown.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
runCountdownAnimation(view);
}
});
}
private void runCountdownAnimation(View view){
//Using an animation set, to allow for concurrent animations.
AnimationSet animationSet = getCountdownAnimationSet();
Animation fadeOutAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_out);
myImageSwitcher.setInAnimation(animationSet);
myImageSwitcher.setOutAnimation(fadeOutAnimation);
while(currentIMAGESIndex < (IMAGES.length - 1)){
myImageSwitcher.setImageResource(IMAGES[currentIMAGESIndex]);
currentIMAGESIndex++;
myImageSwitcher.postDelayed(this, 1000);
}
private AnimationSet getCountdownAnimationSet(){
Animation rotateAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.rotate);
Animation fadeAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
Animation zoomAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.zoom);
AnimationSet animationSet = new AnimationSet(false); //false means don't share interpolators
animationSet.addAnimation(rotateAnimation);
animationSet.addAnimation(fadeAnimation);
animationSet.addAnimation(zoomAnimation);
return animationSet;
}
private void initialiseCountdownTimer(long timerDuration, long timerInterval){
countDownTimer = new CountDownTimer(timerDuration, timerInterval) {
@Override
public void onTick(long millisUntilFinished) {
if(!countdownImageStack.empty()){
countdownImage.setImageResource(countdownImageStack.pop());
countdownImage.startAnimation(countdownAnimationSet);
}
}