Android图像缩放



//从资源中获取位图
    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);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值