2014-03-06 158 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監聽器,而不會干擾移動。

相關問題