如何将Android显示屏分成多个框?

时间:2016-04-26 05:26:43

标签: java android

我想开发一种棋盘游戏,其中游戏区域被划分为25个方格(5X5)当掷骰子时玩家应该能够仅在特定方格中移动硬币并且玩家不应该能够访问其他方块移动他的硬币。我设计了电路板,播放器能够移动硬币,但问题是,我无法将电路板分成正方形,我无法限制用户在掷骰子后访问其他部分电路板。我搜索过互联网上的每一个地方,但没有得到适当的解决方案。我是android的初学者所以请原谅我任何错误。

enter image description here

以下是代码:

主Java文件

private SensorManager mSensorManager;
private Sensor mSensor;
private long lastUpdate = 0;
private float last_x, last_y, last_z;
private static final int SHAKE_THRESHOLD = 600;
ImageView rolldie, yellow1, yellow2, yellow3, yellow4,black1,black2,black3,black4,blue1,blue2,blue3,blue4,white1,white2,white3,white4;
AnimationDrawable ad;
Random r = new Random();
int ranno;
private int _xDelta;
private int _yDelta;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    requestWindowFeature(Window.FEATURE_NO_TITLE);

    mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
    mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
    mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);

    setContentView(R.layout.activity_play_game);

    rolldie = (ImageView) findViewById(R.id.rolldie);
    rolldie.setImageResource(R.drawable.frame5);
    yellow1 = (ImageView) findViewById(R.id.yellow1);
    yellow2 = (ImageView) findViewById(R.id.yellow2);
    yellow3 = (ImageView) findViewById(R.id.yellow3);
    yellow4 = (ImageView) findViewById(R.id.yellow4);

    black1 = (ImageView) findViewById(R.id.black1);
    black2 = (ImageView) findViewById(R.id.black2);
    black3 = (ImageView) findViewById(R.id.black3);
    black4 = (ImageView) findViewById(R.id.black4);

    blue1 = (ImageView) findViewById(R.id.blue1);
    blue2 = (ImageView) findViewById(R.id.blue2);
    blue3 = (ImageView) findViewById(R.id.blue3);
    blue4 = (ImageView) findViewById(R.id.blue4);

    white1 = (ImageView) findViewById(R.id.white1);
    white2 = (ImageView) findViewById(R.id.white2);
    white3 = (ImageView) findViewById(R.id.white3);
    white4 = (ImageView) findViewById(R.id.white4);

    yellow1.setOnTouchListener(this);
    yellow2.setOnTouchListener(this);
    yellow3.setOnTouchListener(this);
    yellow4.setOnTouchListener(this);

    black1.setOnTouchListener(this);
    black2.setOnTouchListener(this);
    black3.setOnTouchListener(this);
    black4.setOnTouchListener(this);

    blue1.setOnTouchListener(this);
    blue2.setOnTouchListener(this);
    blue3.setOnTouchListener(this);
    blue4.setOnTouchListener(this);

    white1.setOnTouchListener(this);
    white2.setOnTouchListener(this);
    white3.setOnTouchListener(this);
    white4.setOnTouchListener(this);

}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

}

public void onSensorChanged(SensorEvent event) {
    Sensor mySensor = event.sensor;

    if (mySensor.getType() == Sensor.TYPE_ACCELEROMETER) {
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];

        long curTime = System.currentTimeMillis();

        if ((curTime - lastUpdate) > 100) {
            long diffTime = (curTime - lastUpdate);
            lastUpdate = curTime;

            float speed = Math.abs(x + y + z - last_x - last_y - last_z) / diffTime * 10000;

            if (speed > SHAKE_THRESHOLD) {
                rolldie.setImageDrawable(null);
                rolldie.setBackgroundResource(R.drawable.dieroll);
                ad = (AnimationDrawable) rolldie.getBackground();
                ad.start();

                final Handler handler = new Handler();
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        ad.stop();
                        rolldie.setImageDrawable(null);
                        ranno = r.nextInt(7 - 1) + 1;
                        switch (ranno) {
                            case 1:
                                rolldie.setImageResource(R.drawable.frame1);
                                break;
                            case 2:
                                rolldie.setImageResource(R.drawable.frame5);
                                break;
                            case 3:
                                rolldie.setImageResource(R.drawable.frame2);
                                break;
                            case 4:
                                rolldie.setImageResource(R.drawable.frame8);
                                break;
                            case 5:
                                rolldie.setImageResource(R.drawable.frame9);
                                break;
                            case 6:
                                rolldie.setImageResource(R.drawable.frame13);
                                break;
                        }
                    }
                }, 2100);
            }

            last_x = x;
            last_y = y;
            last_z = z;
        }
    }

}

@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {

}

@Override
protected void onPause() {
    super.onPause();
    mSensorManager.unregisterListener(this);
}

