1

onTouchlistner 不适用于翻译动画图像视图。在动画过程中实际视图没有移动(只有图像的副本被动画)。请让我知道如何将触摸事件添加到动画图像视图。请在下面找到相同的代码。

                **activity class**


  public class AnimationAndroid extends Activity implements OnTouchListener {
                /** Called when the activity is first created. */
                ImageView viewOne; /* image view */
                Animation aOne;
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                    viewOne = (ImageView) findViewById(R.id.viewone); /* to get image view */
                    aOne = AnimationUtils.loadAnimation(this, R.anim.animatextone);   /* initializing the animation from xml file */
                    viewOne.setOnTouchListener(this); /* onTouch listner on imageView */
                    viewOne.startAnimation(aOne); /* to start animation */
                }

                public boolean onTouch(View v, MotionEvent event) { 
/* onTouch event function*/
                      Toast.makeText(AnimationAndroid.this, "Image One",Toast.LENGTH_SHORT).show(); /* to display the toast message on touch */
                }
         }


                **Animation xml file code** 




 <?xml version="1.0" encoding="utf-8"?>
            <set xmlns:android="http://schemas.android.com/apk/res/android"
                android:shareInterpolator="false">
               <translate
                android:fromXDelta="20%p"
                android:fromYDelta="20%p"
                android:duration="28000"
                android:repeatMode="reverse"
                android:repeatCount="infinite"
                android:toXDelta="28%p"
                android:toYDelta="18%p" 
                android:fillAfter="false"   
                />
            </set>

        Please let me know how i can resolve the issue. I need to have infinite
        animation.still onClick on the animated image is not working.

        **animation using nineoldandroid library**

       Implementation using nineoldandroid library but still not able to succeed.
       The click on animated moving image is not triggering the event.
       Please let me if am going wrong in implementing the nanooldandroid library   



public class NineOldTesting extends Activity implements OnClickListener{
       /* Called when the activity is first created. */
       final int duration = 2 * 15000;  /* duration of animation */
       @Override
       public void onCreate(Bundle savedInstanceState) {
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.main);
           ImageView tv = (ImageView) findViewById(R.id.testView); /* imageview id*/
           AnimatorSet set = new AnimatorSet();   /* to do animation together */
           set.playTogether(
               ObjectAnimator.ofFloat(tv, "translationX", 0, 90),
               ObjectAnimator.ofFloat(tv, "translationY", 0, 90)
           ); /* to move image in x and y direction */
           set.setDuration(duration);  /*  to set duration of animation */
           set.start(); /* start animation */
           tv.setOnClickListener(this);  /* setting onclick listner for image view*/
       }
       @Override
       public void onClick(View v) {
           /* TODO Auto-generated method stub */
           Toast.makeText(getApplicationContext(), "hiiiiii", Toast.LENGTH_SHORT).show(); /* to display toast message on click*/
       }
   }
4

0 回答 0