网上下的源码,花了一天时间读懂了,加了些注释。在这里留个爪痕。。(怕以后忘记。。) 
 
Activity: 
 package com.tyj.onepiece;

import android.app.Activity;
import android.util.AttributeSet;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ProgressBar;
import android.widget.TextView;

public class OnePieceGame extends Activity {
 /** Called when the activity is first created. */
 private ProgressBar pb;
 private TextView show_RemainTime;
 private CtrlView cv;
 public static final int START_ID = Menu.FIRST;
 public static final int REARRARY_ID = Menu.FIRST + 1;
 public static final int END_ID = REARRARY_ID + 1;
 private int dormant = 1000;
 private boolean isCancel=true;

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  findViews();
  mRedrawHandler.sleep(dormant);

 }

 private RefreshHandler mRedrawHandler = new RefreshHandler();

 class RefreshHandler extends Handler {
  @Override
  public void handleMessage(Message msg) {
   if(isCancel){
    run();
   }else{}      
  }

  public void sleep(long delayMillis) {
   this.removeMessages(0);// 移除信息队列中最顶部的信息(从顶部取出信息)
   sendMessageDelayed(obtainMessage(0), delayMillis);// 获得顶部信息并延时发送
  }
 };

 public void run() {
  if (cv.PROCESS_VALUE > 0 && cv.much != 0) {
   cv.PROCESS_VALUE--;
   pb.setProgress(cv.PROCESS_VALUE);
   show_RemainTime.setText(String.valueOf(cv.PROCESS_VALUE));
   mRedrawHandler.sleep(dormant);
  } else if (cv.PROCESS_VALUE == 0 && cv.much != 0) {
   cv.setEnabled(false);
   dialogForFail().show();
  } else if (cv.PROCESS_VALUE != 0 && cv.much == 0) {
   cv.setEnabled(false);
   dialogForSucceed().show();
  }
 }

 private void findViews() {
  pb = (ProgressBar) findViewById(R.id.pb);
  show_RemainTime = (TextView) findViewById(R.id.show_remainTime);
  cv = (CtrlView) findViewById(R.id.cv);
  pb.setMax(cv.GAMETIME);
  pb.incrementProgressBy(-1);
  pb.setProgress(cv.PROCESS_VALUE);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // TODO Auto-generated method stub
  menu.add(0, START_ID, 0, R.string.newgame);
  menu.add(0, REARRARY_ID, 0, R.string.rearrage);
  menu.add(0, END_ID, 0, R.string.exit);
  return super.onCreateOptionsMenu(menu);
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case START_ID:
   newPlay();
   break;
  case REARRARY_ID:
   cv.rearrange();
   cv.PROCESS_VALUE = cv.PROCESS_VALUE - 5;
   pb.setProgress(cv.PROCESS_VALUE);
   break;
  case END_ID:
   isCancel=false;
   finish();
   break;
  default:
   break;
  }
  return super.onOptionsItemSelected(item);
 }

 @Override
 protected void onStop() {
  isCancel=false;
  pb = null;
  cv = null;
  super.onStop();
 }

 @Override
 protected void onDestroy(){
  isCancel=false;
  super.onDestroy();
 }

 @Override
 protected void onStart(){
  isCancel=false;
  newPlay();
  isCancel=true;
  super.onStart();

 }
// @Override
// protected void onRestart(){
//  cv.reset();
//  super.onRestart();
// }
// 

 public void newPlay() {
  cv.reset();
  pb.setProgress(cv.GAMETIME);
  cv.PROCESS_VALUE = cv.GAMETIME;
  mRedrawHandler.sleep(dormant);
  cv.setEnabled(true);
 }

 public AlertDialog dialogForSucceed() {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setIcon(R.drawable.icon).setMessage(R.string.succeedInfo)
    .setPositiveButton(R.string.next,
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog,
         int which) {
        // TODO Auto-generated method stub
        dormant = dormant - 300;
        newPlay();
       }
      }).setNeutralButton(R.string.again_challenge,
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog,
         int which) {
        // TODO Auto-generated method stub
        newPlay();
       }
      });
  return builder.create();
 }

 public AlertDialog dialogForFail() {
  AlertDialog.Builder builder = new AlertDialog.Builder(this);
  builder.setIcon(R.drawable.icon).setMessage(R.string.failInfo)
    .setPositiveButton(R.string.again_challenge,
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog,
         int which) {
        // TODO Auto-generated method stub
        newPlay();
       }
      }).setNegativeButton(R.string.exit,
      new DialogInterface.OnClickListener() {
       @Override
       public void onClick(DialogInterface dialog,
         int which) {
        // TODO Auto-generated method stub
        isCancel=false;
        finish();
       }
      });
  return builder.create();
 }

}main.xml 
 
<?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">
 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent" android:layout_height="wrap_content">
   <TableRow>
   <ProgressBar android:id="@+id/pb" android:layout_width="fill_parent"
    android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal" 
    android:layout_weight="9"/>
   <TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:text="@string/remain_time" android:layout_weight="1"/>
   <TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
    android:id="@+id/show_remainTime" android:layout_weight="1"/> 
  </TableRow>
 </TableLayout>

 <com.tyj.onepiece.CtrlView android:id="@+id/cv"
  android:layout_width="fill_parent" android:layout_height="fill_parent" />
</LinearLayout>
CtrlView.java 
 
package com.tyj.onepiece;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.MotionEvent;

public class CtrlView extends GameView {
 public final int GAMETIME = 300;
 public final int UPTIME = 1;
 public int PROCESS_VALUE = 300;
 public static boolean CURRENT_CH = false;
 public int CURRENT_TYPE = 0;
 private Point C_POINT;
 private Point P_POINT;
 LinkedList<Line> li;

 public CtrlView(Context context, AttributeSet attrs) {
  super(context, attrs);
  initType();
  initGrid();
  much = (row - 2) * (col - 2);
 }

 public CtrlView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  initType();
  initGrid();
  much = (row - 2) * (col - 2);
 }

 public boolean onTouchEvent(MotionEvent event) {
  if (event.getAction() != MotionEvent.ACTION_DOWN)
   return super.onTouchEvent(event);

  int selX = (int) (event.getX() / width);
  int selY = (int) (event.getY() / height);

  if (grid[selX][selY] == 0)
   //实际放图外四周留出的那个方格
   return true;
  else {
   //第一个点击方格
   if (CURRENT_CH == false) {
    select(selX, selY);
    CURRENT_CH = true;
    P_POINT = new Point(selX, selY);//记录第一个点击方格的点
   } else {//第二个点击方格
    C_POINT = new Point(selX, selY);//记录第二个点击方格的点
    lineType = 0;
    if (checkLink(P_POINT, C_POINT)) {
     System.out.println("checkLink is true ... ");
     isLine = true;
     much = much - 2;
     if (0 < PROCESS_VALUE
       && (PROCESS_VALUE + UPTIME) < GAMETIME) {
      PROCESS_VALUE = PROCESS_VALUE + UPTIME;
     }
     invalidate();
     mRedrawHandler.sleep(300);
    }
    CURRENT_CH = false;
   }
  }
  return true;
 }

 public void reset() {
  CURRENT_CH = false;
  CURRENT_TYPE = 0;
  C_POINT = null;
  P_POINT = null;
  lineType = 0;
  isLine = false;
  Point[] p = null;
  initType();
  initGrid();
  much = (row - 2) * (col - 2);
  invalidate();
 }

 public void rearrange() {
  CURRENT_CH = false;
  CURRENT_TYPE = 0;
  C_POINT = null;
  P_POINT = null;
  lineType = 0;
  isLine = false;
  Point[] p = null;
  List<Integer> temp = new ArrayList<Integer>();
  for (int i = 0; i < row; i++) {
   for (int j = 0; j < col; j++) {
    if (grid[i][j] != 0) {
     temp.add(grid[i][j]);
    }
   }
  }
  type.clear();
  Random ad = new Random();
  for (int i = 0; i < temp.size(); i++) {
   type.add(temp.get(i));
  }
  temp.clear();
  temp = null;
  for (int i = 0; i < row; i++) {
   for (int j = 0; j < col; j++) {
    if (grid[i][j] != 0) {
     int index = ad.nextInt(type.size());
     grid[i][j] = type.get(index);
     type.remove(index);
    }
   }
  }
  invalidate();
 }

 private RefreshHandler mRedrawHandler = new RefreshHandler();

 class RefreshHandler extends Handler {

  @Override
  public void handleMessage(Message msg) {
   System.out.println("获取消息:" + msg.obtain());
   isLine = false;
   grid[P_POINT.x][P_POINT.y] = 0;
   grid[C_POINT.x][C_POINT.y] = 0;
   CtrlView.this.invalidate();
  }

  public void sleep(long delayMillis) {
   System.out.println("延时时间:" + delayMillis);
//   this.removeMessages(0);// 移除信息队列中最顶部的信息(从顶部取出信息)
   sendMessageDelayed(obtainMessage(0), delayMillis);// 获得顶部信息并延时发送

  }
 };

 public class Point {
  public int x;
  public int y;

  public Point(int newx, int newy) {
   this.x = newx;
   this.y = newy;
  }

  public boolean equals(Point p) {
   if (p.x == x && p.y == y)
    return true;
   else
    return false;
  }
 }

 private boolean horizon(Point a, Point b) {
  if (a.x == b.x && a.y == b.y)//两次点击的是同一位置
   return false;
  int x_start = a.y <= b.y ? a.y : b.y;//取两次点击y小的那个
  int x_end = a.y <= b.y ? b.y : a.y;//取两次点击y较大的那个
  for (int x = x_start + 1; x < x_end; x++)
   if (grid[a.x][x] != 0) {
    return false;
   }
  p = new Point[] { a, b };
  lineType = H_LINE;
  return true;
 }

 private boolean vertical(Point a, Point b) {
  if (a.x == b.x && a.y == b.y)
   return false;
  int y_start = a.x <= b.x ? a.x : b.x;
  int y_end = a.x <= b.x ? b.x : a.x;
  for (int y = y_start + 1; y < y_end; y++)
   if (grid[y][a.y] != 0)
    return false;
  p = new Point[] { a, b };
  lineType = V_LINE;
  return true;
 }

 private boolean oneCorner(Point a, Point b) {
  //c d 为a b 的对角点
  Point c = new Point(a.x, b.y);
  Point d = new Point(b.x, a.y);
  if (grid[c.x][c.y] == 0) {
   boolean method1 = horizon(a, c) && vertical(b, c);
   p = new Point[] { a, new Point(c.x, c.y), b };
   lineType = ONE_C_LINE;
   return method1;
  }
  if (grid[d.x][d.y] == 0) {
   boolean method2 = vertical(a, d) && horizon(b, d);
   p = new Point[] { a, new Point(d.x, d.y), b };
   lineType = ONE_C_LINE;
   return method2;
  } else {
   return false;
  }
 }

 class Line {
  public Point a;
  public Point b;
  public int direct;

  public Line() {
  }

  public Line(int direct, Point a, Point b) {
   this.direct = direct;
   this.a = a;
   this.b = b;
  }
 }

 private LinkedList<Line> scan(Point a, Point b) {
  li = new LinkedList<Line>();
  System.out.println("a.x = " + a.x + ",a.y = " + a.y + ",b.x = " + b.x + ",b.y = " + b.y);
  for (int y = a.y; y >= 0; y--)
   if (grid[a.x][y] == 0 && grid[b.x][y] == 0
     && vertical(new Point(a.x, y), new Point(b.x, y)))
    li.add(new Line(0, new Point(a.x, y), new Point(b.x, y)));

  for (int y = a.y; y < row; y++)
   if (grid[a.x][y] == 0 && grid[b.x][y] == 0
     && vertical(new Point(a.x, y), new Point(b.x, y)))
    li.add(new Line(0, new Point(a.x, y), new Point(b.x, y)));

  for (int x = a.x; x >= 0; x--)
   if (grid[x][a.y] == 0 && grid[x][b.y] == 0
     && horizon(new Point(x, a.y), new Point(x, b.y)))
    li.add(new Line(1, new Point(x, a.y), new Point(x, b.y)));

  for (int x = a.x; x < col; x++)
   if (grid[x][a.y] == 0 && grid[x][b.y] == 0
     && horizon(new Point(x, a.y), new Point(x, b.y)))
    li.add(new Line(1, new Point(x, a.y), new Point(x, b.y)));

  return li;
 }

 private boolean twoCorner(Point a, Point b) {
  //li 存放着a、b之间可以连通的线段两端点
  li = scan(a, b);
  if (li.isEmpty())
   return false;
  for (int index = 0; index < li.size(); index++) {
   Line line = (Line) li.get(index);
   if (line.direct == 1) {//纵向线段
    if (vertical(a, line.a) && vertical(b, line.b)) {
     p = new Point[] { a, line.a, line.b, b };
     lineType = TWO_C_LINE;
     return true;
    }
   } else if (horizon(a, line.a) && horizon(b, line.b)) {//横向线段
    p = new Point[] { a, line.a, line.b, b };
    lineType = TWO_C_LINE;
    return true;
   }
  }
  return false;
 }

 public boolean checkLink(Point a, Point b) {
  if (grid[a.x][a.y] != grid[b.x][b.y])// 如果图案不同,直接为false
   return false;
  if (a.x == b.x && horizon(a, b))
   return true;
  if (a.y == b.y && vertical(a, b))
   return true;
  if (oneCorner(a, b))
   return true;
  else
   return twoCorner(a, b);
 }

}GameView.java 
 
package com.tyj.onepiece;

//画出网格,并对应的画上分布好的图像
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import com.tyj.onepiece.CtrlView.Point;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

public class GameView extends View {

 public final int row = 10;
 public final int col = 10;
 public float width;//方格的宽
 public float height;//方格的高
 private int selY;
 private int selX;
 public boolean isLine = false;
 public int grid[][] = new int[row][col];
 private Rect selRect = new Rect();
 public int lineType = 0;
 public final int V_LINE = 1;
 public final int H_LINE = 1;
 public final int ONE_C_LINE = 2;
 public final int TWO_C_LINE = 3;
 public int much = 0;
 Point[] p;
 public int[] imageType = new int[] { R.drawable.aa, R.drawable.bb,
   R.drawable.cc, R.drawable.dd, R.drawable.ee, R.drawable.ff,
   R.drawable.gg, R.drawable.hh, R.drawable.ii, R.drawable.jj,
   R.drawable.kk, R.drawable.ll, R.drawable.mm, R.drawable.nn,
   R.drawable.oo, R.drawable.pp };
 public Bitmap[] image;
 public List<Integer> type = new ArrayList<Integer>();

 public GameView(Context context, AttributeSet attrs) {
  super(context, attrs);
  this.setFocusable(true);
  this.setFocusableInTouchMode(true);
 }

