android自訂一圓角ImageView-圓角圖片,

來源:互聯網
上載者:User

android自訂一圓角ImageView-圓角圖片,

public class CircleImageView extendsImageView {     privatestatic final ScaleType SCALE_TYPE = ScaleType.CENTER_CROP;     privatestatic final Bitmap.Config BITMAP_CONFIG = Bitmap.Config.ARGB_8888;    privatestatic final int COLORDRAWABLE_DIMENSION = 1;     privatestatic final int DEFAULT_BORDER_WIDTH = 0;    privatestatic final int DEFAULT_BORDER_COLOR = Color.BLACK;     privatefinal RectF mDrawableRect = newRectF();    privatefinal RectF mBorderRect = newRectF();     privatefinal Matrix mShaderMatrix = newMatrix();    privatefinal Paint mBitmapPaint = newPaint();    privatefinal Paint mBorderPaint = newPaint();     privateint mBorderColor = DEFAULT_BORDER_COLOR;    privateint mBorderWidth = DEFAULT_BORDER_WIDTH;     privateBitmap mBitmap;    privateBitmapShader mBitmapShader;    privateint mBitmapWidth;    privateint mBitmapHeight;     privatefloat mDrawableRadius;    privatefloat mBorderRadius;     privateboolean mReady;    privateboolean mSetupPending;     publicCircleImageView(Context context) {        super(context);         init();    }     publicCircleImageView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }     publicCircleImageView(Context context, AttributeSet attrs, intdefStyle) {        super(context, attrs, defStyle);         TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CircleImageView, defStyle,0);         mBorderWidth = a.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);        mBorderColor = a.getColor(R.styleable.CircleImageView_border_color, DEFAULT_BORDER_COLOR);         a.recycle();         init();    }     privatevoid init() {        super.setScaleType(SCALE_TYPE);        mReady = true;         if(mSetupPending) {            setup();            mSetupPending = false;        }    }     @Override    publicScaleType getScaleType() {        returnSCALE_TYPE;    }     @Override    publicvoid setScaleType(ScaleType scaleType) {        if(scaleType != SCALE_TYPE) {            thrownew IllegalArgumentException(String.format("ScaleType %s not supported.", scaleType));        }    }     @Override    protectedvoid onDraw(Canvas canvas) {        if(getDrawable() == null) {            return;        }         canvas.drawCircle(getWidth() / 2, getHeight() / 2, mDrawableRadius, mBitmapPaint);        if(mBorderWidth != 0) {            canvas.drawCircle(getWidth() / 2, getHeight() / 2, mBorderRadius, mBorderPaint);        }    }     @Override    protectedvoid onSizeChanged(intw, inth, intoldw, intoldh) {        super.onSizeChanged(w, h, oldw, oldh);        setup();    }     publicint getBorderColor() {        returnmBorderColor;    }     publicvoid setBorderColor(intborderColor) {        if(borderColor == mBorderColor) {            return;        }         mBorderColor = borderColor;        mBorderPaint.setColor(mBorderColor);        invalidate();    }     publicint getBorderWidth() {        returnmBorderWidth;    }     publicvoid setBorderWidth(intborderWidth) {        if(borderWidth == mBorderWidth) {            return;        }         mBorderWidth = borderWidth;        setup();    }     @Override    publicvoid setImageBitmap(Bitmap bm) {        super.setImageBitmap(bm);        mBitmap = bm;        setup();    }     @Override    publicvoid setImageDrawable(Drawable drawable) {        super.setImageDrawable(drawable);        mBitmap = getBitmapFromDrawable(drawable);        setup();    }     @Override    publicvoid setImageResource(intresId) {        super.setImageResource(resId);        mBitmap = getBitmapFromDrawable(getDrawable());        setup();    }     @Override    publicvoid setImageURI(Uri uri) {        super.setImageURI(uri);        mBitmap = getBitmapFromDrawable(getDrawable());        setup();    }     privateBitmap getBitmapFromDrawable(Drawable drawable) {        if(drawable == null) {            returnnull;        }         if(drawable instanceofBitmapDrawable) {            return((BitmapDrawable) drawable).getBitmap();        }         try{            Bitmap bitmap;             if(drawable instanceofColorDrawable) {                bitmap = Bitmap.createBitmap(COLORDRAWABLE_DIMENSION, COLORDRAWABLE_DIMENSION, BITMAP_CONFIG);            }else{                bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(), BITMAP_CONFIG);            }             Canvas canvas = newCanvas(bitmap);            drawable.setBounds(0,0, canvas.getWidth(), canvas.getHeight());            drawable.draw(canvas);            returnbitmap;        }catch(OutOfMemoryError e) {            returnnull;        }    }     privatevoid setup() {        if(!mReady) {            mSetupPending = true;            return;        }         if(mBitmap == null) {            return;        }         mBitmapShader = newBitmapShader(mBitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);         mBitmapPaint.setAntiAlias(true);        mBitmapPaint.setShader(mBitmapShader);         mBorderPaint.setStyle(Paint.Style.STROKE);        mBorderPaint.setAntiAlias(true);        mBorderPaint.setColor(mBorderColor);        mBorderPaint.setStrokeWidth(mBorderWidth);         mBitmapHeight = mBitmap.getHeight();        mBitmapWidth = mBitmap.getWidth();         mBorderRect.set(0,0, getWidth(), getHeight());        mBorderRadius = Math.min((mBorderRect.height() - mBorderWidth) / 2, (mBorderRect.width() - mBorderWidth) / 2);         mDrawableRect.set(mBorderWidth, mBorderWidth, mBorderRect.width() - mBorderWidth, mBorderRect.height() - mBorderWidth);        mDrawableRadius = Math.min(mDrawableRect.height() / 2, mDrawableRect.width() / 2);         updateShaderMatrix();        invalidate();    }     privatevoid updateShaderMatrix() {        floatscale;        floatdx = 0;        floatdy = 0;         mShaderMatrix.set(null);         if(mBitmapWidth * mDrawableRect.height() > mDrawableRect.width() * mBitmapHeight) {            scale = mDrawableRect.height() / (float) mBitmapHeight;            dx = (mDrawableRect.width() - mBitmapWidth * scale) * 0.5f;        }else{            scale = mDrawableRect.width() / (float) mBitmapWidth;            dy = (mDrawableRect.height() - mBitmapHeight * scale) * 0.5f;        }         mShaderMatrix.setScale(scale, scale);        mShaderMatrix.postTranslate((int) (dx + 0.5f) + mBorderWidth, (int) (dy + 0.5f) + mBorderWidth);         mBitmapShader.setLocalMatrix(mShaderMatrix);    } }


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.