画出矩形,并能通过键盘控制小方块在矩形内移动
MainActivity.java
package cn.lesson.test; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.MotionEvent; public class MainActivity extends Activity { private TestView pushBox = null; @Override protected void onCreate(Bundle savedInstanceState) { pushBox = new TestView(this); super.onCreate(savedInstanceState); setContentView(pushBox); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_DPAD_UP) { TestView.y--; TestView.reset(9, 9); } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN) { TestView.y++; TestView.reset(9, 9); } if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) { TestView.x--; TestView.reset(9, 9); } if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) { TestView.x++; TestView.reset(9, 9); } pushBox.invalidate(); return super.onKeyDown(keyCode, event); } }
TestView.java
package cn.lesson.test; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.drawable.Drawable; import android.util.AttributeSet; import android.util.Log; import android.view.View; public class TestView extends View { public TestView(Context context) { super(context); } public TestView(Context context, AttributeSet attrs) { super(context, attrs); } private static int height = 30; private static int width = 30; public static int x = 0; public static int y = 0; public static boolean check(int W, int H) { if (TestView.x < H && TestView.y < W && TestView.y > 0 && TestView.x > 0) { return true; } return false; } public static void reset(int W, int H) { if (!check(W, H)) { if (TestView.x >= H) { TestView.x = 0; } if (TestView.x < 0) { TestView.x = H - 1; } if (TestView.y >= W) { TestView.y = 0; } if (TestView.y < 0) { TestView.y = W - 1; } } } @SuppressLint("DrawAllocation") @Override protected void onDraw(Canvas canvas) { Log.d("onDraw:", "x:" + x + "y:" + y); Paint paint = new Paint(); Drawable dw = this.getResources().getDrawable(R.drawable.bg); dw.setBounds(0, 0, height, width); Bitmap bitMap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888); Canvas cs = new Canvas(bitMap); dw.draw(cs); paint.setColor(Color.CYAN); canvas.drawBitmap(bitMap, height * (x + 1), width * (y + 1), paint); canvas.drawLine(30f, 30f, 300f, 30f, paint); canvas.drawLine(30f, 30f, 30f, 300f, paint); canvas.drawLine(300f, 30f, 300f, 300f, paint); canvas.drawLine(30f, 300f, 300f, 300f, paint); Bitmap left = BitmapFactory.decodeResource(getResources(), R.drawable.left); Bitmap right = BitmapFactory.decodeResource(getResources(), R.drawable.right); Bitmap up = BitmapFactory.decodeResource(getResources(), R.drawable.up); Bitmap down = BitmapFactory.decodeResource(getResources(), R.drawable.down); canvas.drawBitmap(left, 50, 320, paint); canvas.drawBitmap(up, 100, 320, paint); canvas.drawBitmap(down, 150, 320, paint); canvas.drawBitmap(right, 200, 320, paint); super.onDraw(canvas); } }
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <cn.lesson.test.TestView android:id="@+id/testView" android:layout_width="100dip" android:layout_height="100dip" /> </RelativeLayout>