我使用处理程序做了animation
,第一次它完全按照我想要的方式工作但在暂停和恢复animation
后,它会挂起并且不会执行animation
。
处理程序代码:
public class AnimationWalrus {
private static ImageView animImage;
boolean pauseAnim = false;
static int timeCounter = 0;
int timeToRefresh = 180;
static int animImageCount = 9;
public AnimationWalrus(ImageView animImage){
pauseAnim = false;
timeCounter = 0;
AnimationWalrus.animImage = animImage;
}
Handler mHandlerToAnimate = new Handler()
{
public void handleMessage(Message msg) {
super.handleMessage(msg);
if(msg.what == 1){
switch (timeCounter) {
case 0:
animImage.setImageResource(R.drawable.walrus1);
break;
case 1:
animImage.setImageResource(R.drawable.walrus2);
break;
case 2:
animImage.setImageResource(R.drawable.walrus3);
break;
case 3:
animImage.setImageResource(R.drawable.walrus1);
break;
case 4:
animImage.setImageResource(R.drawable.walrus2);
break;
case 5:
animImage.setImageResource(R.drawable.walrus3);
break;
case 6:
animImage.setImageResource(R.drawable.walrus1);
break;
case 7:
animImage.setImageResource(R.drawable.walrus2);
break;
case 8:
animImage.setImageResource(R.drawable.walrus3);
break;
default:
animImage.setImageResource(R.drawable.walrus1);
//resetAnimation();
break;
}
if(timeCounter <= (animImageCount-1)){
timeCounter++;
}
}
};
};
private Runnable runnableToAnimate = new Runnable() {
public void run() {
Message msg = new Message();
msg.what = 1;
mHandlerToAnimate.handleMessage(msg);
if(!pauseAnim){
mHandlerToAnimate.postDelayed(this, timeToRefresh);
}
}
};
public void startWalrusAnimation(){
mHandlerToAnimate.post(runnableToAnimate);
}
public void stopWalrusAnimation(){
System.out.println("In Stop..........");
pauseAnim = true;
if (mHandlerToAnimate != null) {
mHandlerToAnimate.removeCallbacks(runnableToAnimate);
mHandlerToAnimate.removeCallbacksAndMessages(null);
}
}
public void resetAnimation()
{
System.out.println("In reset..........");
stopWalrusAnimation();
pauseAnim = false;
// timeCounter = 0;
//startWalrusAnimation();
}
public boolean isRunning(){
return !pauseAnim;
}
我正在使用这样的代码,最初animation
工作正常但在恢复后它会挂起并且不会执行animation
。
请建议我如何解决这个问题。
//Activity where i actually calling handler
imageWalrus = (ImageView)findViewById(R.id.imageViewWalrus);
imageViewWalrus = (ImageView)findViewById(R.id.imageViewWalrus);
//animWalrus = new AnimationWalrus(imageViewWalrus);
//animWalrus.startWalrusAnimation();
imageViewWalrus.setVisibility(View.INVISIBLE);
imageViewWalrus.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent event) {
// TODO Auto-generated method stub
//Creating object of animation walrus class
animWalrus = new AnimationWalrus(imageViewWalrus);
if (event.getAction()==MotionEvent.ACTION_DOWN) {
int id=arg0.getId();
if (id==R.id.imageViewWalrus) {
if (!animWalrus.isRunning())
{
//Calling Handler
animWalrus.startWalrusAnimation();
animWalrus.resetAnimation();
}
}
mainPlayer(R.raw.walrussb);
}
return false;
}
});
答案 0 :(得分:0)
您应该将代码从onCreate()
移到onResume()
,您的观点可能会被破坏,但这会让您遇到问题。如果没有看到所有的活动代码,我怎么能说不出来。但这是我最好的猜测。