【重要】Bitmap 花式耍法
来源:互联网 发布:pic12数据手册 编辑:程序博客网 时间:2024/04/30 06:08
源地址,很强大,这里摘录一些Android Bitmap面面观
1 Matrix 变形
// Matrix matrix = new Matrix();
// 每一种变化都包括set,pre,post三种,分别为设置、矩阵先乘、矩阵后乘。
平移:matrix.setTranslate()
缩放:matrix.setScale()
旋转:matrix.setRotate()
斜切:matrix.setSkew()
旋转
借助Matrix的postRotate方法旋转一定角度
Matrix matrix = new Matrix();// angle为旋转的角度matrix.postRotate(angle);Bitmap rotatedBitmap = Bitmap.createBitmap(originBitmap, 0, 0, originBitmap.getWidth(), originBitmap.getHeight(), matrix, true);
写好Matrix之后穿进去,创建对应Bitmap
缩放
Matrix matrix = new Matrix();// scaleX,scaleY分别为为水平和垂直方向上缩放的比例matrix.postScale(scaleX, scaleY);Bitmap scaledBitmap = Bitmap.createBitmap(originBitmap, 0, 0, originBitmap.getWidth(), originBitmap.getHeight(), matrix, true);
Bitmap本身也带了一个缩放方法,不过是把bitmap缩放到目标大小,原理也是用Matrix,我们封装一下:
// 水平和宽度缩放到指定大小,注意,这种情况下图片很容易变形Bitmap scaledBitmap = Bitmap.createScaledBitmap(originBitmap, dstWidth, dstHeight, true);
还有inSimpleSize压缩
裁剪
图片的裁剪的应用场景还是很多的:头像剪切,照片裁剪,圆角,圆形等等。
矩形
矩阵形状的裁剪比较简单,直接用createBitmap方法即可:
Canvas canvas = new Canvas(originBitmap);draw(canvas);// 确定裁剪的位置和裁剪的大小Bitmap clipBitmap = Bitmap.createBitmap(originBitmap, left, top, clipWidth, clipHeight);
圆角
对于圆角我们需要借助Xfermode和PorterDuffXfermode,把圆角矩阵套在原Bitmap上取交集得到圆角Bitmap。
private Bitmap cicleBitmap(Bitmap bitmap){ int width = bitmap.getWidth(); int height = bitmap.getHeight(); float roundPx; float left, top, right, bottom, dst_left, dst_top, dst_right, dst_bottom; // 这里就是在矩形中取一个正方形(正方形包含圆形),这里就是要确定正方形的四个顶点,很简单的数学知识,我不解释了 // left ,rigjt的值是距离y轴的距离,top,right是距离x轴的距离 if (width <= height) { roundPx = width / 2; left = 0; top = 0; right = width; bottom = width; height = width; dst_left = 0; dst_top = 0; dst_right = width; dst_bottom = width; } else { roundPx = height / 2; float clip = (width - height) / 2; left = clip; right = width - clip; top = 0; bottom = height; width = height; dst_left = 0; dst_top = 0; dst_right = height; dst_bottom = height; } Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(output); final int color = 0xff424242; final Paint paint = new Paint(); final Rect src = new Rect((int) left, (int) top, (int) right, (int) bottom); final RectF dst = new RectF( dst_left, dst_top, dst_right, dst_bottom); paint.setAntiAlias(true);// 设置画笔无锯齿 // canvas.drawARGB(0, 0, 0, 0); // 填充整个Canvas // paint.setColor(color); // 以下有两种方法画圆,drawRounRect和drawCircle // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);// 画圆角矩形,第一个参数为图形显示区域,第二个参数和第三个参数分别是水平圆角半径和垂直圆角半径。 // 前面两个参数是圆的中心点,所以这个圆画的位置是根据前面计算的来画的,与下面的相呼应,形成重叠 canvas.drawCircle(roundPx, roundPx, roundPx, paint); // 在原图上画了一个圈(下图) paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452 // 又加了一层,在圈上画了一个图(上图) /** * src May be null. The subset of the bitmap to be drawn 要在原图的那个区域画图 * 前面算好了,所以src的区域其实是个正方形,并且在矩形的中间,所以画图的地方就是中央 *dst The rectangle that the bitmap will be scaled/translated to fit into 要显示多大的区域(有缩放效果) * */ canvas.drawBitmap(bitmap, src, dst, paint); //以Mode.SRC_IN模式合并bitmap和已经draw了的Circle return output; }
0 0
- 【重要】Bitmap 花式耍法
- Bitmap重要属性
- 花式GCD
- bitmap造成OOM异常的解决方法-(重要文档)
- 制作花式面包
- 个性酒吧花式调酒
- vim花式设置
- 花式杀死进程
- UITableviewCell的花式操作
- Android Toast花式使用
- Arduino花式点灯
- Python 花式索引
- arduino花式点灯续
- 花式找LCA
- 表格花式效果
- 表格花式效果
- ACM花式走楼梯
- Android Snackbar花式使用指南
- Java 环境变量参数
- Android开发一些学习经验
- spring aop表达式详解
- BoneCP数据源配置详解
- TcxgridDbLayoutView使用有感
- 【重要】Bitmap 花式耍法
- x86处理器中的CS与IP寄存器
- linux kernel current宏
- Android使用UncaughtExceptionHandler捕获异常
- MFC运行时动态类实现
- go简易tcp socket客户端和服务器
- 从root后的Android手机中取出数据库数据
- Android Json解析测试对话框
- Android开发之网络通讯详解