圆角头像实现工具类
2016-06-07 16:32
585 查看
本篇介绍使用canvas实现圆角头像
先来看下效果图首先介绍一些需要用到的方法
drawBitmap基本用法
1、基本的绘制图片方法//Bitmap:图片对象,left:偏移左边的位置,top: 偏移顶部的位置 drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
2、对图片剪接和限定显示区域
一
drawBitmap(Bitmap bitmap, Rect src, RectF dst, Paint paint);
Rect src: 是对图片进行裁截,若是空null则显示整个图片 RectF dst:是图片在Canvas画布中显示的区域,大于src则把src的裁截区放大,小于src则把src的裁截区缩小
二
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
这个方法我看了很久,并做了一些测试,终于弄明白了。 这个方法可以用来剪辑一张图片的一部分,即当我们把一组图片做成一张时,我们可以用此方法来剪辑出单个图片。 bitmap的默认坐标是0,0.我们可以在此基础上剪图片。 矩形src为我们所剪辑的图片的包围框,即你所剪的图片,如果为空,就是整张图片。 矩形dst容纳你所剪的图片,然后根据此矩形的位置设置图片的位置。此参数不能为空。 当你剪的图片大小大于dst时,多余的部分将不会显示。 也就是说src是裁减区,对原始图的裁减区域,而dst是代表图片显示位置.
关于drawARGB(0,0,0,0)
// 将会以颜色ARBG填充整个控件的Canvas背景 mCanvas.drawARGB(122, 10, 159, 163) ;
关于drawRoundRect()
【功能说明】该方法用于在画布上绘制圆角矩形,通过指定RectF对象以及圆角半径来实现。该方法是绘制圆角矩形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆角矩形。【基本语法】
public void drawRoundRect (RectF rect, float rx, float ry, Paint paint)
参数说明
rect:RectF对象。
rx:x方向上的圆角半径。
ry:y方向上的圆角半径。
paint:绘制时所使用的画笔。
下面就是源代码了
import android.graphics.AvoidXfermode.Mode; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.PorterDuffXfermode; import android.graphics.Rect; import android.graphics.RectF; public class ImageHelper { /** * @author 马世豪 转换图片成圆形 * * @param bitmap * 传入Bitmap对象 * @return */ public static Bitmap toRoundBitmap(Bitmap 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; top = 0; bottom = width; left = 0; right = 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, 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); // 将会以颜色ARBG填充整个控件的Canvas背景 canvas.drawARGB(0, 0, 0, 0); paint.setColor(color); // 通过canvas 来画圆,传进去一个圆,圆的 X轴半径,Y轴半径 canvas.drawRoundRect(rectF, roundPx, roundPx, paint); // 设置绘制模式 paint.setXfermode(new PorterDuffXfermode( android.graphics.PorterDuff.Mode.SRC_IN)); // 将bitmip绘制到canvas中 canvas.drawBitmap(bitmap, 0, 0, paint); return output; } }
在activity中调用
head = (ImageView)findViewById(R.id.head); Resources res = getResources(); Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.mashihao); head.setImageBitmap(ImageHelper.toRoundBitmap(bitmap));
参考了网上好多的资料
Android Rect和RectF的区别drawRoundRect方法:绘制圆角矩形
下一篇的话会写一个自定义view实现当前功能
相关文章推荐
- ARM 中断状态和SVC状态的堆栈切换 (异常)【转】
- 分析分布式服务框架
- 探索《How Tomcat Works》心得(四)生命周期 Lifecycle
- unity如何使用ios模拟器运行(需要xcode)
- [GitHub]第三讲:简单分支操作
- LinkedList和ArrayList的区别
- java中基本输入输出流的解释
- 小记
- Burpsuite视频教程(更新中)
- [GitHub]第三讲:简单分支操作
- elasticseach multi-field的实际用途
- hdu1007
- Linux安装配置JDK
- PHP中提供的用于MySQL的主要API是什么?(官网整理)
- 如何使cmd窗口正确显示utf-8编码的文字
- 1、Zabbix RPM包安装
- IOS使用HTTP方式登录
- The Import android.support.v7 cannot be resolved
- easyui远程ajax验证
- Centos Linux 虚拟机克隆后无法上网的问题(学习)