此文章摘自不同博主内容混合总结部分以测试过:
这里的切割,主要使用的是Bitmap对象的createBitmap方法。
List<ImagePiece> pieces = new ArrayList<ImagePiece>(xPiece * yPiece);
Bitmap bitmap = ;
int bitmapW = bitmap.getWidth();int bitmapH = bitmap.getHeight();
int pieceWidth = width / 3;
int pieceHeight = height / 3;
for (int i = 0; i < yPiece; i++) {
for (int j = 0; j < xPiece; j++) {
Bitmap imgBitmap = Bitmap.createBitmap(bitmap, i % 2 *bitmapW, i / 2 * bitmapH, i % 2 *bitmapW +
bitmapW, i / 2 * bitmapH + bitmapH);
}
}
public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter) {
Bitmap bitmap;
try {
bitmap = Bitmap.createBitmap(source, x, y, width, height, m, filter);
} catch (OutOfMemoryError localOutOfMemoryError) {
gc();
bitmap = Bitmap.createBitmap(source, x, y, width, height, m, filter);
}
return bitmap;
}
返回一个不可变的源位图的位图的子集,改变了可选的矩阵。新的位图可能与源相同的对象,或可能是一个副本。
它初始化与原始位图的密度。如果源位图是不可变的,请求的子集是一样的源位图本身,然后返回源位图,没有新的位图创建。
sourceBitmap: The bitmap we are subsetting 产生子位图的源位图;
x int: The x coordinate of the first pixel in source 子位图第一个像素在源位图的X坐标
y int: The y coordinate of the first pixel in source 子位图第一个像素在源位图的y坐标
width int: The number of pixels in each row 子位图每一行的像素个数
height int: The number of rows 子位图的行数
m Matrix: Optional matrix to be applied to the pixels 对像素值进行变换的可选矩阵
filter boolean: true if the source should be filtered. Only applies if the matrix contains more than just translation. 如果为true,源图要被过滤。
该参数仅在matrix包含了超过一个翻转才有效
----------------------------------------------------------------------------------------------
图片处理
public Bitmap zoomImage(Bitmap bgimage, int newWidth, int newHeight) {
// 获取这个图片的宽和高
int width = bgimage.getWidth();
int height = bgimage.getHeight();
// 创建操作图片用的matrix对象
Matrix matrix = new Matrix();
// 计算缩放率,新尺寸除原始尺寸
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// 缩放图片动作
matrix.postScale(scaleWidth, scaleHeight);
Bitmap bitmap = Bitmap.createBitmap(bgimage, 0, 0, width, height,
matrix, true);
return bitmap;
.绘制带有边框的文字,一般在游戏中起文字的美化作用
@param strMsg:绘制内容 @param g :画布 @param paint :画笔
@param setx:X轴起始坐标 @param sety:Y轴的起始坐标
@param fg:前景色@param bg:背景色
public void drawText(String strMsg, Canvas g, Paint paint, int setx,
int sety, int fg, int bg) {
paint.setColor(bg);
g.drawText(strMsg, setx + 1, sety, paint);
g.drawText(strMsg, setx, sety – 1, paint);
g.drawText(strMsg, setx, sety + 1, paint);
g.drawText(strMsg, setx – 1, sety, paint);