圆形图片的处理

圆形图片的处理小Demo(混合模式):

private static Bitmap getCircleAvater(Bitmap source, Context context) {
Bitmap target = Bitmap.createBitmap(source.getWidth(),
source.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(target);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);

    // 混合模式中的DST中的圆
    paint.setColor(Color.BLACK);
    // 取两者中较小的值并且别忘了高要除2;
    float radius = Math.min(source.getWidth(), source.getHeight() / 2);
    // 画圆
    canvas.drawCircle(source.getWidth() / 2, source.getHeight() / 2,
            radius, paint);

    // 设置混合模式
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    canvas.drawBitmap(source, 0, 0, paint);
    paint.setColor(Color.WHITE);
    paint.setStyle(Style.STROKE);
    paint.setStrokeWidth(3);
    paint.setAntiAlias(true);
    canvas.drawCircle(source.getWidth() / 2, source.getHeight() / 2,
            radius - 1, paint);
    return target;
}

你可能感兴趣的:(android代码,demo)