2012-01-21 45 views
4

我正在做一個在Android中使用TranslateAnimation的項目。在這我成功地翻譯圖像,但我必須添加onClickListener的圖像。這意味着我在翻譯過程中點擊圖片並獲取它們的值,但是我在圖片上獲得了onClick操作。TranslateAnimation中的OnClickListener

我使用下面的代碼:

public class SampleGame extends Activity implements OnClickListener { 

    int x10, x20, y10, y20; 

    ImageView img; 

    Button animation; 
    Handler transHandler; 
    RelativeLayout layout; 
    Random rand; 
    int mSpeedX, mSpeedY; 
    int width, height; 
    Thread thread; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     layout = (RelativeLayout) findViewById(R.id.layout); 
     animation = (Button) findViewById(R.id.animation); 

     animation.setOnClickListener(this); 

     rand = new Random(); 

     width = getWindow().getWindowManager().getDefaultDisplay().getWidth() - 20; 
     height = getWindow().getWindowManager().getDefaultDisplay().getHeight() - 70; 

     x10 = width - rand.nextInt(width); 
     y10 = height; 

     x20 = width; 
     y20 = height - rand.nextInt(height); 

     thread = new Thread(); 

     img.setOnClickListener(this); 

    } 

    public void onPause() { 
     super.onPause(); 
     thread = null; 
    } 

    @Override 
    public void onClick(View v) { 

     if (v.equals(img)) { 
      Log.e("img clicked :", "yaaaaaaaaa"); 
     } 

     if (v.equals(animation)) { 
      animation.setVisibility(View.INVISIBLE); 
      callTransform(); 
     } 
    } 

    public void callTransform() { 
     if (thread != null) { 
      thread = new Thread() { 
       public void run() { 

// Log.e("X1 value  :",String.valueOf(x10));  
// Log.e("X2 value  :",String.valueOf(x20));  
// Log.e("Y1 value  :",String.valueOf(y10));  
// Log.e("Y2 value  :",String.valueOf(y20));  

        transformAnimation(x10, x20, y10, y20, img, 4000); 

        try { 
         Thread.sleep(4000); 
        } catch (Exception e) { 
        } 
// Message msg=transHandler.obtainMessage(); 
// transHandler.sendMessage(msg); 
        x10 = x20; 
        y10 = y20; 

        if (x10 == width) { 
         x20 = width - rand.nextInt(width); 
         y20 = 0; 
        } else if (y10 == 0) { 
         x20 = 0; 
         y20 = height - rand.nextInt(height); 
        } else if (x10 == 0) { 
         x20 = width - rand.nextInt(width); 
         y20 = height; 
        } else if (y10 == height) { 
         x20 = width; 
         y20 = height - rand.nextInt(height); 
        } 

        callTransform(); 
       } 
      }; 
      thread.start(); 
     } 
    } 


    public void transformAnimation(int xFrom, int xTo, int yFrom, int yTo, final ImageView image, int time) { 
     TranslateAnimation anim = new TranslateAnimation(xFrom, xTo, yFrom, yTo); 
     anim.setDuration(time); 
     image.setAnimation(anim); 
    } 

} 

,我嘗試下面的例子也 - 2D-Tutorial Example

但在這也我沒有onClick行動的圖像。

那麼如何在翻譯過程中對圖像進行onClick動作呢?

是否有任何其他方式在圖像上單擊動作翻譯?

在此先感謝。

回答

0

在動畫過程中,對象保持原位,但它爲動畫翻譯,因此您需要在動畫過程中手動更新佈局參數