# android 遮罩制作 圆形（ 多边形）头像

</pre>看到QQ上的圆形头头像，是否有种模仿的冲动呢：</p><p>  下面给出一个声称多边形Bitmap的方法：</p><p>  <img src="https://img-blog.csdn.net/20150327180625749?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvc2hhbmd4aW5sZWk4OA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /><pre name="code" class="java">private Bitmap createStarPhoto(int x, int y, Bitmap image)
{
//根据源文件新建一个darwable对象
Drawable imageDrawable = new BitmapDrawable(image);

// 新建一个新的输出图片
Bitmap output = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(output);

// 新建一个矩形
RectF outerRect = new RectF(0, 0, x, y);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.RED);
Path path = new Path();

//绘制三角形
//path.moveTo(0, 0);
//path.lineTo(320, 250);
//path.lineTo(400, 0);

//绘制正无边形
long tmpX,tmpY;
path.moveTo(200, 200);// 此点为多边形的起点
for(int i=0;i<=5;i++)
{
tmpX =(long)(200+200*Math.sin((i*72+36)*2*Math.PI/360));
tmpY =(long)(200+200*Math.cos((i*72+36)*2*Math.PI/360));
path.lineTo(tmpX, tmpY);
}
path.close(); // 使这些点构成封闭的多边形
canvas.drawPath(path, paint);

//回执圆形
canvas.drawCircle(40, 40, 40, paint);

// 将源图片绘制到这个圆角矩形上
// 产生一个红色的圆角矩形
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
imageDrawable.setBounds(0, 0, x, y);
canvas.saveLayer(outerRect, paint, Canvas.ALL_SAVE_FLAG);
imageDrawable.draw(canvas);
canvas.restore();

return output;
}