 public GameView(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  this.setFocusable(true);
  this.setFocusableInTouchMode(true);
 }

 public void reset() {

 }
 /**
  * 填充图片
  * @param context
  */
 public void fillImage(Context context) {
  int lth = imageType.length;
  image = new Bitmap[lth];
  for (int i = 0; i < lth; i++) {
   Bitmap bitmap = Bitmap.createBitmap((int) width, (int) height,
     Bitmap.Config.ARGB_8888);
   Drawable drw;
   Canvas canvas = new Canvas(bitmap);
   drw = context.getResources().getDrawable(imageType[i]);
   drw.setBounds(1, 1, 30, 30);
   drw.draw(canvas);
   image[i] = bitmap;
  }
 }

 /**
  * 初始化type
  * 挨着放置四个一样共64个图片id
  */
 public void initType() {
  //实际放置图片的位置个数
  int size = (row - 2) * (col - 2);
  //此处的count应该是每一样图片应该放置的个数,也就是说一个有64个位置、16样图片,那么64个位置每样图片应该都放置4个。
  int count = size / imageType.length;
  for (int j = 0; j < imageType.length; j++) {
   for (int i = 0; i < count; i++) {
    //挨着放置四个一样共64个图片id
    type.add(imageType[j]);
    System.out.println("内层 j = " + j + ",i = " + i);
   }
  }
 }

 public void select(int x, int y) {
  invalidate(selRect);
  selX = Math.min(Math.max(x, 0), 9);
  selY = Math.min(Math.max(y, 0), 9);
  getRect(selX, selY, selRect);
  invalidate(selRect);
 }

 private void getRect(int x, int y, Rect rect) {
  rect.set((int) (x * width), (int) (y * height),
    (int) (x * width + width), (int) (y * height + height));
 }

