Advertisement
Guest User

Untitled

a guest
May 3rd, 2012
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.79 KB | None | 0 0
  1. Button is not clickable after TranslateAnimation
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <Button xmlns:android="http://schemas.android.com/apk/res/android"
  4. android:id="@+id/button"
  5. android:layout_width="wrap_content"
  6. android:layout_height="wrap_content"
  7. android:layout_gravity="center"
  8. android:text="Press to begin animation" />
  9.  
  10. public class TestActivity extends Activity {
  11. public final String TAG="TestActivity";
  12. boolean toTop=false;
  13. @Override
  14. public void onCreate(Bundle savedInstanceState) {
  15. super.onCreate(savedInstanceState);
  16. setContentView(R.layout.main);
  17. Button b=(Button)findViewById(R.id.button);
  18. b.setOnClickListener(new OnClickListener() {
  19.  
  20. public void onClick(View v) {
  21. Toast.makeText(TestActivity.this, "left="+v.getLeft()+"nright="+v.getRight(), Toast.LENGTH_SHORT).show();
  22.  
  23. Animation translateAnimation;
  24. if(toTop) translateAnimation=new TranslateAnimation(0, 0, 0, -100);
  25. else translateAnimation=new TranslateAnimation(0, 0, 0, 100);
  26. translateAnimation.setDuration(1000);
  27. translateAnimation.setFillEnabled(true);
  28. translateAnimation.setFillAfter(true);
  29.  
  30. v.startAnimation(translateAnimation);
  31. toTop=!toTop;
  32. }
  33. });
  34. }
  35. }
  36.  
  37. public class TestActivity extends Activity {
  38. public final String TAG="TestActivity";
  39. boolean toTop=false;
  40. @Override
  41. public void onCreate(Bundle savedInstanceState) {
  42. super.onCreate(savedInstanceState);
  43. setContentView(R.layout.main);
  44. Button b=(Button)findViewById(R.id.button);
  45. b.setOnClickListener(new OnClickListener() {
  46.  
  47. public void onClick(View v) {
  48. int modifierY;
  49. if(toTop) modifierY=-100;
  50. else modifierY=100;
  51. Animation translateAnimation=new TranslateAnimation(0, 0, 0, modifierY);
  52. translateAnimation.setDuration(1000);
  53. translateAnimation.setFillEnabled(true);
  54. MyAnimationListener listener=new MyAnimationListener(v, modifierY,TestActivity.this);
  55. translateAnimation.setAnimationListener(listener);
  56.  
  57. v.startAnimation(translateAnimation);
  58. toTop=!toTop;
  59. }
  60. });
  61. }
  62.  
  63. public class MyAnimationListener implements AnimationListener{
  64. View mView;
  65. int mModifier;
  66. Context mContext;
  67.  
  68. public MyAnimationListener(View v, int modifier, Context c){
  69. mView=v;
  70. mModifier=modifier;
  71. mContext=c;
  72. }
  73. public void onAnimationEnd(Animation animation) {
  74. int[] pos={mView.getLeft(),mView.getTop()+mModifier,mView.getRight(),mView.getBottom()+mModifier};
  75. mView.layout(pos[0],pos[1],pos[2],pos[3]);
  76. Toast.makeText(mContext, "left="+mView.getLeft()+"ntop="+mView.getTop(), Toast.LENGTH_SHORT).show();
  77. }
  78.  
  79. public void onAnimationRepeat(Animation animation) {}
  80.  
  81. public void onAnimationStart(Animation animation) {}
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement