Bitmap实现圆,保存Bitmap到文件
来源:互联网 发布:av99.tv的新域名 编辑:程序博客网 时间:2024/06/05 18:02
/** * 转换图片成圆形 * @param bitmap 传入Bitmap对象 * @return */ public static Bitmap toRoundBitmap(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; 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 Rect dst = new Rect((int) dst_left, (int) dst_top, (int) dst_right, (int) dst_bottom); final RectF rectF = new RectF(dst); 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(PorterDuff.Mode.SRC_IN));// 设置两张图片相交时的模式,参考http://trylovecatch.iteye.com/blog/1189452 canvas.drawBitmap(bitmap, src, dst, paint); //以Mode.SRC_IN模式合并bitmap和已经draw了的Circle return output; } /** * 保存Bitmap到文件中 * @param bitmap * @param filePath */ public static void saveBitmap(Bitmap bitmap, String filePath) { File f = new File(filePath); FileOutputStream fos = null; try { f.createNewFile(); fos = new FileOutputStream(f); bitmap.compress(Bitmap.CompressFormat.PNG, 100, fos); fos.flush(); } catch (Exception e) { e.printStackTrace(); } finally { if (fos != null) { try { fos.close(); } catch (IOException e) { e.printStackTrace(); } } } }
<pre name="code" class="java">/** * 将图片按自己的要求缩放 * @param bitmap * @param newWidth * @param newHeight * @return */ public static Bitmap resizeBitmap(Bitmap bitmap,int newWidth,int newHeight){ int width = bitmap.getWidth(); int height = bitmap.getHeight(); float scaleW = ((float)newWidth)/width; float scaleH = ((float)newHeight)/height; Matrix matrix = new Matrix(); matrix.postScale(scaleW, scaleH); return Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix,true); }
0 0
- Bitmap实现圆,保存Bitmap到文件
- Android保存Bitmap到文件
- 使用GDI+保存BITMAP到文件
- android 保存bitmap到指定文件
- android Bitmap保存到文件(PNG)
- Bitmap保存到本地
- Bitmap保存到SD
- Bitmap转为文件保存
- Bitmap文件保存
- Android Bitmap保存到SQLite
- 将bitmap保存到sdcard
- 保存Bitmap到SD卡
- Android Bitmap保存到SQLite
- Android Bitmap保存到SQLite
- 将Bitmap保存到相册
- Android 保存bitmap到相册
- android保存bitmap到sdcard
- 保存Bitmap到本地文件夹
- NEUQ 1287: 防御导弹
- 面试题:用最有效的方式算出2*8等于几
- QMetaObject分析
- IntentFilter
- 继承
- Bitmap实现圆,保存Bitmap到文件
- 【十六】排序算法(二)--希尔排序算法
- 大数据产业链
- 深入理解并发(二)--生产者及消费者
- kNN文本分类器实现
- 再谈RESTAPI最佳实践
- 微信公众平台配置
- 将图库的图片剪切并保存
- 互联网思维