顯示圓形圖片CircleImageView

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Canvas;

import android.graphics.Color;

import android.graphics.ColorFilter;

import android.graphics.Paint;

import android.graphics.PorterDuff;

import android.graphics.PorterDuffXfermode;

import android.graphics.RectF;

import android.graphics.Xfermode;

import android.graphics.drawable.Drawable;

import android.util.AttributeSet;

import android.widget.ImageView;

public class CircleImageViewextends ImageView {

private Bitmapmask;

? private Paintpaint;

? private int borderColor=0xffffffff;

? private int roundWidth=0;

? private int roundHeight=0;

? private int borderWidth=0;

? private static final XfermodeMASK_XFERMODE;

? static {

PorterDuff.Mode localMode = PorterDuff.Mode.DST_IN;

? ? ? MASK_XFERMODE =new PorterDuffXfermode(localMode);

? }

public CircleImageView(Context paramContext) {

super(paramContext);

? ? ? this.setScaleType(ScaleType.CENTER_CROP);

? }

public CircleImageView(Context paramContext, AttributeSet paramAttributeSet) {

super(paramContext, paramAttributeSet);

? ? ? this.setScaleType(ScaleType.CENTER_CROP);

? }

public CircleImageView(Context paramContext,

? ? ? ? ? ? ? ? ? ? ? ? ? AttributeSet paramAttributeSet, int paramInt) {

super(paramContext, paramAttributeSet, paramInt);

? ? ? this.setScaleType(ScaleType.CENTER_CROP);

? }

private BitmapmakeDst(int w, int h)

{

Bitmap bm = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_4444);

? ? ? ? Canvas c =new Canvas(bm);

? ? ? ? Paint p =new Paint(Paint.ANTI_ALIAS_FLAG);

? ? ? ? p.setColor(Color.parseColor("#ffffffff"));

? ? ? ? //c.drawOval(new RectF(0, 0, w, h), p);

? ? ? ? c.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()), w, h, p);

? ? ? ? return bm;

? ? }

@Override

? protected void onDraw(Canvas paramCanvas) {

Drawable localDrawable = getDrawable();

? ? ? if (localDrawable ==null)

return;

? ? ? try {

initPaint();

? ? ? ? //獲取drawable的寬和高

? ? ? ? int dWidth = localDrawable.getIntrinsicWidth();

? ? ? ? int dHeight = localDrawable.getIntrinsicHeight();

? ? ? ? //創(chuàng)建bitmap

//? ? Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),Config.ARGB_8888);

//創(chuàng)建畫布

//? ? ? Canvas drawCanvas = new Canvas(bitmap);

//縮放比例

? ? ? ? float scale =1.0f;

? ? ? ? //按照bitmap的寬高读拆,以及view的寬高江场,計算縮放比例;因為設(shè)置的src寬高比例可能和imageview的寬高比例不同寥枝,這里我們不希望圖片失真伪窖;

//? ? ? if (dWidth == dHeight)

//? ? ? {

//? ? ? ? ? // 如果圖片的寬或者高與view的寬高不匹配回官,計算出需要縮放的比例甘畅;縮放后的圖片的寬高列另,一定要大于我們view的寬高芽腾;所以我們這里取大值;

//? ? ? ? ? scale =? Math.max(getWidth() * 1.0f / dWidth, getHeight()

//? ? ? ? ? ? ? ? * 1.0f / dHeight);

//? ? ? } else

//? ? ? {

//? ? ? ? ? scale = getWidth() * 1.0F / Math.min(dWidth, dHeight);

//? ? ? }

? ? ? ? scale =? Math.max(getWidth() *1.0f / dWidth, getHeight()

*1.0f / dHeight);

? ? ? ? //根據(jù)縮放比例页衙,設(shè)置bounds摊滔,相當于縮放圖片了

? ? ? ? localDrawable.setBounds(0, 0, (int) (scale * dWidth),

? ? ? ? ? ? ? (int) (scale * dHeight));

//? ? ? localDrawable.draw(drawCanvas);

? ? ? ? float f1 = getWidth();

? ? ? ? float f2 = getHeight();

? ? ? ? int i = paramCanvas.saveLayer(0.0F, 0.0F, f1, f2, null, Canvas.ALL_SAVE_FLAG);

? ? ? ? int j = getWidth();

? ? ? ? int k = getHeight();

//? ? ? localDrawable.setBounds(0, 0, j, k);

? ? ? ? localDrawable.draw(paramCanvas);

? ? ? ? if ((this.mask ==null) || (this.mask.isRecycled())) {

if(this.roundHeight==0&&this.roundWidth==0){

this.roundHeight = getHeight();

? ? ? ? ? ? ? this.roundWidth = getWidth();

? ? ? ? ? ? }

this.mask = makeDst(roundWidth, roundHeight);

? ? ? ? }

paramCanvas.drawBitmap(this.mask, 0.0F, 0.0F, this.paint);

? ? ? ? drawBorder(paramCanvas, j, k);

? ? ? ? drawPercent(paramCanvas,paint);

? ? ? ? paramCanvas.restoreToCount(i);

return;

? ? ? }catch (Exception localException) {

localException.printStackTrace();

? ? ? }

}

private void drawBorder(Canvas canvas, final int width, final int height) {

if(this.borderWidth==0){

return;

? ? ? }

Paint mBorderPaint =new Paint();

? ? ? mBorderPaint.setStyle(Paint.Style.STROKE);

? ? ? mBorderPaint.setAntiAlias(true);

? ? ? mBorderPaint.setColor(this.borderColor);

? ? ? mBorderPaint.setStrokeWidth(borderWidth);

? ? ? int step =borderWidth/2;

? ? ? canvas.drawRoundRect(new RectF(0+step,step, getWidth()-step, getHeight()-step), width-borderWidth, height-borderWidth, mBorderPaint);

? ? ? /**

* 坐標x:view寬度的一般 坐標y:view高度的一般 半徑r:因為是view的寬度-border的一半

*/

? ? ? //canvas.drawCircle(width >> 1, height >> 1, (width - border) >> 1,? ? mBorderPaint);

? }

public void setRound(int roundWidthDP,int roundHeightDP){

this.roundHeight=roundHeightDP;

? ? ? this.roundWidth = roundWidthDP;

? ? ? float density =this.getContext().getResources().getDisplayMetrics().density;

? ? ? roundWidth = (int) (roundWidth*density);

? ? ? roundHeight = (int) (roundHeight*density);

? }

/**

? * @Title: setBorderColor

? * @Description: TODO(設(shè)置邊框顏色值)

? * @param @param color? argb值 設(shè)定文件

? * @return void? ? 返回類型

? * @throws

? */

? public void setBorder(int color,int widthPX){

if(color==-1){

//? ? ? borderColor=0xffececec;

? ? ? ? borderColor=0xffffffff;

? ? ? }

else{

this.borderColor=color;

? ? ? }

this.borderWidth = widthPX;

? }

public void setBorder(int widthPX){

borderColor=0xffececec;

? ? ? this.borderWidth = widthPX;

? }

@Override

? public void setColorFilter(ColorFilter cf) {

// TODO Auto-generated method stub

? ? ? super.setColorFilter(cf);

? ? ? initPaint();

? ? ? paint.setColorFilter(cf);

? }

private void initPaint(){

if (this.paint ==null) {

Paint localPaint1 =new Paint();

? ? ? ? this.paint = localPaint1;

? ? ? ? this.paint.setFilterBitmap(false);

? ? ? ? Paint localPaint2 =this.paint;

? ? ? ? Xfermode localXfermode1 =MASK_XFERMODE;

? ? ? ? @SuppressWarnings("unused")

Xfermode localXfermode2 = localPaint2

.setXfermode(localXfermode1);

? ? ? }

}

/////////////////////////////////////////////////////////////////////////////

? ? public enum Status {

RUNNING, NONE

? ? }

private static final int DEFAULT_TEXTCOLOT =0xff0074a2;

? ? private static final int DEFAULT_TEXTSIZE =32;

? ? private float mPercent;

? ? private int percent;

? ? private BitmapmScaledBitmap;

? ? private float mLeft;

? ? private int mSpeed =10;

? ? private int mRepeatCount =0;

? ? private StatusmFlag = Status.NONE;

? ? private int mTextColot =DEFAULT_TEXTCOLOT;

? ? private int mTextSize =DEFAULT_TEXTSIZE;

? ? public void setTextColor(int color) {

mTextColot = color;

? ? }

public void setTextSize(int size) {

mTextSize = size;

? ? }

public void setPercent(float percent) {

mFlag = Status.RUNNING;

? ? ? ? mPercent = percent;

? ? ? ? this.percent = (int)(percent*100);

? ? ? ? postInvalidate();

? ? }

public void setStatus(Status status) {

mFlag = status;

? ? }

public void clear() {

mFlag = Status.NONE;

? ? ? ? if (mScaledBitmap !=null) {

mScaledBitmap.recycle();

? ? ? ? ? ? mScaledBitmap =null;

? ? ? ? }

postInvalidate();

? ? }

protected void drawPercent(Canvas canvas,Paint paint) {

if (mFlag == Status.RUNNING) {

if (mScaledBitmap ==null) {

Bitmap mBitmap = BitmapFactory.decodeResource(getContext().getResources(), R.mipmap.percent_wave);

//? ? ? ? ? ? ? ? mScaledBitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth(), getHeight(), false);

? ? ? ? ? ? ? ? mScaledBitmap = Bitmap.createScaledBitmap(mBitmap, mBitmap.getWidth(), mBitmap.getHeight(), false);

? ? ? ? ? ? ? ? mBitmap.recycle();

? ? ? ? ? ? ? ? mBitmap =null;

? ? ? ? ? ? ? ? mRepeatCount = (int) Math.ceil(getWidth() /mScaledBitmap.getWidth() +0.5) +1;

? ? ? ? ? ? }

for (int idx =0; idx

canvas.drawBitmap(mScaledBitmap, mLeft + (idx -1) *mScaledBitmap.getWidth(), -mPercent * getHeight(), null);

? ? ? ? ? ? }

if(percent<=100){

String str =percent +"%";

? ? ? ? ? ? ? paint.setColor(mTextColot);

? ? ? ? ? ? ? paint.setTextSize(mTextSize);

? ? ? ? ? ? ? canvas.drawText(str, (getWidth() - paint.measureText(str)) /2, getHeight() /2 +mTextSize /2, paint);

? ? ? ? ? ? ? mLeft +=mSpeed;

? ? ? ? ? ? ? ? if (mLeft >=mScaledBitmap.getWidth())

mLeft =0;

? ? ? ? ? ? ? ? postInvalidateDelayed(30);

? ? ? ? ? ? }

}

}

@Override

? ? protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

// TODO Auto-generated method stub

? ? ? super.onMeasure(widthMeasureSpec, heightMeasureSpec);

? ? }

/**

* 繪制形狀

? ? * @return

? ? */

? public BitmapgetBitmap()

{

Bitmap bitmap = Bitmap.createBitmap(getWidth(), getHeight(),

? ? ? ? ? ? Bitmap.Config.ARGB_8888);

? ? ? Canvas canvas =new Canvas(bitmap);

? ? ? Paint paint =new Paint(Paint.ANTI_ALIAS_FLAG);

? ? ? paint.setColor(Color.BLACK);

? ? ? if (getWidth()!= getHeight())

{

canvas.drawRoundRect(new RectF(0, 0, getWidth(), getHeight()),

? ? ? ? ? ? ? roundWidth, roundHeight, paint);

? ? ? }else

? ? ? {

canvas.drawCircle(getWidth() /2, getWidth() /2, getWidth() /2,

? ? ? ? ? ? ? paint);

? ? ? }

return bitmap;

? }

}

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子惭载,更是在濱河造成了極大的恐慌旱函,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,734評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件描滔,死亡現(xiàn)場離奇詭異棒妨,居然都是意外死亡,警方通過查閱死者的電腦和手機含长,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,931評論 3 394
  • 文/潘曉璐 我一進店門券腔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人拘泞,你說我怎么就攤上這事纷纫。” “怎么了陪腌?”我有些...
    開封第一講書人閱讀 164,133評論 0 354
  • 文/不壞的土叔 我叫張陵辱魁,是天一觀的道長。 經(jīng)常有香客問我诗鸭,道長染簇,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,532評論 1 293
  • 正文 為了忘掉前任强岸,我火速辦了婚禮锻弓,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘蝌箍。我一直安慰自己青灼,他們只是感情好,可當我...
    茶點故事閱讀 67,585評論 6 392
  • 文/花漫 我一把揭開白布妓盲。 她就那樣靜靜地躺著杂拨,像睡著了一般。 火紅的嫁衣襯著肌膚如雪悯衬。 梳的紋絲不亂的頭發(fā)上扳躬,一...
    開封第一講書人閱讀 51,462評論 1 302
  • 那天,我揣著相機與錄音甚亭,去河邊找鬼贷币。 笑死,一個胖子當著我的面吹牛亏狰,可吹牛的內(nèi)容都是我干的役纹。 我是一名探鬼主播,決...
    沈念sama閱讀 40,262評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼暇唾,長吁一口氣:“原來是場噩夢啊……” “哼促脉!你這毒婦竟也來了辰斋?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,153評論 0 276
  • 序言:老撾萬榮一對情侶失蹤瘸味,失蹤者是張志新(化名)和其女友劉穎宫仗,沒想到半個月后,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體旁仿,經(jīng)...
    沈念sama閱讀 45,587評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡藕夫,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,792評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了枯冈。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片毅贮。...
    茶點故事閱讀 39,919評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖尘奏,靈堂內(nèi)的尸體忽然破棺而出滩褥,到底是詐尸還是另有隱情,我是刑警寧澤炫加,帶...
    沈念sama閱讀 35,635評論 5 345
  • 正文 年R本政府宣布瑰煎,位于F島的核電站,受9級特大地震影響俗孝,放射性物質(zhì)發(fā)生泄漏酒甸。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點故事閱讀 41,237評論 3 329
  • 文/蒙蒙 一驹针、第九天 我趴在偏房一處隱蔽的房頂上張望烘挫。 院中可真熱鬧诀艰,春花似錦柬甥、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,855評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至绿满,卻和暖如春臂外,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背喇颁。 一陣腳步聲響...
    開封第一講書人閱讀 32,983評論 1 269
  • 我被黑心中介騙來泰國打工漏健, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人橘霎。 一個月前我還...
    沈念sama閱讀 48,048評論 3 370
  • 正文 我出身青樓蔫浆,卻偏偏與公主長得像,于是被迫代替她去往敵國和親姐叁。 傳聞我的和親對象是個殘疾皇子瓦盛,可洞房花燭夜當晚...
    茶點故事閱讀 44,864評論 2 354

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