2010-12-22 2 views
2

내 코드 : Android Animation 드로잉, 왜 항상 두 번 촬영 ​​했나요?

<?xml version="1.0" encoding="utf-8"?> 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="true"> 
    <item android:drawable="@drawable/bubble0" android:duration="500" /> 
    <item android:drawable="@drawable/explode1" android:duration="500" /> 
    <item android:drawable="@drawable/explode2" android:duration="500" /> 
    <item android:drawable="@drawable/explode3" android:duration="500" /> 
    <item android:drawable="@drawable/explode4" android:duration="500" /> 
    <item android:drawable="@drawable/explode5" android:duration="500" /> 
</animation-list> 


package net.tq5.bubbleexplosion; 

import android.app.Activity; 
import android.content.Context; 
import android.graphics.drawable.AnimationDrawable; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.view.View.OnTouchListener; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 

public class BubbleExplosionActivity extends Activity { 
public static final String TAG = "BubbleExplosionActivity"; 
/** Called when the activity is first created. */ 
private FrameLayout parent; 
private ExplosionView customView; 
private AnimationDrawable exal; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // set no title; 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    // Create a FrameLayout and set the background; 
    parent = new FrameLayout(this); 
    parent.setBackgroundResource(R.drawable.bg); 

    customView = new ExplosionView(this); 
    customView.setVisibility(View.INVISIBLE); 
    customView.setBackgroundResource(R.anim.explosion); 

    exal = (AnimationDrawable) customView.getBackground(); 

    parent.addView(customView); 
    parent.setOnTouchListener(new LayoutListener()); 
    setContentView(parent); 
} 

class ExplosionView extends ImageView { 
    public ExplosionView(Context context) { 
    super(context); 
    } 

    // handle the location of the Explosion; 
    public void setLocation(int left, int top) { 
    this.setFrame(left, top, left + 40, top + 40); 
    } 
} 

class LayoutListener implements OnTouchListener { 
    @Override 
    public boolean onTouch(View view, MotionEvent event) { 

    // first you stop the animation 
    // you can always restart it; 

    customView.setVisibility(View.INVISIBLE); 
    if (exal.isRunning()) 
    return false; 
// exal.stop(); 
    float x = event.getX(); 
    float y = event.getY(); 
    Log.i(TAG, "on Touch"); 
    customView.setLocation((int) x - 20, (int) y - 20); 
    customView.setVisibility(View.VISIBLE); 
    exal.start(); 
    return false; 
    } 

} 
} 

이미지는 항상 두 번 보여 bubble0;


public class BubbleExplosionActivity extends Activity { 
public static final String TAG = "BubbleExplosionActivity"; 
/** Called when the activity is first created. */ 
private FrameLayout parent; 
private ExplosionView customView; 
private AnimationDrawable exal; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    // set no title; 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
    WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    // Create a FrameLayout and set the background; 
    parent = new FrameLayout(this); 
    parent.setBackgroundResource(R.drawable.bg); 

    customView = new ExplosionView(this); 
    customView.setVisibility(View.INVISIBLE); 
    customView.setBackgroundResource(R.anim.explosion); 

    exal = (AnimationDrawable) customView.getBackground(); 

    parent.addView(customView); 
    parent.setOnTouchListener(new LayoutListener()); 
    setContentView(parent); 

    (new Handler()).postDelayed(new Runnable() { 

    @Override 
    public void run() { 
    customView.setLocation((int) 100 - 20, (int) 100 - 20); 
    customView.setVisibility(View.VISIBLE); 
    exal.start(); 
    } 

    }, 3000); 
} 
} 

:이 애니메이션은 두 번 해고,하지만 이벤트가 있는지 확인 시작() 메서드를 한 번만 트리거 될 수 있도록하기 위해 시도했지만 애니메이션이 여전히 두 번 수행됩니다 된 것 같다

Help!

답변

6

당신에게 조금 늦을 지 모르지만, 같은 문제가 생겼을 때이 질문을 보았습니다.

애니메이션을 두 번 실행하는 이유는보기의 가시성을 VISIBLE로 변경하면 애니메이션이 처음부터 다시 시작되기 때문입니다.

해결 방법은 애니메이션을 시작하거나보기 가시성을 View.VISIBLE로 변경하는 것입니다.

관련 문제