获取各种常见形状的位图
有的时候我们须要圆形或者矩形的位图。比方QQ头像是圆形的,还有圆角矩形的,这些是怎么做到呢?
这涉及到Xfermode,所以有必要先看一下XFermode的概念,可參考这篇文章
http://blog.csdn.net/t12x3456/article/details/10432935
以下给出获取圆形位图的详细代码
public Bitmap getRoundBitmap(){
Paint paint = new Paint();
int color = paint.getColor();
Bitmap bmp = Bitmap.createBitmap(getWidth(), getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bmp);
canvas.<span style="color:#ff0000;">drawCircle</span>(getWidth()/2, getHeight()/2, Util.dp2px(mContext, 100), paint);
//paint.setColor(color);
paint.setXfermode(new PorterDuffXfermode(<span style="color:#ff0000;">PorterDuff.Mode.SRC_IN</span>));
canvas.drawBitmap(mBmpPhoto, saveMatrix, paint);
return bmp;
}须要什么样的图形,仅仅须要把drawCircle换成对应的方法就可以上述代码中的
Util.dp2px(mContext, 100)方法是将dp转换成像素
public static int dp2px(Context context, float dipValue) {
<span style="white-space:pre"> </span>final float scale = context.getResources().getDisplayMetrics().density;
<span style="white-space:pre"> </span>return (int) (dipValue * scale + 0.5f);
<span style="white-space:pre"> </span>}
相应的还有将sp转换成像素的
public static int sp2px(Context context, float spValue) {
<span style="white-space:pre"> </span>final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
<span style="white-space:pre"> </span>return (int) (spValue * fontScale + 0.5f);
<span style="white-space:pre"> </span>}

浙公网安备 33010602011771号