在專案開發中,需要給使用者的頭像設定為圓形。無論獲取到什麼,都顯示為圓形頭像。自定義view寫法如下。使用的時候在xml布局中,直接使用就好。
public class mycircleimageview extends imageview
public mycircleimageview(context context, attributeset attrs)
public mycircleimageview(context context, attributeset attrs, int defstyle)
private void setcustomattributes(attributeset attrs)
@override
protected void ondraw(canvas canvas)
if (getwidth() == 0 || getheight() == 0)
this.measure(0, 0);
if (drawable.getclass() == ninepatchdrawable.class)
return;
bitmap b = ((bitmapdrawable) drawable).getbitmap();
bitmap bitmap = b.copy(bitmap.config.argb_8888, true);
if (defaultwidth == 0)
if (defaultheight == 0)
int radius = 0;
if (mborderinsidecolor != defaultcolor && mborderoutsidecolor != defaultcolor) else if (mborderinsidecolor != defaultcolor && mborderoutsidecolor == defaultcolor) else if (mborderinsidecolor == defaultcolor && mborderoutsidecolor != defaultcolor) else
bitmap roundbitmap = getcroppedroundbitmap(bitmap, radius);
canvas.drawbitmap(roundbitmap, defaultwidth / 2 - radius, defaultheight / 2 - radius, null);
}/**
* 獲取裁剪後的圓形
* @param //radius半徑
*/public bitmap getcroppedroundbitmap(bitmap bmp, int radius) else if (bmpheight < bmpwidth) else
if (squarebitmap.getwidth() != diameter || squarebitmap.getheight() != diameter) else
bitmap output = bitmap.createbitmap(scaledsrcbmp.getwidth(),
scaledsrcbmp.getheight(),
bitmap.config.argb_8888);
canvas canvas = new canvas(output);
paint paint = new paint();
rect rect = new rect(0, 0, scaledsrcbmp.getwidth(),scaledsrcbmp.getheight());
paint.setantialias(true);
paint.setfilterbitmap(true);
paint.setdither(true);
canvas.drawargb(0, 0, 0, 0);
canvas.drawcircle(scaledsrcbmp.getwidth() / 2,
scaledsrcbmp.getheight() / 2,
scaledsrcbmp.getwidth() / 2,
paint);
paint.setxfermode(new porterduffxfermode( porterduff.mode.src_in));
canvas.drawbitmap(scaledsrcbmp, rect, rect, paint);
bmp = null;
squarebitmap = null;
scaledsrcbmp = null;
return output;
}/**
* 邊緣畫圓
*/private void drawcircleborder(canvas canvas, int radius, int color)
}
Android自定義View之圓形頭像
圓形頭像是我們在開發中經常用到的控制項,下面我們就來自定義這樣乙個控制項 1.自定義circleimageview類繼承imageview package com.example.qw.circleimageview created by quwei on 2015 5 13 0013.import...
圓形頭像設計
圓形頭像嘛說到底就是張,所以自定義圓形控制項自然要繼承android原生的imgaeview,實現其中的setimagebitmap,setimagedrawable,setimageuri,setimageresource的方法。當然主要的ondraw方法也不能缺少,在其中要實現圓形頭像的繪製。這...
ios 裁剪圓形頭像 iOS中裁剪圓形頭像
void clipimage 0.載入 uiimage image uiimage imagenamed 阿狸頭像 1.開啟位圖上下文,跟尺寸一樣大 uigraphicsbeginimagecontextwithoptions image.size,no,0 2.設定圓形裁剪區域,正切與 2.1建立...