Android Bitmap 相关操作

Android系列

常见的几个操作:缩放,裁剪,旋转,偏移

    

很多操作需要 Matrix 来支持;Matrix 通过矩阵来处理位图,计算出各个像素点的位置,从而把bitmap显示出来。
matrix里有一个3x3的矩阵,用于图像处理:

MSCALE_X MSKEW_X  MTRANS_X
MSKEW_Y MSCALE_Y MTRANS_Y
MPERSP_0 MPERSP_1 MPERSP_2

根据变量名能猜出具体的用途:
缩放X 偏移X 平移X
偏移Y 缩放Y 平移Y
透视0 透视1 透视2

matrix的操作有set,pre和post;set能够直接设置矩阵中的数值;pre类似于矩阵左乘;post类似与矩阵中的右乘

原bitmap经过计算后,会重新生成一张bitmap

代码片段:

    /**
* 根据给定的宽和高进行拉伸
*
* @param origin 原图
* @param newWidth 新图的宽
* @param newHeight 新图的高
* @return new Bitmap
*/
private Bitmap scaleBitmap(Bitmap origin, int newWidth, int newHeight) {
if (origin == null) {
return null;
}
int height = origin.getHeight();
int width = origin.getWidth();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);// 使用后乘
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (!origin.isRecycled()) {
origin.recycle();
}
return newBM;
} /**
* 按比例缩放图片
*
* @param origin 原图
* @param ratio 比例
* @return 新的bitmap
*/
private Bitmap scaleBitmap(Bitmap origin, float ratio) {
if (origin == null) {
return null;
}
int width = origin.getWidth();
int height = origin.getHeight();
Matrix matrix = new Matrix();
matrix.preScale(ratio, ratio);
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (newBM.equals(origin)) {
return newBM;
}
origin.recycle();
return newBM;
} /**
* 裁剪
*
* @param bitmap 原图
* @return 裁剪后的图像
*/
private Bitmap cropBitmap(Bitmap bitmap) {
int w = bitmap.getWidth(); // 得到图片的宽,高
int h = bitmap.getHeight();
int cropWidth = w >= h ? h : w;// 裁切后所取的正方形区域边长
cropWidth /= 2;
int cropHeight = (int) (cropWidth / 1.2);
return Bitmap.createBitmap(bitmap, w / 3, 0, cropWidth, cropHeight, null, false);
} /**
* 选择变换
*
* @param origin 原图
* @param alpha 旋转角度,可正可负
* @return 旋转后的图片
*/
private Bitmap rotateBitmap(Bitmap origin, float alpha) {
if (origin == null) {
return null;
}
int width = origin.getWidth();
int height = origin.getHeight();
Matrix matrix = new Matrix();
matrix.setRotate(alpha);
// 围绕原地进行旋转
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (newBM.equals(origin)) {
return newBM;
}
origin.recycle();
return newBM;
} /**
* 偏移效果
* @param origin 原图
* @return 偏移后的bitmap
*/
private Bitmap skewBitmap(Bitmap origin) {
if (origin == null) {
return null;
}
int width = origin.getWidth();
int height = origin.getHeight();
Matrix matrix = new Matrix();
matrix.postSkew(-0.6f, -0.3f);
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (newBM.equals(origin)) {
return newBM;
}
origin.recycle();
return newBM;
}

按钮的操作定义:

    @Override
public void onClick(View v) {
Bitmap originBM = BitmapFactory.decodeResource(getResources(),
R.drawable.littleboygreen_x128);
switch (v.getId()) {
case R.id.btn1: {// 按尺寸缩放
effectTextView.setText(R.string.scale);
Bitmap nBM = scaleBitmap(originBM, 100, 72);
effectView.setImageBitmap(nBM);
break;
}
case R.id.btn2: {// 按比例缩放,每次点击缩放比例都会不同
effectTextView.setText(R.string.scale_ratio);
if (ratio < 3) {
ratio += 0.05f;
} else {
ratio = 0.1f;
}
Bitmap nBM = scaleBitmap(originBM, ratio);
effectView.setImageBitmap(nBM);
break;
}
case R.id.btn3: {// 裁剪
effectTextView.setText("剪个头");
Bitmap cropBitmap = cropBitmap(originBM);
effectView.setImageBitmap(cropBitmap);
break;
}
case R.id.btn4: {// 顺时针旋转效果;每次点击更新旋转角度
if (alpha < 345) {
alpha += 15;
} else {
alpha = 0;
}
effectTextView.setText("旋转");
Bitmap rotateBitmap = rotateBitmap(originBM, alpha);
effectView.setImageBitmap(rotateBitmap);
break;
}
case R.id.btn5: {// 逆时针旋转效果;每次点击更新旋转角度
if (beta > 15) {
beta -= 15;
} else {
beta = 360;
}
effectTextView.setText("旋转");
Bitmap rotateBitmap = rotateBitmap(originBM, beta);
effectView.setImageBitmap(rotateBitmap);
break;
}
case R.id.btn6: {// 偏移效果;偏移量在方法中
Bitmap skewBM = skewBitmap(originBM);
effectView.setImageBitmap(skewBM);
break;
}
}
}

遇到的问题

Matrix matrix = new Matrix();
matrix.preScale(ratio, ratio);// 当 ratio=1,下面的 newBM 将会等价于 origin
Bitmap newBM = Bitmap.createBitmap(origin, 0, 0, width, height, matrix, false);
if (!origin.isRecycled()) {
origin.recycle();
}

log如下,当ratio=1时,新bitmap和旧的bitmap同一地址

11-27 05:27:16.086 16723-16723/? D/rust: originBitmap = android.graphics.Bitmap@1e8849e
11-27 05:27:16.086 16723-16723/? D/rust: newBitmap = android.graphics.Bitmap@1e8849e

