实现圆形图片
实现原理:
- 得到原图的宽高,计算出圆心,取图片的短边为基准半径。
- 创建空白Bitmap,大小为基准半径的正方形。
- 位移原图,使其中心点和创建的空白图中心点重合。
- 利用Android的Paint绘制叠加图,只绘制重叠部分
效果完成。
public class CircleImageView extends ImageView{
public CircleImageView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CircleImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CircleImageView(Context context) {
super(context);
}
@Override
public void setBackgroundDrawable(Drawable background) {
super.setBackgroundDrawable(getCircleDrawable(getResources(), background));
}
@Override
public void setBackgroundResource(int resid) {
//Don't worry, we don't need to override it,because it will be call
//setBackgroundDrawable(Drawable background)
super.setBackgroundResource(resid);
}
@Override
public void setImageBitmap(Bitmap bm) {
//Don't worry, we don't need to override it,because it will be call
//setImageDrawable(Drawable drawable)
super.setImageBitmap(bm);
}
@Override
public void setImageDrawable(Drawable drawable) {
super.setImageDrawable(getCircleDrawable(getResources(), drawable));
}
@Override
public void setImageURI(Uri uri) {
//cheat it, let's change the way to implement
super.setImageURI(uri);
Drawable img = getCircleDrawable(getResources(), getDrawable());
super.setImageDrawable(img);
}
@Override
public void setImageResource(int resId) {
//cheat it, let's change the way to implement
Drawable img = getCircleDrawable(getResources(), resId);
super.setImageDrawable(img);
}
private static final int SPACING_LINE = 2;
private static Paint mCirclePaint = null;
private static Paint mLinePaint = null;
private static Paint getCirclePaint(){
if(mCirclePaint == null){
mCirclePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
}
return mCirclePaint;
}
private static Paint getLinePaint(){
if(mLinePaint == null){
mLinePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mLinePaint.setStyle(Style.STROKE);
//You can use it to change the width of the line
mLinePaint.setStrokeWidth(1);
//You can use it to change the color of the line
mLinePaint.setColor(Color.BLACK);
}
return mLinePaint;
}
/** * You can call this method to generate the circular bitmap, * even if you don't use this class */
public static Bitmap getCircleBitmap(Bitmap src){
if(src == null){
return null;
}
int width = src.getWidth();
int height = src.getHeight();
int centerX = width / 2;
int centerY = height / 2;
int radius = Math.min(centerX, centerY) / 2;
Bitmap result = Bitmap.createBitmap(radius * 2, radius * 2, Config.ARGB_8888);
Canvas canvas = new Canvas(result);
canvas.drawCircle(radius, radius, radius - SPACING_LINE, getCirclePaint());
getCirclePaint().setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(src, -(centerX - radius), -(centerY - radius), getCirclePaint());
//outer
canvas.drawCircle(radius, radius, radius, getLinePaint());
//inner
canvas.drawCircle(radius, radius, radius - SPACING_LINE, getLinePaint());
//reset
getCirclePaint().setXfermode(null);
//recycle
src.recycle();
return result;
}
public static Bitmap getCircleBitmap(Drawable src){
if(src instanceof BitmapDrawable){
return getCircleBitmap(((BitmapDrawable)src).getBitmap());
}else{
//now, i don't know how to do...
throw new UnsupportedException("Unsupported");
}
}
public static Bitmap getCircleBitmap(Resources res,int id){
return getCircleBitmap(BitmapFactory.decodeResource(res, id));
}
public static Drawable getCircleDrawable(Resources res, Bitmap src){
return new BitmapDrawable(res,getCircleBitmap(src));
}
public static Drawable getCircleDrawable(Resources res, Drawable src){
return new BitmapDrawable(res,getCircleBitmap(src));
}
public static Drawable getCircleDrawable(Resources res, int id) {
return new BitmapDrawable(res, getCircleBitmap(res, id));
}
static class UnsupportedException extends RuntimeException{
private static final long serialVersionUID = 1L;
public UnsupportedException(String str){
super(str);
}
}
}
还没有评论,来说两句吧...