From: gumartinm Date: Thu, 26 Apr 2012 07:45:10 +0000 (+0200) Subject: Working on my simple Android tetris. X-Git-Url: https://git.gumartinm.name/?a=commitdiff_plain;h=f05b57706d0f214a9edc4605f416423d68519a4a;p=JavaForFun Working on my simple Android tetris. Using GestureListener and nested views. --- diff --git a/Android/AndroidTetris/gen/de/android/androidtetris/R.java b/Android/AndroidTetris/gen/de/android/androidtetris/R.java index f19d335..1a06597 100644 --- a/Android/AndroidTetris/gen/de/android/androidtetris/R.java +++ b/Android/AndroidTetris/gen/de/android/androidtetris/R.java @@ -17,6 +17,7 @@ public final class R { public static final int yellowstar=0x7f020003; } public static final class id { + public static final int game_display=0x7f050002; public static final int level_display=0x7f050001; public static final int score_display=0x7f050000; } diff --git a/Android/AndroidTetris/res/layout/main.xml b/Android/AndroidTetris/res/layout/main.xml index ad10b77..2b6b050 100644 --- a/Android/AndroidTetris/res/layout/main.xml +++ b/Android/AndroidTetris/res/layout/main.xml @@ -2,20 +2,37 @@ - - - - + android:gravity="center" + android:orientation="vertical" > + + + + + + + + + diff --git a/Android/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java b/Android/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java index 283a20a..f855210 100644 --- a/Android/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java +++ b/Android/AndroidTetris/src/de/android/androidtetris/AndroidTetrisActivity.java @@ -1,10 +1,8 @@ package de.android.androidtetris; import android.app.Activity; -import android.content.Context; import android.os.Bundle; import android.util.DisplayMetrics; -import android.view.inputmethod.InputMethodManager; public class AndroidTetrisActivity extends Activity { DrawView drawView; @@ -16,9 +14,9 @@ public class AndroidTetrisActivity extends Activity { this.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics); setContentView(R.layout.main); - drawView = new DrawView(this); + //drawView = new DrawView(this); //drawView.setDimensions(displayMetrics.widthPixels, displayMetrics.heightPixels); - this.setContentView(drawView); - drawView.requestFocus(); + //this.setContentView(drawView); + //drawView.requestFocus(); } } \ No newline at end of file diff --git a/Android/AndroidTetris/src/de/android/androidtetris/DrawView.java b/Android/AndroidTetris/src/de/android/androidtetris/DrawView.java index 4277223..6b7981c 100644 --- a/Android/AndroidTetris/src/de/android/androidtetris/DrawView.java +++ b/Android/AndroidTetris/src/de/android/androidtetris/DrawView.java @@ -11,7 +11,10 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; +import android.util.AttributeSet; +import android.view.GestureDetector; import android.view.KeyEvent; +import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; @@ -21,16 +24,17 @@ import android.view.SurfaceView; */ public class DrawView extends SurfaceView { private SurfaceHolder holder; - private final MainLoop mainLoop; + private MainLoop mainLoop; private static final int TILESIZE=32; - private static final int MAPWIDTH=10; - private static final int MAPHEIGHT=20; - private static final int GREY=8; + private static final int MAPWIDTH=10; //Traditional tetris just 10 columns + private static final int MAPHEIGHT=24; //Traditional tetris just 20 rows + private static final int GREY=7; private Bitmap[] tileArray; private Tile[][] mapMatrix; private PrePiece prePiece; private CurrentPiece currentPiece; - private final ExecutorService exec; + private ExecutorService exec; + private GestureDetector gestureDetector; private class MainLoop implements Runnable { @@ -69,6 +73,23 @@ public class DrawView extends SurfaceView { public DrawView(final Context context) { super(context); + this.initialize(context); + } + + public DrawView(Context context, AttributeSet attrs) { + super(context, attrs); + initialize(context); + } + + public DrawView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + this.initialize(context); + } + + private void initialize(Context context) { + gestureDetector = new GestureDetector(context, new GestureListener(this)); + + //I have so much to learn... //The OnKeyListener for a specific View will only be called if the key is pressed @@ -108,8 +129,6 @@ public class DrawView extends SurfaceView { } }); } - - private void resetTiles(int tilecount) { tileArray = new Bitmap[tilecount]; } @@ -163,35 +182,35 @@ public class DrawView extends SurfaceView { private void drawMap(Canvas canvas) { - canvas.drawColor(Color.WHITE); + canvas.drawColor(Color.BLACK); //We have to center the grid in the middle of our canvas. //canvas.getWidth() <----------------- retrieve the screen width //canvas.getWidth()/TILESIZE <-------- the tile size is 16, so we have to count on it when finding the center //((canvas.getWidth()/TILESIZE))/2 <-- this is the middle of our screen, it depends on the tile size. - final int initX = ((((canvas.getWidth()/TILESIZE)/2) - MAPWIDTH) + 1); + final int initX = ((((canvas.getWidth()/TILESIZE)/2) - MAPWIDTH) + 2); //draw the left bar (with scores, and next pieces for(int x=MAPWIDTH; x< MAPWIDTH + GREY; x++) for(int y=0; y< MAPHEIGHT; y++) - drawTile(canvas, Tile.GRAY.getColor(), x + initX, y + 10); + drawTile(canvas, Tile.GRAY.getColor(), x + initX, y + 2); //draw the pre-piece for(int x=0; x < PrePiece.WIDTH; x++) for(int y=0; y< PrePiece.HEIGHT; y++) if(prePiece.size[x][y] != Tile.NOCOLOR) - drawTile(canvas, prePiece.size[x][y].getColor(), prePiece.x + x + initX, prePiece.y + y + 10); + drawTile(canvas, prePiece.size[x][y].getColor(), prePiece.x + x + initX, prePiece.y + y + 2); //draw grid for(int x=0; x < MAPWIDTH; x++) for(int y=0; y < MAPHEIGHT; y++) - drawTile(canvas, mapMatrix[x][y].getColor(), x + initX, y+10); + drawTile(canvas, mapMatrix[x][y].getColor(), x + initX, y + 2); //draw the current block for(int x=0; x < CurrentPiece.WIDTH; x++) for(int y=0; y < CurrentPiece.HEIGHT; y++) if(currentPiece.size[x][y] != Tile.NOCOLOR) - drawTile(canvas, currentPiece.size[x][y].getColor(), currentPiece.x + x + initX, currentPiece.y +y +10); + drawTile(canvas, currentPiece.size[x][y].getColor(), currentPiece.x + x + initX, currentPiece.y +y + 2); } @@ -371,4 +390,36 @@ public class DrawView extends SurfaceView { return false; } + + @Override + public boolean onTouchEvent(MotionEvent motionEvent) { + return gestureDetector.onTouchEvent(motionEvent); + } + + + public void onLeftSwipe() { + //userMoveLeft(); + } + + + public void onRightSwipe() { + synchronized (this.getHolder()) + { + Canvas c = this.getHolder().lockCanvas(); + this.move(1, 0); + this.drawMap(c); + //view.onDraw(c); + this.getHolder().unlockCanvasAndPost(c); + } + } + + public void onTapUp(boolean onGround) { + //userRotate(); + } + + public void onDownSwipe() { + //userFallDown(); + } + + } diff --git a/Android/AndroidTetris/src/de/android/androidtetris/GestureListener.java b/Android/AndroidTetris/src/de/android/androidtetris/GestureListener.java new file mode 100644 index 0000000..f31751c --- /dev/null +++ b/Android/AndroidTetris/src/de/android/androidtetris/GestureListener.java @@ -0,0 +1,52 @@ +package de.android.androidtetris; + +import android.view.GestureDetector.OnGestureListener; +import android.view.MotionEvent; + +public class GestureListener implements OnGestureListener { + private final DrawView drawView; + + public GestureListener(final DrawView drawView) + { + this.drawView = drawView; + } + + @Override + public boolean onDown(MotionEvent e) { + //this.drawView.onRightSwipe(); + return false; + } + + @Override + public void onShowPress(MotionEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public boolean onSingleTapUp(MotionEvent e) { + // TODO Auto-generated method stub + return false; + } + + @Override + public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, + float distanceY) { + // TODO Auto-generated method stub + return false; + } + + @Override + public void onLongPress(MotionEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, + float velocityY) { + this.drawView.onRightSwipe(); + return false; + } + +}