package my.android.pin; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; public class Pin extends Activity implements OnClickListener { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); View newGame = findViewById(R.id.newGameButton_id); newGame.setOnClickListener(this); View exitButton = findViewById(R.id.exitButton_id); exitButton.setOnClickListener(this); } public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.newGameButton_id: Intent newGame = new Intent(this, Game.class); startActivity(newGame); break; case R.id.exitButton_id: //finish(); /* this.onDestroy(); Intent i = new Intent(this, Message.class); startActivity(i); */ AlertDialog.Builder bb = new AlertDialog.Builder(this); bb.setTitle("about"); bb.setMessage("message"); DialogInterface.OnClickListener l; l = new DialogInterface.OnClickListener() { public void onClick(DialogInterface d, int i) { } }; bb.setPositiveButton("ok", l); bb.show(); break; } } } package my.android.pin; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; public class BackGround extends View { public BackGround(Context context) { super(context); // TODO Auto-generated constructor stub } public BackGround(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub } Paint mPaint; @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); Paint d = new Paint(); d.setColor(Color.BLACK); d.setStrokeWidth(3); for (int i = 0; i < 4; i++) { canvas.drawLine(0, (i * Value.grid_height), Value.layout_width, (i * Value.grid_height), d); } for (int i = 0; i < 4; i++) { canvas.drawLine((i * Value.grid_width), 0, (i * Value.grid_width), Value.layout_width, d); } } } package my.android.pin; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.widget.AbsoluteLayout; import android.widget.ImageView; public class MyImageView extends ImageView { public MyImageView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyImageView(Context context, AttributeSet attr) { super(context, attr); // TODO Auto-generated constructor stub } public MyImageView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); // TODO Auto-generated constructor stub } public MyImageView(Context context, String m) { super(context); mString = m; // TODO Auto-generated constructor stub } private Paint mPaint; //private Context mContext; private String mString = "Welcome to Mr Wei's blog"; protected void onCreate() { } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); mPaint = new Paint(); // 设置画笔颜色 //mPaint.setColor(Color.RED); // 设置填充 //mPaint.setStyle(Style.FILL); // 画一个矩形,前俩个是矩形左上角坐标,后面俩个是右下角坐标 //canvas.drawRect(new Rect(0, 0, 20, 20), mPaint); mPaint.setColor(Color.RED); mPaint.setTextSize(30); // 绘制文字 canvas.drawText(mString, 15, 35, mPaint); } public boolean onTouchEvent(MotionEvent event) { int x = (int) event.getX(); int y = (int) event.getY(); try { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Value.src_i = -1; Value.src_j = -1; Logic.whichGrid((super.getLeft() + x), (super.getTop() + y), 0); Logic.moveArea(); break; case MotionEvent.ACTION_MOVE: picMove(x, y); break; case MotionEvent.ACTION_UP: if (Value.dir >= 0) { Value.target_i = -1; Value.target_j = -1; Logic.whichGrid((super.getLeft() + x), (super.getTop() + y), 1); if (Value.fill[Value.target_i][Value.target_j] == Value.fill[Value.src_i][Value.src_j]) { resetMove(); } if (Value.fill[Value.target_i][Value.target_j] == 0) { resetMove(); Logic.extendValue(); } if (Logic.sort()) { System.out.println("OK"); } } break; } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return true; } @SuppressWarnings("deprecation") private void picMove(int cX, int cY) { int x = super.getLeft() + (cX - 25); int y = super.getTop() + (cY - 25); if (x < Value.moveXL) { x = Value.moveXL; } if (x > Value.moveXR) { x = Value.moveXR; } if (y < Value.moveYT) { y = Value.moveYT; } if (y > Value.moveYB) { y = Value.moveYB; } // System.out.println(Value.dir + "#" + Value.moveXL + "#" + Value.moveXR + "#" + Value.moveYT + "#" + Value.moveYB + "#" + x + "#" + y); this.setLayoutParams(new AbsoluteLayout.LayoutParams(50, 50, x, y)); } /** 当移动不到位,则自动移动到当前格子的基本位置 */ private void resetMove() { //System.out.println(Value.target_i + "#" + Value.target_j + "#" + Value.gridarea[Value.target_i][Value.target_j].left + "#" + Value.gridarea[Value.target_i][Value.target_j].top); this.setLayoutParams(new AbsoluteLayout.LayoutParams(50, 50, Value.gridarea[Value.target_i][Value.target_j].left, Value.gridarea[Value.target_i][Value.target_j].top)); } } package my.android.pin; import android.util.DisplayMetrics; class gridArea { int top = 0; int bottom = 0; int left = 0; int right = 0; } public class Value { public static int layout_width = 0; public static int layout_height = 0; public static int grid_width = 0; public static int grid_height = 0; public static DisplayMetrics dm = new DisplayMetrics(); public static int fill[][] = { {-1, -1, -1}, {-1, -1, -1}, {-1, -1, -1} }; public static gridArea[][] gridarea = new gridArea[3][3]; public static byte dir = -1; public final static byte TOP = 0; public final static byte BOTTOM = 1; public final static byte LEFT = 2; public final static byte RIGHT = 3; // 移动范围 public static int moveXL = 0; // x 左 public static int moveXR = 0; // x 右 public static int moveYT = 0; // y 上 public static int moveYB = 0; // y 下 public static int src_i = 0; public static int src_j = 0; public static int target_i = 0; public static int target_j = 0; } package my.android.pin; public class Tools { public static int rand(int floor, int top) { return (int) (floor + Math.floor(Math.random() * (top - floor + 1))); } } package my.android.pin; import android.app.Activity; import android.graphics.Color; import android.os.Bundle; import android.widget.AbsoluteLayout; import android.widget.LinearLayout; import android.widget.AbsoluteLayout.LayoutParams; public class Game extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.main); getWindowManager().getDefaultDisplay().getMetrics(Value.dm); Value.layout_width = Value.dm.widthPixels; Value.layout_height = Value.dm.heightPixels; Value.grid_width = Value.layout_width / 3; Value.grid_height = Value.layout_width / 3; Logic.initGridFill(); //System.out.println(Const.fill[0][0]); Logic.initGridArea(); show_layout(); } private MyImageView[][] miv = new MyImageView[3][3]; private LayoutParams param; private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final int FP = LinearLayout.LayoutParams.FILL_PARENT; private static int e = 0; @SuppressWarnings("deprecation") public void show_layout() { AbsoluteLayout layout = new AbsoluteLayout(this); layout.setBackgroundColor(Color.WHITE); //layout.setOrientation(LinearLayout.VERTICAL); BackGround bg = new BackGround(this); param = new AbsoluteLayout.LayoutParams(FP, FP, FP, FP); layout.addView(bg, param); /* e = Tools.rand(0, 9); for (int i = 0; i < 9; i++) { String t = ""; if (i == e) { t = ""; } else { t = String.valueOf(i); } miv[i] = new MyImageView(this, t); miv[i].setBackgroundColor(Color.BLUE); param = new AbsoluteLayout.LayoutParams(50, 50, 50, (50 * i)); layout.addView(miv[i], param); } */ for (int i = 0; i < Value.fill.length; i++) { for (int j = 0; j < Value.fill[i].length; j++) { String t = ""; if (Value.fill[i][j] == 0) { t = ""; continue; } else { t = String.valueOf(Value.fill[i][j]); } miv[i][j] = new MyImageView(this, t); miv[i][j].setBackgroundColor(Color.GRAY); param = new AbsoluteLayout.LayoutParams(50, 50, (Value.grid_width * j), (Value.grid_height * i)); layout.addView(miv[i][j], param); } } setContentView(layout); } } package my.android.pin; public class Logic { /** 每个格子填充数值 */ public static void initGridFill() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { while (true) { int r = Tools.rand(0, 8); if (being(r)) { Value.fill[i][j] = r; break; } } } } } /** 每个格子范围 */ public static void initGridArea() { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { Value.gridarea[i][j] = new gridArea(); Value.gridarea[i][j].top = Value.grid_height * i; Value.gridarea[i][j].left = Value.grid_width * j; Value.gridarea[i][j].bottom = Value.grid_height * (i + 1); Value.gridarea[i][j].right = Value.grid_width * (j + 1); } } } /** 相同的数值不能出现 */ private static boolean being(int r) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (Value.fill[i][j] == r) { return false; } } } return true; } /** 点击时所属格子 */ public static void whichGrid(int x, int y, int type) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { if (x >= Value.gridarea[i][j].left && x < Value.gridarea[i][j].right && y >= Value.gridarea[i][j].top && y < Value.gridarea[i][j].bottom) { int l = -1; int r = -1; int t = -1; int b = -1; // 左 if ((j - 1) < 0) { l = 10; } else { l = Value.fill[i][j - 1]; } // 右 if ((j + 1) > 2) { r = 10; } else { r = Value.fill[i][j + 1]; } // 上 if ((i - 1) < 0) { t = 10; } else { t = Value.fill[i - 1][j]; } // 下 if ((i + 1) > 2) { b = 10; } else { b = Value.fill[i + 1][j]; } Value.dir = -1; if (l == 0) { Value.dir = Value.LEFT; } else if (r == 0) { Value.dir = Value.RIGHT; } else if (t == 0) { Value.dir = Value.TOP; } else if (b == 0) { Value.dir = Value.BOTTOM; } switch (type) { case 0: Value.src_i = i; Value.src_j = j; break; case 1: Value.target_i = i; Value.target_j = j; break; } return; } } } } public static void moveArea() { Value.moveXL = Value.moveXR = Value.gridarea[Value.src_i][Value.src_j].left; Value.moveYT = Value.moveYB = Value.gridarea[Value.src_i][Value.src_j].top; switch (Value.dir) { case Value.TOP: Value.moveYT -= Value.grid_height; break; case Value.BOTTOM: Value.moveYB += Value.grid_height; break; case Value.LEFT: Value.moveXL -= Value.grid_width; break; case Value.RIGHT: Value.moveXR += Value.grid_width; break; default: return; } } /** 源格子和目标格子数值对换 */ public static void extendValue() { //System.out.println(Value.fill[Value.src_i][Value.src_j] + "#" + Value.fill[Value.target_i][Value.target_j]); if (Value.fill[Value.target_i][Value.target_j] == 0) { int tmp = Value.fill[Value.src_i][Value.src_j]; Value.fill[Value.src_i][Value.src_j] = Value.fill[Value.target_i][Value.target_j]; Value.fill[Value.target_i][Value.target_j] = tmp; } //System.out.println(Value.fill[Value.src_i][Value.src_j] + "#" + Value.fill[Value.target_i][Value.target_j]); } public static boolean sort() { for (int i = 0; i < 3; i++) { int n = i * 3 + 1; for (int j = 0; j < 3; j++) { int m = n + j; //System.out.println(m); if (i == 2 && j == 2) { m = 0; } if (Value.fill[i][j] != m) { return false; } } } return true; } } package my.android.pin; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; public class Message extends Activity implements OnClickListener { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setTheme(android.R.style.Theme_Dialog); show_layout(); } private Button btn; private TextView tv; private final int WC = LinearLayout.LayoutParams.WRAP_CONTENT; private final int FP = LinearLayout.LayoutParams.FILL_PARENT; @SuppressWarnings("deprecation") public void show_layout() { LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(FP, WC); tv = new TextView(this); param.topMargin = 5; tv.setText("############"); layout.addView(tv, param); btn = new Button(this); param.topMargin = 5; btn.setText("OK"); btn.setOnClickListener(this); layout.addView(btn, param); setContentView(layout); } public void onClick(View v) { // TODO Auto-generated method stub if (v == btn) { this.onDestroy(); Intent i = new Intent(this, Pin.class); startActivity(i); } } } <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:id="@+id/newGameButton_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/newGameButton_label" /> <Button android:id="@+id/exitButton_id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/exitButton_label" /> </LinearLayout> <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, pin!</string> <string name="app_name">pin</string> <string name="new_game">Game</string> <string name="message">Message</string> <string name="newGameButton_label">New Game</string> <string name="exitButton_label">Exit Game</string> </resources> <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="my.android.pin" android:versionCode="1" android:versionName="1.0"> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Pin" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".Game" android:label="@string/new_game"> </activity> <activity android:name=".Message" android:label="@string/message" android:theme="@android:style/Theme.Dialog" > </activity> </application> </manifest>