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;
? }
}