public void BallStart() { int height = image.getBottom() - image.getTop(); int width = image.getRight() - image.getLeft(); Log.v("hehe", "" + arrayMap.getStart()); image.layout( arrayMap.getStart().x - width / 2, arrayMap.getStart().y - height / 2, arrayMap.getStart().x - width / 2 + width, arrayMap.getStart().y - height / 2 + height); }
public void onWindowFocusChanged(boolean hasFocus) { // TODO Auto-generated method stub super.onWindowFocusChanged(hasFocus); TableLayout map1 = (TableLayout) findViewById(R.id.map); int left = map1.getLeft(); int top = map1.getTop(); arrayMap.setPara(left, top); // BallStart(); }
public void putImage(int b_size) { LinearLayout map = (LinearLayout) findViewById(R.id.map); int[][] things = arrayMap.getArray(); // 获得二维数组 ImageView[] hole = new ImageView[16]; // 图片直接使用位图缩放 Bitmap test2 = Bitmap.createScaledBitmap( BitmapFactory.decodeResource(this.getBaseContext().getResources(), R.drawable.testbmp), b_size, b_size, true); Bitmap test1 = Bitmap.createScaledBitmap( BitmapFactory.decodeResource(this.getBaseContext().getResources(), R.drawable.testbmp2), b_size, b_size, true); Bitmap test3 = Bitmap.createScaledBitmap( BitmapFactory.decodeResource(this.getBaseContext().getResources(), R.drawable.testbmp3), b_size, b_size, true); for (int rowC = 0; rowC < 21; rowC++) { TableRow tr = new TableRow(this); for (int colC = 0; colC < hole.length; colC++) { hole[colC] = new ImageView(this); LayoutParams lparam = (LayoutParams) hole[colC].getLayoutParams(); hole[colC].setLayoutParams(new LayoutParams(32, 32)); hole[colC].setAdjustViewBounds(false); // 设置边界对齐 hole[colC].setScaleType(ImageView.ScaleType.CENTER_CROP); // 设置刻度的类型 hole[colC].setPadding(0, 0, 0, 0); // 设置间距 if (rowC == 0 && colC == 0) { firstBlock = hole[colC]; } hole[0].setId(rowC); int temp; switch (things[rowC][colC]) { case 0: // 没有东西 // hole[colC].setImageResource(R.drawable.test5); hole[colC].setImageBitmap(test1); break; case 1: // 墙 // hole[colC].setImageResource(R.drawable.test3); break; case 2: // 洞 // hole[colC].setImageResource(R.drawable.test2); hole[colC].setImageBitmap(test2); break; case 3: // 终点 // hole[colC].setImageResource(R.drawable.test4); hole[colC].setImageBitmap(test3); break; } TableRow.LayoutParams lp = new TableRow.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); tr.addView(hole[colC], lp); // size = hole[colC].getRight() - hole[colC].getLeft() ; } map.addView(tr); } map = (LinearLayout) findViewById(R.id.map); map.bringChildToFront(image); }
public void handleMessage(Message msg) { switch (msg.what) { case TIME_1S: int timeLeft = Integer.parseInt(timeText.getText().toString()) - 1; timeText.setText(Integer.toString(timeLeft)); if (timeLeft == 0) { timer.cancel(); gameOver.setMessage("Time is up! Again ?"); gameOver.show(); } break; case RESET_TIME: timeText.setText("100"); timer = new Timer(); timerTask = new TimerTask() { public void run() { Message msg = new Message(); msg.what = TIME_1S; handler.sendMessage(msg); } }; timer.schedule(timerTask, 1000, 1000); break; case MOVE_IMAGE: if ((speedX > 0 && acceleratorX < 0) || (speedX < 0 && acceleratorX > 0)) speedX -= 2 * acceleratorX; else speedX -= 0.1 * acceleratorX; if ((speedY > 0 && acceleratorY < 0) || (speedY < 0 && acceleratorY > 0)) speedY += 2 * acceleratorY; else speedY += 0.1 * acceleratorY; int dx = (int) (speedX * 0.05); int dy = (int) (speedY * 0.05); View v = findViewById(R.id.image); int left = v.getLeft() + dx; int top = v.getTop() + dy; int right = v.getRight() + dx; int bottom = v.getBottom() + dy; if (left < 20) { left = 20; right = left + v.getWidth(); speedX = 0; } if (right > screenWidth - 20) { right = screenWidth - 20; left = right - v.getWidth(); speedX = 0; } if (top < 100) { top = 100; bottom = top + v.getHeight(); speedY = 0; } if (bottom > screenHeight - 20) { bottom = screenHeight - 20; top = bottom - v.getHeight(); speedY = 0; } if (arrayMap.isCollision(left, top, right, bottom) != 0) { MessageSend(COLLISION); top = v.getTop(); bottom = v.getBottom(); if (arrayMap.isCollision(left, top, right, bottom) == 1) { speedY = 0; top = v.getTop(); bottom = v.getBottom(); } else if (arrayMap.isCollision(left, top, right, bottom) == 2) { speedX = 0; left = v.getLeft(); right = v.getRight(); } } else if (arrayMap.isArrive(left, top, right, bottom)) { MessageSend(NEXT_LEVEL); left = v.getLeft(); top = v.getTop(); right = v.getRight(); bottom = v.getBottom(); LinearLayout map = (LinearLayout) findViewById(R.id.map); map.removeAllViews(); speedX = speedY = 0; } else if (arrayMap.isDead(left, top, right, bottom)) { left = v.getLeft(); top = v.getTop(); right = v.getRight(); bottom = v.getBottom(); speedX = speedY = 0; removeImage(); gameOver.show(); MessageSend(ACCELERATOR_STOP); } v.layout(left, top, right, bottom); break; case NEXT_LEVEL: LevelDatabaseHelper dbhelper = new LevelDatabaseHelper(OnGame.this, "LevelData", 1); SQLiteDatabase db = dbhelper.getReadableDatabase(); // db.execSQL("update levellock set lock=? where difficulty=? and level=?", // new Object[]{0,difficulty,level+1}); LevelDatabaseHelper.Unlock(db, level, difficulty); nextLevel.show(); break; case BALL_START: // BallStart(); break; case ACCELERATOR_STOP: SManager.unregisterListener(listener); acceleratorX = 0; acceleratorY = 0; speedX = 0; speedY = 0; break; } }
@Override public boolean onTouch(View v, MotionEvent event) { int action = event.getAction(); switch (action) { case MotionEvent.ACTION_DOWN: lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_MOVE: int dx = (int) event.getRawX() - lastX; int dy = (int) event.getRawY() - lastY; int left = v.getLeft() + dx; int top = v.getTop() + dy; int right = v.getRight() + dx; int bottom = v.getBottom() + dy; if (left < 20) { left = 20; right = left + v.getWidth(); } if (right > screenWidth - 20) { right = screenWidth - 20; left = right - v.getWidth(); } if (top < 100) { top = 100; bottom = top + v.getHeight(); } if (bottom > screenHeight - 20) { bottom = screenHeight - 20; top = bottom - v.getHeight(); } if (arrayMap.isCollision(left, top, right, bottom) != 0) { MessageSend(COLLISION); if (arrayMap.isCollision(left, top, right, bottom) == 2) { speedY = 0; top = v.getTop(); bottom = v.getBottom(); } else if (arrayMap.isCollision(left, top, right, bottom) == 1) { left = v.getLeft(); right = v.getRight(); speedX = 0; } } else if (arrayMap.isArrive(left, top, right, bottom)) { MessageSend(NEXT_LEVEL); left = v.getLeft(); top = v.getTop(); right = v.getRight(); bottom = v.getBottom(); speedX = speedY = 0; } else if (arrayMap.isDead(left, top, right, bottom)) { // SManager.registerListener(null, sensor,SensorManager.SENSOR_DELAY_FASTEST); left = v.getLeft(); top = v.getTop(); right = v.getRight(); bottom = v.getBottom(); speedX = speedY = 0; } v.layout(left, top, right, bottom); lastX = (int) event.getRawX(); lastY = (int) event.getRawY(); break; case MotionEvent.ACTION_UP: break; } return false; }