@Override
protected void onResume() {
    super.onResume();
    mSensorManager.registerListener(this, mSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
    final int X = (int) event.getRawX();
    final int Y = (int) event.getRawY();
    switch (v.getId()) {
        case R.id.yellow1:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - yellow1.getTranslationX());
                    _yDelta = (int) (Y - yellow1.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    yellow1.setTranslationX(X - _xDelta);
                    yellow1.setTranslationY(Y - _yDelta);
                    break;

            }
            break;
        case R.id.yellow2:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - yellow2.getTranslationX());
                    _yDelta = (int) (Y - yellow2.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    yellow2.setTranslationX(X - _xDelta);
                    yellow2.setTranslationY(Y - _yDelta);
                    break;

            }
            break;
        case R.id.yellow3:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - yellow3.getTranslationX());
                    _yDelta = (int) (Y - yellow3.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    yellow3.setTranslationX(X - _xDelta);
                    yellow3.setTranslationY(Y - _yDelta);
                    break;
            }
             break;
        case R.id.yellow4:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - yellow4.getTranslationX());
                    _yDelta = (int) (Y - yellow4.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    yellow4.setTranslationX(X - _xDelta);
                    yellow4.setTranslationY(Y - _yDelta);
                    break;
            }
            break;



        case R.id.black1:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - black1.getTranslationX());
                    _yDelta = (int) (Y - black1.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    black1.setTranslationX(X - _xDelta);
                    black1.setTranslationY(Y - _yDelta);
                    break;

            }
            break;
        case R.id.black2:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - black2.getTranslationX());
                    _yDelta = (int) (Y - black2.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    black2.setTranslationX(X - _xDelta);
                    black2.setTranslationY(Y - _yDelta);
                    break;

            }
            break;
        case R.id.black3:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - black3.getTranslationX());
                    _yDelta = (int) (Y - black3.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    black3.setTranslationX(X - _xDelta);
                    black3.setTranslationY(Y - _yDelta);
                    break;
            }
            break;
        case R.id.black4:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - black4.getTranslationX());
                    _yDelta = (int) (Y - black4.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    black4.setTranslationX(X - _xDelta);
                    black4.setTranslationY(Y - _yDelta);
                    break;
            }
            break;
        case R.id.blue4:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - blue4.getTranslationX());
                    _yDelta = (int) (Y - blue4.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    blue4.setTranslationX(X - _xDelta);
                    blue4.setTranslationY(Y - _yDelta);
                    break;

            }
            break;
        case R.id.blue3:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - blue3.getTranslationX());
                    _yDelta = (int) (Y - blue3.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    blue3.setTranslationX(X - _xDelta);
                    blue3.setTranslationY(Y - _yDelta);
                    break;

            }
            break;
        case R.id.blue2:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - blue2.getTranslationX());
                    _yDelta = (int) (Y - blue2.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    blue2.setTranslationX(X - _xDelta);
                    blue2.setTranslationY(Y - _yDelta);
                    break;
            }
            break;
        case R.id.blue1:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - blue1.getTranslationX());
                    _yDelta = (int) (Y - blue1.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    blue1.setTranslationX(X - _xDelta);
                    blue1.setTranslationY(Y - _yDelta);
                    break;
            }
            break;

        case R.id.white4:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - white4.getTranslationX());
                    _yDelta = (int) (Y - white4.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    white4.setTranslationX(X - _xDelta);
                    white4.setTranslationY(Y - _yDelta);
                    break;

            }
            break;
        case R.id.white3:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - white3.getTranslationX());
                    _yDelta = (int) (Y - white3.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    white3.setTranslationX(X - _xDelta);
                    white3.setTranslationY(Y - _yDelta);
                    break;

            }
            break;
        case R.id.white2:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - white2.getTranslationX());
                    _yDelta = (int) (Y - white2.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    white2.setTranslationX(X - _xDelta);
                    white2.setTranslationY(Y - _yDelta);
                    break;
            }
            break;
        case R.id.white1:
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
                case MotionEvent.ACTION_DOWN:
                    _xDelta = (int) (X - white1.getTranslationX());
                    _yDelta = (int) (Y - white1.getTranslationY());
                    break;
                case MotionEvent.ACTION_UP:
                    break;
                case MotionEvent.ACTION_POINTER_DOWN:
                    break;
                case MotionEvent.ACTION_POINTER_UP:
                    break;
                case MotionEvent.ACTION_MOVE:
                    white1.setTranslationX(X - _xDelta);
                    white1.setTranslationY(Y - _yDelta);
                    break;
            }
            break;
    }
    return true;
}
}

1 个答案:

答案 0 :(得分:0)

在您的XML文件中,您可以使用不同的布局来显示游戏中的骰子,如

 <LinearLayout
        android:layout_width  = "100dp"
        android:layout_height ="100dp"
        android:layout_margin="16dp"
        android:orientation="vertical">

    -----Here you can use your dice code 
</LinearLayout>