Android development circular ImageView implementation, androidimageview

Source: Internet
Author: User

Android development circular ImageView implementation, androidimageview

As follows:

1. Create an attrs file in the value folder and declare the following attributes.

<declare-styleable name="CircleImageView">        <attr name="border_color" format="color"/>        <attr name="border_width" format="dimension"/></declare-styleable>

2. inherit ImageView, override constructor and ondraw Method

public class CircleImageView extends AppCompatImageView {    private int borderColor;    private int borderWidth;    private final static int DEFAULT_COLOR = Color.BLACK;    private final static int DEFAULT_BORDER_WIDTH = 0;    private Paint mPaint;    public CircleImageView(Context context) {        this(context,null);    }    public CircleImageView(Context context, @Nullable AttributeSet attrs) {        this(context, attrs,0);    }    public CircleImageView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.CircleImageView);        borderColor = typedArray.getColor(R.styleable.CircleImageView_border_color, DEFAULT_COLOR);        borderWidth = typedArray.getDimensionPixelSize(R.styleable.CircleImageView_border_width, DEFAULT_BORDER_WIDTH);        borderWidth = dp2px(context,borderWidth);        mPaint = new Paint();        typedArray.recycle();    }    public int dp2px(Context ctx,int dp) {        float scale = ctx.getResources().getDisplayMetrics().density;        return (int) (dp * scale + 0.5);    }    @Override    protected void onDraw(Canvas canvas) {        Drawable drawable = getDrawable();        if (drawable != null && drawable instanceof BitmapDrawable) {            Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();            Bitmap circleBitmap = getCircleBitmap(bitmap);            Rect srcRect = new Rect(0,0,circleBitmap.getWidth(),circleBitmap.getHeight());            Rect dstRect = new Rect(0,0,getWidth(),getHeight());            mPaint.reset();            canvas.drawBitmap(circleBitmap,srcRect,dstRect,mPaint);        }else {               super.onDraw(canvas);        }    }    private Bitmap getCircleBitmap(Bitmap bitmap) {        Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(),bitmap.getHeight(),Bitmap.Config.ARGB_8888);        Canvas canvas = new Canvas(circleBitmap);        mPaint.setAntiAlias(true);        canvas.drawARGB(0,255,255,255);        mPaint.setColor(borderColor);        int radius = bitmap.getWidth() < bitmap.getHeight()? bitmap.getWidth():bitmap.getHeight();        canvas.drawCircle(radius/2,radius/2,radius/2-borderWidth,mPaint);        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));        Rect rect = new Rect(0,0,bitmap.getWidth(),bitmap.getHeight());        canvas.drawBitmap(bitmap,rect,rect,mPaint);        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OVER));        canvas.drawCircle(radius/2,radius/2,radius / 2,mPaint);        return circleBitmap;    }}

3. Introduce CircleImageView

<com.mydemo.view.CircleImageView        android:layout_width="100dp"        android:layout_height="100dp"        android:src="@drawable/logo"        app:border_color="#ffff00"        app:border_width="1dp"       />


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

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.