Android 马赛克(Mosaics)效果

  public static Bitmap getMosaicsBitmap(Bitmap bmp, double precent) {

  long start = System.currentTimeMillis();

  int bmpW = bmp.getWidth();

  int bmpH = bmp.getHeight();

  Bitmap resultBmp = Bitmap.createBitmap(bmpW, bmpH, Bitmap.Config.ARGB_8888);

  Canvas canvas = new Canvas(resultBmp);

  Paint paint = new Paint();

  double unit;

  if (precent == 0) {

  unit = bmpW;

  } else {

  unit = 1 / precent;

  }

  double resultBmpW = bmpW / unit;

  double resultBmpH = bmpH / unit;

  for (int i = 0; i < resultBmpH; i++) {

  for (int j = 0; j < resultBmpW; j++) {

  int pickPointX = (int) (unit * (j + 0.5));

  int pickPointY = (int) (unit * (i + 0.5));

  int color;

  if (pickPointX >= bmpW || pickPointY >= bmpH) {

  color = bmp.getPixel(bmpW / 2, bmpH / 2);

  } else {

  color = bmp.getPixel(pickPointX, pickPointY);

  }

  paint.setColor(color);

  canvas.drawRect((int) (unit * j), (int) (unit * i), (int) (unit * (j + 1)), (int) (unit * (i + 1)), paint);

  }

  }

  canvas.setBitmap(null);

  long end = System.currentTimeMillis();

  Log.v(TAG, "DrawTime:" + (end - start));

  return resultBmp;

  }