Android:循环播放图像

时间:2011-01-13 05:39:42

标签: java android

我试图修改代码示例没有成功:

http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html

因此,当单击图像一次时,它将循环旋转图像。 (第二次点击应暂停)。

我尝试使用Handler和线程但无法更新视图,因为只有主线程可以更新UI。

例外我从下面的代码中得到:

android.view.ViewRoot $ CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能触及其视图。

[在'image1.startAnimation(rotation);' ('applyRotation(0,90);'来自主线程)]

package com.example.flip3d;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.widget.ImageView;

public class Flip3d extends Activity {


private ImageView image1;
private ImageView image2;

private boolean isFirstImage = true;


/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

image1 = (ImageView) findViewById(R.id.image01);
image2 = (ImageView) findViewById(R.id.image02);
image2.setVisibility(View.GONE);

image1.setOnClickListener(new View.OnClickListener() {
   public void onClick(View view) {
    if (isFirstImage) {       
     applyRotation(0, 90);
     isFirstImage = !isFirstImage;

    } else {    
     applyRotation(0, -90);
     isFirstImage = !isFirstImage;
    }
   }
});          
}

private void applyRotation(float start, float end) {
// Find the center of image
final float centerX = image1.getWidth() / 2.0f;
final float centerY = image1.getHeight() / 2.0f;

// Create a new 3D rotation with the supplied parameter
// The animation listener is used to trigger the next animation
final Flip3dAnimation rotation =
       new Flip3dAnimation(start, end, centerX, centerY);
rotation.setDuration(500);
rotation.setFillAfter(true);
rotation.setInterpolator(new AccelerateInterpolator());
rotation.setAnimationListener(new DisplayNextView(isFirstImage, image1, image2));

if (isFirstImage)
{
image1.startAnimation(rotation);
} else {
image2.startAnimation(rotation);
}

}
}

如何在onClick侦听器中设置更新UI并控制旋转?

谢谢,

Oakist

1 个答案:

答案 0 :(得分:0)

尝试在runOnUIThread()

中包装UI代码