说到图片切块,首先想到的是拼图游戏,在载入图片的时候回进行图片切块操作,增加难度的时候会再一次进行切块操作,不同的是切块的数量会更多。
首先来看一下拼图算法:
[java] view plain copy
- //拼图
- public static Bitmap Pintu(Bitmap bitmap){
- int w = bitmap.getWidth();
- int h = bitmap.getHeight();
- Bitmap resultBitmap = Bitmap.createBitmap(w - 4, h - 4, Bitmap.Config.ARGB_8888);
- List<ImagePiece> mItemBitmaps;
- //切图 3*3 得到9个碎片
- mItemBitmaps = ImageSplitterUtil.splitImage(bitmap, 3);
- List<ImagePiece> imagePieces = new ArrayList<ImagePiece>();
- Canvas cv = new Canvas(resultBitmap);
- int pieceWidth = w / 3;
- int pieceHeight = h / 3;
- int w2 = 0;
- int h2 = 0;
- //对每个碎片随机产生一种效果,并保存到数组中
- for(int s = 0; s < mItemBitmaps.size(); s++){
- int type = (int) (Math.random() * 13);
- Bitmap tempBitmap = mItemBitmaps.get(s).getBitmap();
- ImagePiece imagePiece = new ImagePiece();
- imagePiece.setBitmap(pintu(tempBitmap, type));
- imagePieces.add(imagePiece);
- }
- //在画布上将每个经过处理后的碎片重新绘制到画布上
- for(int i = 0; i < imagePieces.size(); i++){
- if(i == 3 || i == 6){
- w2 = w2 + pieceWidth - 2;
- h2 = 0;
- }else if(i != 0){
- h2 += pieceHeight - 2;
- }
- cv.drawBitmap(imagePieces.get(i).getBitmap(), w2, h2, null);
- cv.save(Canvas.ALL_SAVE_FLAG);
- cv.restore();
- }
- return resultBitmap;
- }
//拼图
public static Bitmap Pintu(Bitmap bitmap){
int w = bitmap.getWidth();
int h = bitmap.getHeight();
Bitmap resultBitmap = Bitmap.createBitmap(w - 4, h - 4, Bitmap.Config.ARGB_8888);
List<ImagePiece> mItemBitmaps;
//切图 3*3 得到9个碎片
mItemBitmaps = ImageSplitterUtil.splitImage(bitmap, 3);
List<ImagePiece> imagePieces = new ArrayList<ImagePiece>();
Canvas cv = new Canvas(resultBitmap);
int pieceWidth = w / 3;
int pieceHeight = h / 3;
int w2 = 0;
int h2 = 0;
//对每个碎片随机产生一种效果,并保存到数组中
for(int s = 0; s < mItemBitmaps.size(); s++){
int type = (int) (Math.random() * 13);
Bitmap tempBitmap = mItemBitmaps.get(s).getBitmap();
ImagePiece imagePiece = new ImagePiece();
imagePiece.setBitmap(pintu(tempBitmap, type));
imagePieces.add(imagePiece);
}
//在画布上将每个经过处理后的碎片重新绘制到画布上
for(int i = 0; i < imagePieces.size(); i++){
if(i == 3 || i == 6){
w2 = w2 + pieceWidth - 2;
h2 = 0;
}else if(i != 0){
h2 += pieceHeight - 2;
}
cv.drawBitmap(imagePieces.get(i).getBitmap(), w2, h2, null);
cv.save(Canvas.ALL_SAVE_FLAG);
cv.restore();
}
return resultBitmap;
}
其中的ImagePiece类如下:
[java] view plain copy
- package com.hqu.cst.imageedit.util;
- import android.graphics.Bitmap;
- /**
- * 图片碎片类
- * @author KaipingZhou
- *
- */
- public class ImagePiece
- {
- private Bitmap bitmap ;
- public ImagePiece(){
- }
- public ImagePiece(Bitmap bitmap){
- this.bitmap = bitmap;
- }
- public Bitmap getBitmap(){
- return bitmap;
- }
- public void setBitmap(Bitmap bitmap){
- this.bitmap = bitmap;
- }
- }
package com.hqu.cst.imageedit.util;
import android.graphics.Bitmap;
/**
* 图片碎片类
* @author KaipingZhou
*
*/
public class ImagePiece
{
private Bitmap bitmap ;
public ImagePiece(){
}
public ImagePiece(Bitmap bitmap){
this.bitmap = bitmap;
}
public Bitmap getBitmap(){
return bitmap;
}
public void setBitmap(Bitmap bitmap){
this.bitmap = bitmap;
}
}
在进行拼图游戏的时候也可以用到这个类,还可以增加很多方法。
另外的splitImage方法如下:
[java] view plain copy
- package com.hqu.cst.imageedit.util;
- import java.util.ArrayList;
- import java.util.List;
- import android.graphics.Bitmap;
- /**
- * 图片切块工具
- * @author KaipingZhou
- *
- */
- public class ImageSplitterUtil
- {
- /**
- * @param bitmap
- * @param piece
- * 切成piece*piece块
- * @return List<ImagePiece>
- */
- public static List<ImagePiece> splitImage(Bitmap bitmap, int piece)
- {
- List<ImagePiece> imagePieces = new ArrayList<ImagePiece>();
- int width = bitmap.getWidth();
- int height = bitmap.getHeight();
- int pieceWidth = width / piece;
- int pieceHeight = height / piece;
- for (int i = 0; i < piece; i++){
- for (int j = 0; j < piece; j++){
- ImagePiece imagePiece = new ImagePiece();
- int x = i * pieceWidth;
- int y = j * pieceHeight;
- imagePiece.setBitmap(Bitmap.createBitmap(bitmap, x, y,
- pieceWidth, pieceHeight));
- imagePieces.add(imagePiece);
- }
- }
- return imagePieces;
- }
- }
package com.hqu.cst.imageedit.util;
import java.util.ArrayList;
import java.util.List;
import android.graphics.Bitmap;
/**
* 图片切块工具
* @author KaipingZhou
*
*/
public class ImageSplitterUtil
{
/**
* @param bitmap
* @param piece
* 切成piece*piece块
* @return List<ImagePiece>
*/
public static List<ImagePiece> splitImage(Bitmap bitmap, int piece)
{
List<ImagePiece> imagePieces = new ArrayList<ImagePiece>();
int width = bitmap.getWidth();
int height = bitmap.getHeight();
int pieceWidth = width / piece;
int pieceHeight = height / piece;
for (int i = 0; i < piece; i++){
for (int j = 0; j < piece; j++){
ImagePiece imagePiece = new ImagePiece();
int x = i * pieceWidth;
int y = j * pieceHeight;
imagePiece.setBitmap(Bitmap.createBitmap(bitmap, x, y,
pieceWidth, pieceHeight));
imagePieces.add(imagePiece);
}
}
return imagePieces;
}
}
这个方法不是死的,通过传入切块的数量去动态的调整切块的大小。
其中ImagePiece.setBitmap(pintu(tempBitmap, type));其中拼图方法就是集合了几种不同滤镜的一个方法。
其效果如下:
效果并不是美观,是因为我的pintu()方法中的滤镜种类比较多,而且颜色差异比较大,如果只选择两种到四种滤镜,效果应该会好很多。