2014-02-19 4 views
1

보드에 사각형 타일 묶음을 만들고 있습니다. 나는 타일을 만지거나 이동하고 문제가있는 모션 이벤트를 구현하기 시작했습니다. 타일 ​​(ACTION_DOWN)을 터치하면 현재 뷰가 제거되고 다른 부모 뷰로 이동됩니다. 새 레이아웃을 설정했지만 레이아웃이 존중되지 않으면 타일이 새 뷰에서 0,0으로 이동합니다. ACTION_MOVE 이벤트에서 타일 위치를 정의하는 코드가 같고 예상대로 작동합니다. 그래서 내가 보는 것은 내가 타일을 만지면, 그것은 새로운 뷰에서 0,0으로 튀어 나옵니다. 그러면 제 손가락을 움직이면서 예상대로 나는 손가락으로 움직이고 움직입니다. 코드는 다음과 같습니다.초기 레이아웃을 고려하지 않고 RelativeLayout에 뷰 추가

My Tile 클래스가 View를 확장하고 보드 클래스 TileViewContainer가 RelativeLayout을 확장합니다. 나는 타일을 만들 때

나는과 같이 터치 이벤트를 등록 :

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

    final int X = (int) event.getRawX(); 
    final int Y = (int) event.getRawY(); 

    if(tileBoard == null) 
     tileBoard = (TileBoard) findViewById(R.id.tileBoard); 
    if(tileTray == null) 
     tileTray = (TileTray) findViewById(R.id.tileTray); 

    Tile tile = (Tile) v; 
    TileView parent = tile.lastParent; 
    Rect rect = null; 
    int size = tileBoard.mTileSize; 

    switch (event.getAction() & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 

      //Add tile to container 
      tile.removeParent(); 
      this.addView(tile); 
      rect = new Rect(X-(size/2), Y-(size), X-(size/2)+size, Y-(size)+size); 
      tile.layout(rect.left, rect.top, rect.right, rect.bottom); 
      break; 

     case MotionEvent.ACTION_UP: 
      print("action up"); 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      print("pointer down"); 
      break; 
     case MotionEvent.ACTION_POINTER_UP: 
      print("pointer up"); 
      break; 
     case MotionEvent.ACTION_MOVE: 

      //Move tile 
      rect = new Rect(X-(size/2) , Y-(size) , X-(size/2)+size, Y-(size)+size); 
      tile.layout(rect.left, rect.top, rect.right, rect.bottom); 
      break; 
    } 

    this.invalidate(); 
    return true; 

} 

답변

1

나는이로 실행 다른 사람에 대한 해결책을 발견 :

TileViewContainer gameBoard = (TileViewContainer) findViewById(R.id.gameBoard); 
tile.setOnTouchListener(gameBoard); 

여기 내 OnTouchListener입니다. 이를 위해

//Add tile to container 
tile.removeParent(); 
this.addView(tile); 
rect = new Rect(X-(size/2), Y-(size), X-(size/2)+size, Y-(size)+size); 
tile.layout(rect.left, rect.top, rect.right, rect.bottom); 
break; 

: 나는에서 ACTION_DOWN 케이스를 변경

//Add tile to container 
tile.removeParent(); 
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(sizeScaled, sizeScaled); 
params.leftMargin = X-sizeScaled/2; 
params.topMargin = Y-sizeScaled; 
addView(tile, params); 
break; 

내가 초기 배치를위한 새로운의 LayoutParams을 만드는 데 필요한 것 같습니다. 이동 섹션에서 tile.layout을 설정해도 문제가 없습니다.

관련 문제