码迷,mamicode.com
首页 > 其他好文 > 详细

绘制圆形图片:登陆头像

时间:2015-04-17 11:37:48      阅读:85      评论:0      收藏:0      [点我收藏+]

标签:imageview   圆形图片   

代码如下:

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.Paint;

import android.graphics.PorterDuff.Mode;

import android.graphics.PorterDuffXfermode;

import android.graphics.Rect;

import android.graphics.drawable.BitmapDrawable;

import android.graphics.drawable.Drawable;

import android.util.AttributeSet;

import android.widget.ImageView;


public class CircleImage extends ImageView {


public CircleImage(Context context) {

super(context);

}


public CircleImage(Context context, AttributeSet attrs) {

super(context, attrs);

}


public CircleImage(Context context, AttributeSet attrs, int defStyle) {

super(context, attrs, defStyle);

}


@Override

protected void onDraw(Canvas canvas) {

Drawable drawable = getDrawable();

if (drawable == null) {

return;

}


if (getWidth() == 0 || getHeight() == 0) {

return;

}

BitmapDrawable bd = (BitmapDrawable) drawable;

Bitmap b = bd.getBitmap();

Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);

Bitmap roundBitmap = getCroppedBitmap(bitmap, getWidth());

canvas.drawBitmap(roundBitmap, 0, 0, null);

}


/*

* 对Bitmap裁剪,使其变成圆形

*/

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {

Bitmap sbmp;

if (bmp.getWidth() != radius || bmp.getHeight() != radius)

sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);

else

sbmp = bmp;


Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(),

Bitmap.Config.ARGB_8888);

final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());


Paint paint = new Paint();

paint.setAntiAlias(true);

paint.setFilterBitmap(true);

paint.setDither(true);

paint.setColor(Color.parseColor("#BAB399"));


Canvas c = new Canvas(output);

c.drawARGB(0, 0, 0, 0);

c.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f,

sbmp.getWidth() / 2 + 0.1f, paint);

paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));

c.drawBitmap(sbmp, rect, rect, paint);

return output;

}


}


绘制圆形图片:登陆头像

标签:imageview   圆形图片   

原文地址:http://blog.csdn.net/sweiqin/article/details/45083257

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!