 @Override
 protected void onDraw(Canvas canvas) {
  Paint background = new Paint();
  background.setColor(Color.WHITE);
  canvas.drawRect(0, 0, getWidth(), getHeight(), background);
  //网格线画笔
  Paint hilite = new Paint();
  hilite.setColor(getResources().getColor(R.color.hilite));
  Paint light = new Paint();
  light.setColor(getResources().getColor(R.color.light));
  //画出网格
  for (int i = 0; i <= 9; i++) {
   canvas.drawLine(0, i * height, getWidth(), i * height, light);
   canvas.drawLine(0, i * height + 1, getWidth(), i * height + 1,
     hilite);
   canvas.drawLine(i * width, 0, i * width, getHeight(), light);
   canvas.drawLine(i * width + 1, 0, i * width + 1, getHeight(),
     hilite);
  }

  if (CtrlView.CURRENT_CH) {
   Paint selected = new Paint();
   selected.setColor(getResources().getColor(R.color.puzzle_selected));
   canvas.drawRect(selRect, selected);
  }

  for (int i = 0; i < 9; i++) {
   for (int j = 0; j < 9; j++) {
    if (grid[i][j] != 0) {
     canvas.drawBitmap(image[Arrays.binarySearch(imageType,
       grid[i][j])], i * width, j * height, null);
    }
   }
  }

  if (isLine) {
   Paint lineColor = new Paint();
   lineColor.setColor(Color.RED);

   Paint test = new Paint();
   test.setColor(Color.BLUE);

   switch (lineType) {
   //相邻两个图片的一条横、纵线
   case V_LINE:
    canvas.drawLine(p[0].x * width + width / 2, p[0].y * height
      + height / 2, p[1].x * width + width / 2, p[1].y
      * height + height / 2, lineColor);
    break;
   //一个折角的两条线
   case ONE_C_LINE:
    canvas.drawLine(p[0].x * width + width / 2, p[0].y * height
      + height / 2, p[1].x * width + width / 2, p[1].y
      * height + height / 2, lineColor);
    canvas.drawLine(p[1].x * width + width / 2, p[1].y * height
      + height / 2, p[2].x * width + width / 2, p[2].y
      * height + height / 2, lineColor);
    break;
   //两个折角的
   case TWO_C_LINE:
    canvas.drawLine(p[0].x * width + width / 2, p[0].y * height
      + height / 2, p[1].x * width + width / 2, p[1].y
      * height + height / 2, test);
    canvas.drawLine(p[1].x * width + width / 2, p[1].y * height
      + height / 2, p[2].x * width + width / 2, p[2].y
      * height + height / 2, test);
    canvas.drawLine(p[3].x * width + width / 2, p[3].y * height
      + height / 2, p[2].x * width + width / 2, p[2].y
      * height + height / 2, test);
    break;
   default:
    break;
   }
  }
  super.onDraw(canvas);
 }

 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  width = w / row;
  height = h / col;
  // getRect(1,1,selRect);
  fillImage(this.getContext());
  super.onSizeChanged(w, h, oldw, oldh);
 }

 /**
  * 初始化grid
  * 将16样共64张图片的id 随机化
  */

 public void initGrid() {
  //随机
  Random ad = new Random();
  for (int i = 0; i < row; i++) {
   for (int j = 0; j < col; j++) {
    if (i == 0 || i == row - 1 || j == 0 || j == col - 1) {
     //实际放图片的是8*8,所以四周留出1一个方格位置
     grid[i][j] = 0;
    } else {
     if (type != null && type.size() > 0) {
      //取0到64的随机数
      int index = ad.nextInt(type.size());
      //将随机数位置的图片id   赋值给grid
      grid[i][j] = type.get(index);
      //将该位置中的图片id移出
      type.remove(index);
     }
    }
   }
  }
 }

}color.xml 
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
<!-- 网格线颜色 -->
<color name="hilite">#ffffffff</color>
<color name="light">#64c6d4ef</color>
<color name="puzzle_dark">#6456648f</color>
<color name="puzzle_foreground">#ff000000</color>
<color name="puzzle_hint_0">#64ff0000</color>
<color name="puzzle_hint_1">#6400ff80</color>
<color name="puzzle_hint_2">#2000ff80</color>
<color name="puzzle_selected">#64ff8000</color>
</resources>运行效果图: 
 
 
  


帮助理解的图解: