圓形頭像白邊加陰影

先上效果圖:

上一段源碼

```

public class CircularImageView extends ImageView

{

private int borderWidth = 4;

private int viewWidth;

private int viewHeight;

private Bitmap image;

private Paint paint;

private Paint paintBorder;

private BitmapShader shader;

public CircularImageView(Context context)

{

super(context);

setup();

}

public CircularImageView(Context context, AttributeSet attrs)

{

super(context, attrs);

setup();

}

public CircularImageView(Context context, AttributeSet attrs, int defStyle)

{

super(context, attrs, defStyle);

setup();

}

private void setup()

{

// init paint

paint = new Paint();

paint.setAntiAlias(true);

paintBorder = new Paint();

setBorderColor(Color.WHITE);

paintBorder.setAntiAlias(true);

this.setLayerType(LAYER_TYPE_SOFTWARE, paintBorder);

paintBorder.setShadowLayer(4.0f, 0.0f, 2.0f, Color.BLACK);

}

public void setBorderWidth(int borderWidth)

{

this.borderWidth = borderWidth;

this.invalidate();

}

public void setBorderColor(int borderColor)

{

if (paintBorder != null)

paintBorder.setColor(borderColor);

this.invalidate();

}

private void loadBitmap()

{

BitmapDrawable bitmapDrawable = (BitmapDrawable) this.getDrawable();

if (bitmapDrawable != null)

image = bitmapDrawable.getBitmap();

}

@SuppressLint("DrawAllocation")

@Override

public void onDraw(Canvas canvas)

{

// load the bitmap

loadBitmap();

// init shader

if (image != null)

{

shader = new BitmapShader(Bitmap.createScaledBitmap(image, canvas.getWidth(), canvas.getHeight(), false), Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);

paint.setShader(shader);

int circleCenter = viewWidth / 2;

// circleCenter is the x or y of the view's center

// radius is the radius in pixels of the cirle to be drawn

// paint contains the shader that will texture the shape

canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter + borderWidth - 4.0f, paintBorder);

canvas.drawCircle(circleCenter + borderWidth, circleCenter + borderWidth, circleCenter - 4.0f, paint);

}

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)

{

int width = measureWidth(widthMeasureSpec);

int height = measureHeight(heightMeasureSpec, widthMeasureSpec);

viewWidth = width - (borderWidth * 2);

viewHeight = height - (borderWidth * 2);

setMeasuredDimension(width, height);

}

private int measureWidth(int measureSpec)

{

int result = 0;

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY)

{

// We were told how big to be

result = specSize;

}

else

{

// Measure the text

result = viewWidth;

}

return result;

}

private int measureHeight(int measureSpecHeight, int measureSpecWidth)

{

int result = 0;

int specMode = MeasureSpec.getMode(measureSpecHeight);

int specSize = MeasureSpec.getSize(measureSpecHeight);

if (specMode == MeasureSpec.EXACTLY)

{

// We were told how big to be

result = specSize;

}

else

{

// Measure the text (beware: ascent is a negative number)

result = viewHeight;

}

return (result + 2);

}

}


/**

* 把bitmap轉(zhuǎn)成圓形

*/

publicBitmaptoRoundBitmap(Bitmap bitmap) {

intwidth = bitmap.getWidth();

intheight = bitmap.getHeight();

intr =0;

//取最短邊做邊長

if(width < height) {

r = width;

}else{

r = height;

}

//構(gòu)建一個(gè)bitmap

Bitmap backgroundBm = Bitmap.createBitmap(width,height,Bitmap.Config.ARGB_8888);

//new一個(gè)Canvas臂聋,在backgroundBmp上畫圖

Canvas canvas =newCanvas(backgroundBm);

Paint p =newPaint();

//設(shè)置邊緣光滑立宜,去掉鋸齒

p.setAntiAlias(true);

RectF rect =newRectF(0,0,r,r);

//通過制定的rect畫一個(gè)圓角矩形鳄哭,當(dāng)圓角X軸方向的半徑等于Y軸方向的半徑時(shí)店诗,

//且都等于r/2時(shí)阳柔,畫出來的圓角矩形就是圓

canvas.drawRoundRect(rect,r /2,r /2,p);

//設(shè)置當(dāng)兩個(gè)圖形相交時(shí)的模式桥爽,SRC_IN為取SRC圖形相交的部分杠览,多余的將被去掉

p.setXfermode(newPorterDuffXfermode(PorterDuff.Mode.SRC_IN));

//canvas將bitmap畫在backgroundBmp上

canvas.drawBitmap(bitmap, null,rect,p);

returnbackgroundBm;

}

```



首次寫文章下次修改报账。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末蝴罪,一起剝皮案震驚了整個(gè)濱河市董济,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌要门,老刑警劉巖虏肾,帶你破解...
    沈念sama閱讀 218,546評(píng)論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件廓啊,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡封豪,警方通過查閱死者的電腦和手機(jī)谴轮,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評(píng)論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來吹埠,“玉大人第步,你說我怎么就攤上這事≡道牛” “怎么了粘都?”我有些...
    開封第一講書人閱讀 164,911評(píng)論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長刷袍。 經(jīng)常有香客問我翩隧,道長,這世上最難降的妖魔是什么做个? 我笑而不...
    開封第一講書人閱讀 58,737評(píng)論 1 294
  • 正文 為了忘掉前任鸽心,我火速辦了婚禮,結(jié)果婚禮上居暖,老公的妹妹穿的比我還像新娘顽频。我一直安慰自己,他們只是感情好太闺,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,753評(píng)論 6 392
  • 文/花漫 我一把揭開白布糯景。 她就那樣靜靜地躺著,像睡著了一般省骂。 火紅的嫁衣襯著肌膚如雪蟀淮。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,598評(píng)論 1 305
  • 那天钞澳,我揣著相機(jī)與錄音怠惶,去河邊找鬼。 笑死轧粟,一個(gè)胖子當(dāng)著我的面吹牛策治,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播兰吟,決...
    沈念sama閱讀 40,338評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼通惫,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了混蔼?” 一聲冷哼從身側(cè)響起履腋,我...
    開封第一講書人閱讀 39,249評(píng)論 0 276
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后遵湖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體悔政,經(jīng)...
    沈念sama閱讀 45,696評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,888評(píng)論 3 336
  • 正文 我和宋清朗相戀三年奄侠,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了卓箫。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片载矿。...
    茶點(diǎn)故事閱讀 40,013評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡垄潮,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出闷盔,到底是詐尸還是另有隱情弯洗,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評(píng)論 5 346
  • 正文 年R本政府宣布逢勾,位于F島的核電站牡整,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏溺拱。R本人自食惡果不足惜逃贝,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,348評(píng)論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望迫摔。 院中可真熱鬧沐扳,春花似錦、人聲如沸句占。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽纱烘。三九已至杨拐,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間擂啥,已是汗流浹背哄陶。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評(píng)論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留哺壶,地道東北人屋吨。 一個(gè)月前我還...
    沈念sama閱讀 48,203評(píng)論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像变骡,于是被迫代替她去往敵國和親离赫。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,960評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容