2014. 1. 11. 10:19 안드로이드

 translateAnimation후 click이벤트 먹게 하는 방법


package com.paran.animation.demo.app.animation;


import android.app.Activity;

import android.content.Context;

import android.graphics.Rect;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.view.View.OnClickListener;

import android.view.animation.Animation;

import android.view.animation.Animation.AnimationListener;

import android.view.animation.AnimationSet;

import android.view.animation.AnimationUtils;

import android.view.animation.RotateAnimation;

import android.view.animation.TranslateAnimation;

import android.widget.Button;

import android.widget.FrameLayout;

import android.widget.FrameLayout.LayoutParams;

import android.widget.Toast;


public class ShotButton extends Activity

{


FrameLayout Root;

Button ShotBtn, BulletBtn[];



@Override

protected void onCreate(Bundle savedInstanceState)

{

// TODO Auto-generated method stub

super.onCreate(savedInstanceState);

Root = new FrameLayout(this);


BulletBtn = new Button[5];

for (int i = 0; i < 5; i++)

{

BulletBtn[i] = new Button(this);

BulletBtn[i].setText("" + i);

final int index = i;

BulletBtn[i].setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

// TODO Auto-generated method stub

Toast.makeText(ShotButton.this, BulletBtn[index].getText(), Toast.LENGTH_SHORT).show();

}

});

Root.addView(BulletBtn[i], new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,

LayoutParams.WRAP_CONTENT));

}


ShotBtn = new Button(this);

ShotBtn.setText("START");

ShotBtn.setOnClickListener(new OnClickListener()

{

@Override

public void onClick(View v)

{

// TODO Auto-generated method stub

for (int i = 0; i < 5; i++)

{

final int index = i;

final int finalx = 100 * i + 50, finaly = 500;

AnimationSet animation = new AnimationSet(false);

RotateAnimation AniRotation = new RotateAnimation(0, 720);

AniRotation.setDuration(400);

TranslateAnimation AniTrans = new TranslateAnimation(0, finalx, 0, finaly);

AniTrans.setDuration(400);

AniTrans.setInterpolator(AnimationUtils.loadInterpolator(ShotButton.this,

android.R.anim.overshoot_interpolator));

AniTrans.setStartOffset(20 * i);


// animation.addAnimation(AniRotation);

animation.addAnimation(AniTrans);

animation.setFillAfter(true);//이거 하면 위치에 가던데 아래의 리스너로 setmargins하면 더 아래로 내려 가버리더라

// animation.setFillEnabled(true);


BulletBtn[i].startAnimation(animation);


animation.setAnimationListener(new AnimationListener()

{

@Override

public void onAnimationStart(Animation animation)

{

// TODO Auto-generated method stub

//처음의 버튼 위치 

FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(BulletBtn[index]

.getLayoutParams());

lp.setMargins(0, 0, 0, 0);

BulletBtn[index].setLayoutParams(lp);

BulletBtn[index].requestLayout();

fAniEnd = false;

}


@Override

public void onAnimationRepeat(Animation animation)

{

// TODO Auto-generated method stub


}


@Override

public void onAnimationEnd(Animation animation)

{

// TODO Auto-generated method stub

// if (animation.equals(BulletBtn[index].getAnimation()))

{

//애니가 끝났을때 버튼의 위치 

// FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(BulletBtn[index]

// .getLayoutParams());

// Log.i("aa","Margin "+lp.leftMargin+" "+lp.rightMargin+ " "+lp.topMargin+" "+lp.bottomMargin);

// lp.setMargins(finalx, finaly, 0, 0);

// BulletBtn[index].setLayoutParams(lp);

// BulletBtn[index].requestLayout();


BulletBtn[index].postDelayed(new Runnable()

{

@Override

public void run()

{

// TODO Auto-generated method stub



FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(BulletBtn[index]

.getLayoutParams());


lp.setMargins(finalx, finaly, 0, 0);

BulletBtn[index].setLayoutParams(lp);

BulletBtn[index].requestLayout();


BulletBtn[index].clearAnimation();

}

}, 0);

}


}

});

}

}

});

Root.addView(ShotBtn, new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));


setContentView(Root);

}


boolean fAniEnd = false;

}



posted by 욱이다