2014-03-06 161 views
4

我正在开发一个应用程序,它具有从右向中心移动的动画图像视图。当单击图像时,将调用OnClick()。但是,当我点击图像移动路径(接近图像视图)屏幕上,然后也OnClick()发射。请告诉我如何设置点击侦听器只为图像视图。 我的代码是:如何为动画图像视图设置onclick监听器

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); 
+0

你想要onClick()在图像视图动画或动画完成后触发,这意味着什么 – San

+0

L1 linearLayout中的imageview是什么? – Amrut

+0

我想单击时触发onClick()。 –

回答

0

一旦动画已完成,您将附加onclick监听器。 要获得更加可靠的解决方案,请创建一个工作线程来处理需要为动画完成的所有计算,并仅更新主线程上的实际绘图。例如:

ScheduledExecutorService executor = Executors 
     .newScheduledThreadPool(1); 

// Execute the 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); 

通过这种方式,您可以附加onclick监听器,而不会干扰移动。

相关问题