2012-11-24 36 views
1


我想創建一個簡單的拼字遊戲般的棋盤遊戲。我已經使用RelativeLayout創建了一個XML文件,其中包含我的主板的所有字段。現在我想要完成的是讓這塊電路板可移動 - 這樣用戶可以放大,縮小和移動屏幕。爲了做到這一點,我使用了HERE的提示。我用我自己的簡單BoardView替換TextView:android移動查看包含RelativeLayout

public class BoardView extends View { 

     public BoardView(Context context) { 
      super(context); 
      setContentView(R.layout.board); 
     } 

    } 

不幸的是,它不工作...我的董事會不動。 請幫我一把。 也許你有完全不同的方法來這種遊戲?我很樂意聽到它。

我BoardActivity代碼:

public class BoardActivity extends Activity implements OnTouchListener { 

private int deltaX_; 
private int deltaY_; 

private ViewGroup root_; 
private BoardView view_; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_board); 

    root_ = (ViewGroup) findViewById(R.id.rootBoard); 

    view_ = new BoardView(this); 

    RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(540, 540); 
    layoutParams.leftMargin = 50; 
    layoutParams.topMargin = 50; 
    layoutParams.bottomMargin = -2500; 
    layoutParams.rightMargin = -2500; 
    view_.setLayoutParams(layoutParams); 

    view_.setOnTouchListener(this); 
    root_.addView(view_); 
} 

public boolean onTouch(View v, MotionEvent event) { 

     final int X = (int) event.getRawX(); 
     final int Y = (int) event.getRawY(); 
     switch (event.getAction() & MotionEvent.ACTION_MASK) { 
     case MotionEvent.ACTION_DOWN: 
      RelativeLayout.LayoutParams lParams = (RelativeLayout.LayoutParams) v.getLayoutParams(); 
      deltaX_ = X - lParams.leftMargin; 
      deltaY_ = Y - lParams.topMargin; 
      break; 
     case MotionEvent.ACTION_UP: 
      break; 
     case MotionEvent.ACTION_POINTER_DOWN: 
      break; 
     case MotionEvent.ACTION_POINTER_UP: 
      break; 
     case MotionEvent.ACTION_MOVE: 
      RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams(); 
      layoutParams.leftMargin = X - deltaX_; 
      layoutParams.topMargin = Y - deltaY_; 
      layoutParams.rightMargin = -2500; 
      layoutParams.bottomMargin = -2500; 
      view_.setLayoutParams(layoutParams); 
      break; 
     } 
     return true; 
    } 
} 

我board.xml佈局類同THIS

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <ImageView 
    android:background="@drawable/special_square" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/square_a1" /> 
    <ImageView 
    android:layout_toRightOf="@id/square_a1" 
    android:background="@drawable/normal" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:id="@+id/square_a2" /> 
    ...and so on... 
+0

是不是*的*所有的BoardView代碼? – Simon

+0

在這個階段 - 是的。我只是想讓它顯示出來,然後移動。然後我會專注於其他行動。 – karex

+0

設置layoutParams後,嘗試view_.invalidate()。 – Simon

回答

1
case MotionEvent.ACTION_MOVE: 
     RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) v.getLayoutParams(); 
     layoutParams.leftMargin = X - deltaX_; 
     layoutParams.topMargin = Y - deltaY_; 
     layoutParams.rightMargin = -2500; 
     layoutParams.bottomMargin = -2500; 
     //no needs to setLayout again: view_.setLayoutParams(layoutParams); 

     //requestLayout is required 
     view_.requestLayout();