现在有的应用在用户头像上使用圆形的imageview,比如百度贴吧。实现上并不困难,需要写一个类继承自ImageView,重写ondraw方法。
代码如下:
public class CircleImage extends ImageView {
/**
* 3个构造函数
* @param context
*/
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);
}
/**
* 重写的ondraw方法
*/
@Override
protected void onDraw(Canvas canvas) {
Drawable drawable = getDrawable();
if (drawable == null) {
return;
}
if (getWidth() == 0 || getHeight() == 0) {
return;
}
Bitmap b = ((BitmapDrawable) drawable).getBitmap();
Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);