2012-01-26 35 views
1

我正試圖做一個應用程序,用戶可以移動圖像...而當用戶將其移動到某個地方時,應用程序應該做一些其他的事情...有點解鎖...ACTION_UP不起作用

但問題是,當ACTION_UP語句不起作用...任何建議?

這裏是我的代碼:

public class Main extends Activity { 

    private View selected_item = null; 
    private int offset_x = 0; 
    private int offset_y = 0; 

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

     ViewGroup vg = (ViewGroup) findViewById(R.id.lout); 
     vg.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      @SuppressWarnings("deprecation") 
      public boolean onTouch(View v, MotionEvent event) { 

          switch (event.getActionMasked()) { 
       case MotionEvent.ACTION_MOVE: 
        int x = (int) event.getX() - offset_x; 
        int y = (int) event.getY() - offset_y; 
        int w = getWindowManager().getDefaultDisplay().getWidth() - 100; 
        int h = getWindowManager().getDefaultDisplay().getHeight() - 100; 
        if (x > w) 
         x = w; 
        if (y > h) 
         y = h; 
        AbsoluteLayout.LayoutParams lp = new AbsoluteLayout.LayoutParams(
          new ViewGroup.MarginLayoutParams(
            AbsoluteLayout.LayoutParams.WRAP_CONTENT, 
            AbsoluteLayout.LayoutParams.WRAP_CONTENT)); 
        lp.x = x; 
        lp.y = y; 
        selected_item.setLayoutParams(lp); 
        break; 
       default: 
        break; 
       } 
       return true; 
      } 
     }); 

     ImageView img = (ImageView) findViewById(R.id.imageView1); 
     img.setOnTouchListener(new View.OnTouchListener() { 

      @Override 
      public boolean onTouch(View v, MotionEvent event) { 
       switch (event.getActionMasked()) { 
       case MotionEvent.ACTION_DOWN: 
        offset_x = (int) event.getX(); 
        offset_y = (int) event.getY(); 
        selected_item = v; 
        break; 
       case MotionEvent.ACTION_UP: 
        if (offset_x < 150 && offset_x > 120 && offset_y < 230 
          && offset_y > 200) 
         startActivity(new Intent(
           "com.yahya.GeneralTraining.UNLOCKED")); 
        break; 
       default: 
        break; 
       } 
       return false; 

      } 
     }); 

    } 
} 
+0

當你鬆開手指時,他仍然在ImageView上? –

+0

我怎麼知道它? – yahya

回答

1

似乎您的實施並不完全符合您的要求,您應該在ACTION_UP而不是ACTION_DOWN內指定offset_xoffset_y值。現在,它會像用戶開始從x(120到150)和y(200到230)拖動到任何地方一樣拖放。解鎖意圖會觸發,但是你想要的是從任何地方拖拽到x(120到150)和y(200到230)。

所以寫:

@Override 
public boolean onTouch(View v, MotionEvent event) { 
    //Are you sure you want event.getActionMasked() ?? 
    //I use event.getAction() 
    switch (event.getAction()) { 
     case MotionEvent.ACTION_DOWN: 
     //I don't believe you really need anything to do here 
     break; 
     case MotionEvent.ACTION_UP: 
      offset_x = (int) event.getX(); 
      offset_y = (int) event.getY(); 
      selected_item = v; 
      if (offset_x < 150 && offset_x > 120 && offset_y < 230 && offset_y > 200) 
       startActivity(new Intent("com.yahya.GeneralTraining.UNLOCKED")); 
     break; 
     default: 
     break; 
    } 
    return false; 
} 
}); 
+0

像這樣,它甚至沒有移動:/所以我想它應該在ACTION_DOWN ...但我也嘗試將它們放入ACTION_UP .. 。仍然沒有工作:/ – yahya

+1

你確定你想要event.getActionMasked()? 我使用使用event.getAction()......檢查編輯的代碼 –

+0

我只是想用自己的方式,但它給在這一行「NullPointerException異常」錯誤:\t \t \t \t \t selected_item.setLayoutParams(LP); – yahya

7

我認爲你需要在你的ImageView onTouch()回調返回true。出於某種原因在ACTION_DOWN之後返回錯誤會導致您無法通過ACTION_UP接到另一個電話。

+0

當我這樣做「真」,圖像甚至沒有移動... – yahya

2

您onTouch函數必須返回true。這可以避免其他監聽者控制事件並弄亂應用程序。