//从资源中获取位图
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.pic2);
Bitmap bitmap = bd.getBitmap();
##### BitmapFactory
解码图像信息(宽高、像素点、裁剪)可以借助裁剪功能处理大图加载的OOM问题(Out Of Memory)
BitmapFactory.Options opt = new BitmapFactory.Options();
//先取宽高
opt.inJustDecodeBounds = true;
//解析宽高(解析出的bitmap为null)
BitmapFactory.decodeFile(path, opt);
int oWidth = opt.outWidth;
int oHeight = opt.outHeight;
int scW = (int) Math.ceil(oWidth / maxWidth);
int scH = (int) Math.ceil(oHeight / maxHeight);
if (scW > 1 || scH > 1) {
if (scH > scW) {
opt.inSampleSize = scH;
//必须要大于1,必须是整数,最终显示的是1/opt.inSampleSize
} else {
opt.inSampleSize = scW;
}
}
//解码图像,要获取像素点
opt.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeFile(path, opt);
图片保存
FileOutputStream out = new FileOutputStream("/mnt/sdcard/pic_small.png");
//格式,质量,输出位置
mBitmap.compress(Bitmap.CompressFormat.JPEG,80,out);
out.flush();
out.close();
#### 图像绘制
//绘制图像(位图对象,位图左上角x坐标,位图左上角y坐标)
canvas.drawBitmap(mBitmap, 50, 10, null);
//第二个参数表示图像上的区域,可以null,null表示全图
//第三个参数表示画布上显示图像的区域
canvas.drawBitmap(gameBitmap, bitmapRect, showRect, null);
#### 图像变换
需要有Matrix对象来处理
Matrix mMatrix = new Matrix(); //角度0 缩放倍数1 偏移0
set:
设置一个新状态
mMatrix.setTranslate(100,200);
post:
基于上一个状态累加新状态
mMatrix.postRotate(90);
pre:
前置状态,如需要对图片进行平移(100,200)放大2倍
mMatrix.postTranslate(100,200);
mMatrix.postScare(2,2);
mMatrix.postScare(2,2);
mMatrix.preTranslate(100,200);
//基于矩阵绘制图像
canvas.drawBitmap(mBitmap,mMatrix,null);
#### 多点触控
int action = event.getAction();
switch (action & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
//单个触控按下
break;
case MotionEvent.ACTION_POINTER_DOWN:
//多个触控点按下
break;
case MotionEvent.ACTION_MOVE:
按下之后离开之前都会触发
break;
case MotionEvent.ACTION_UP:
单个触控离开
break;
case MotionEvent.ACTION_POINTER_UP:
多个触控离开
break;
}
获取精确的触控点个数
int count = event.getPointerCount(); //获取总个数
for(int i = 0 ; i < count ; i++){
int x = event.getX(i);
int y = event.getY(i);
}