android - How to set onclick listener for animated image view -


i developing application has animated image view moving right center.when click on image onclick() called. when click on screen in image moving path(near image view) onclick() fired. please tell me how set on click listener image view. code is:

  ll = new linearlayout(this);             ll.setlayoutparams(new layoutparams(layoutparams.wrap_content, layoutparams.wrap_content));             ll.setorientation(linearlayout.vertical);              ll.setgravity(gravity.center);             imageview=new imageview(getapplicationcontext());             imageview.setimageresource(r.drawable.moveimage1);     int width=getwindowmanager().getdefaultdisplay().getwidth()/2;     system.out.println("width==="+width);             movelefttoright = new translateanimation(width, 0, 0, 0);             movelefttoright.setduration(3000);             movelefttoright.setrepeatcount(translateanimation.infinite);  // animation repeat count             movelefttoright.setrepeatmode(2);                imageview.setonclicklistener(new onclicklistener() {                  @override                 public void onclick(view v) {                     toast.maketext(getapplicationcontext(), "clicked", toast.length_long).show();                     system.out.println("clicked");                 }             });  imageview.startanimation(movelefttoright);  ll.addview(imageview);          setcontentview(ll); 

you attach onclick listener once animation has finished. more solid solution make worker thread handles computations need done animation , update actual drawing on main thread. e.g.:

scheduledexecutorservice executor = executors         .newscheduledthreadpool(1);  // execute run() in worker thread every refresh_rate // milliseconds mmoverfuture = executor.schedulewithfixeddelay(new runnable() {     @override     public void run() {         // todo - implement movement logic.         if (movewhileonscreen()) {             stop(false);         }         else {             postinvalidate();         }      } }, 0, refresh_rate, timeunit.milliseconds); 

that way can attach onclick listener without interfering movement.


Comments

Popular posts from this blog

c# - How to get the current UAC mode -

postgresql - Lazarus + Postgres: incomplete startup packet -

javascript - Ajax jqXHR.status==0 fix error -