1


I want to create a simple scrabble-like board game. I've already created an XML file with RelativeLayout containing all fields of my board. Now what I want to accomplish is to have this board movable - so that user can zoom it in, zoom out and move over the screen. To make this happen I used tips from HERE. I replaced TextView with my own simple BoardView:

public class BoardView extends View {

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

    }

Unfortunately it doesn't work... My Board doesn't move. Please help me with it. Maybe you have completely different approach to this kind of game? I'd love to hear about it.

My BoardActivity code:

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;
    }
}

My board.xml layout is similiar to 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...
Community
  • 1
  • 1
karex
  • 221
  • 1
  • 6
  • 14
  • Is that *all* of your BoardView code? – Simon Nov 24 '12 at 14:15
  • At this stage - yes. I just want it to be shown, and move. Then I will focus on other actions. – karex Nov 24 '12 at 14:22
  • Try view_.invalidate() after setting layoutParams. – Simon Nov 24 '12 at 14:30
  • It didn't help. I've added code of BoardActivity, as you can see. Problem is that this board shows up in top left corner of screen (although it has margins set initially to 50, 50) and doesn't move. – karex Nov 24 '12 at 14:32
  • When I changed "setContentView(R.layout.board);" in BoardView to "inflate(context, R.layout.board, root_);" and added Toast to tell me if Touch Event was consumed it showed up that BoardView has some margin. Clicking in top left corner doesn't make toast appear. While click in bottom right corner shows it. But my images still are aligned to the top left side of screen. – karex Nov 24 '12 at 14:57

1 Answers1

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();
19 Lee
  • 108
  • 6