更多请参见 https://rustfisher.com

Android Bitmap 常见的几个操作:缩放,裁剪,旋转,偏移的更多相关文章

  1. Android -- 图片处理, 画画板,缩放,旋转,平移,镜面,倒影,图片合成,颜色处理

    1. 画画板 示例代码 public class MainActivity extends Activity { private ImageView iv; private Bitmap baseBi ...

  2. Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas

    Android应用程序开发之图片操作(一)——Bitmap,surfaceview,imageview,Canvas   1,Bitmap对象的获取 首先说一下Bitmap,Bitmap是Androi ...

  3. 【读书笔记《Android游戏编程之从零开始》】14.游戏开发基础(Bitmap 位图的渲染与操作)

    Bitmap 是图形类,Android 系统支持的图片格式有 png.jpg.bmp 等. 对位图操作在游戏中是很重要的知识点,比如游戏中需要两张除了大小之外其他完全相同的图,那么如果会对位图进行缩放 ...

  4. Android处理Bitmap使其能够不失真等比缩放裁剪后显示在ImageView上

    Android开发过程中,我们有时需要动态得显示一些图片,并且这些图片的大小差距会十分大,如果需求并不是需要图片完整显示,但是需要不失真,并且要图片中间部分的情况下,我们需要做一系列处理,因为这个时候 ...

  5. Android Bitmap 载入与像素操作

    Android Bitmap 载入与像素操作 一:载入与像素读写 在Android SDK中,图像的像素读写能够通过getPixel与setPixel两个Bitmap的API实现. Bitmap AP ...

  6. [翻译]开发文档:android Bitmap的高效使用

    内容概述 本文内容来自开发文档"Traning > Displaying Bitmaps Efficiently",包括大尺寸Bitmap的高效加载,图片的异步加载和数据缓存 ...

  7. Android动画及图片的缩放和旋转

    Android动画有2种,一种是Tween Animation,另一种是Frame Animation,先说说Tween动画吧. Tween动画是对视图对象中的内容进行一系列简单的转换,比如位置的移动 ...

  8. 二十一、Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读

    术语和概念 屏幕尺寸 屏幕的物理尺寸,以屏幕的对角线长度作为依据(比如 2.8寸, 3.5寸). 简而言之, Android把所有的屏幕尺寸简化为三大类:大,正常,和小. 程序可以针对这三种尺寸的屏幕 ...

  9. Android上常见度量单位【xdpi、hdpi、mdpi、ldpi】解读

    术语和概念  屏幕尺寸  屏幕的物理尺寸,以屏幕的对角线长度作为依据(比如 2.8寸, 3.5寸).  简而言之, Android把所有的屏幕尺寸简化为三大类:大,正常,和小.  程序可以针对这三种尺 ...

随机推荐

  1. JVM点滴

    JVM java拥有GC,为什么还会内存泄漏? 理解什么是内存泄漏: Java中的内存泄露,广义并通俗的说,就是:不再会被使用的对象的内存不能被回收,就是内存泄露. Java为了简化编程工作,对于不再 ...

  2. Java对【JSON数据的解析】--Gson解析法

    Gson和fastjson分别为谷歌和阿里巴巴对JSON数据进行处理封装的jar包 两者异同点: 相同点:都是根据JSON数据创建相应的类 不同点: 1.调用方式区别 谷歌:方法都是非静态的,需要先创 ...

  3. java设计模式之 装饰器模式

    装饰器模式 装饰器模式(Decorator Pattern)允许向一个现有的对象添加新的功能,同时又不改变其结构. 这种类型的设计模式属于结构型模式,它是作为现有的类的一个包装. 这种模式创建了一个装 ...

  4. mysql之 MySQL 主从基于 GTID 复制原理概述

    一. 什么是GTID ( Global transaction identifiers ):MySQL-5.6.2开始支持,MySQL-5.6.10后完善,GTID 分成两部分,一部分是服务的UUid ...

  5. Dojo初探之4:dojo的event(鼠标/键盘)事件绑定操作(基于dojo1.11.2版本)

    前言: 上一章详解了dojo的dom/query操作,本章基于dom/query基础上进行事件绑定操作 dojo的事件 dojo的事件绑定操作分为鼠标和键盘两种进行详解 1.鼠标事件 我们沿用上一章中 ...

  6. LANMP一键安装包 版本服务任你选 可安装单一服务

    介绍与使用 更多内容请到 乌龟运维 wuguiyunwei.com 请保证在系统原有yum源文件存在的情况下运行此脚本 以下以centos7.3为例: 下面以安装LNMP为例: ? 1 wget ht ...

  7. centos5.5下mangodb启动报错glibc

    mangodb启动报错glibc找不到(centos5.5) 报错形式 [root@test-172-16-0-139-ip mongodb-server]# /data/mongodb-server ...

  8. 数据结构与算法1-2 C语言运行时间检测算法

    #include <stdio.h> #include <math.h> #include <time.h> clock_t start,stop; #define ...

  9. 用php+mysql+ajax实现淘宝客服或阿里旺旺聊天功能 之 后台页面

    在上一篇随笔中,我们已经看了如何实现前台的对话功能:前台我限定了店主只有一人,店铺只有一个,所有比较单一,但后台就不一样了,而后台更像是我们常见的聊天软件:当然,前台也应该实现这种效果,但原理懂了,可 ...

  10. vue项目构建与实战

    关于 微信公众号:前端呼啦圈(Love-FED) 我的博客:劳卜的博客 知乎专栏:前端呼啦圈 前言 由于vue相对来说比较平缓的学习过程和新颖的技术思路,使其受到了广大前后端开发者的青睐,同时其通俗易 ...