Android图像处理之绘制圆形、三角形及扇形的头像-创新互联
前言
创新互联专注于孝义企业网站建设,成都响应式网站建设,商城网站定制开发。孝义网站建设公司,为孝义等地区提供建站服务。全流程定制网站设计,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务相信大家在Android日常开发中,绘制圆形和绘制图片都是很容易的事情,但是绘制圆形图片就有点难倒人了。以前为了偷懒就直接去github上找一个开源项目,后来才发现绘制圆形图片其实也是很简单的事。
绘制圆形图片也需要两个步骤:
绘制圆形和绘制图片,只不过要让它们取并集,得到的结果就是一张圆形图片了。
直接上代码:
public class CircleImageView extends View { private Paint mPaint; private Paint mTargetPaint; private Bitmap mSourceBitmap; private Bitmap mTargetBitmap; private Canvas mTargetCanvas; private int mWidth; private int mHeight; public CircleImageView(Context context) { this(context, null); } public CircleImageView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public CircleImageView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTargetPaint = new Paint(Paint.ANTI_ALIAS_FLAG); mTargetPaint.setXfermode(new PorterDuffXfermode(SRC_IN)); mSourceBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.xiaojiangshi); mTargetBitmap = Bitmap.createBitmap(mSourceBitmap.getWidth(), mSourceBitmap.getHeight(), Bitmap.Config.ARGB_8888); mTargetCanvas = new Canvas(mTargetBitmap); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); mWidth = w; mHeight = h; } @Override protected void onDraw(Canvas canvas) { // 生成圆形Bitmap过程. int radius = Math.min(mWidth, mHeight) / 2; // 先绘制圆形 mTargetCanvas.drawCircle(mWidth / 2, mHeight / 2, radius, mPaint); // 再绘制Bitmap mTargetCanvas.drawBitmap(mSourceBitmap, 0, 0, mTargetPaint); canvas.drawBitmap(mTargetBitmap, 0, 0, null); } }
本文名称:Android图像处理之绘制圆形、三角形及扇形的头像-创新互联
转载来于:http://azwzsj.com/article/jisec.html