自定义圆形FrameLayout布局

来源:互联网 发布:网络传播杂志官网 编辑:程序博客网 时间:2024/06/06 01:51

代码: 

package com.wxl.circleimageviewdemo.widget;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Path;import android.graphics.PorterDuff;import android.graphics.PorterDuffXfermode;import android.graphics.RectF;import android.util.AttributeSet;import android.widget.FrameLayout;import com.wxl.circleimageviewdemo.R;/** * 圆形FrameLayout,可以设置外圆圈 */public class CircleFrameLayout extends FrameLayout{    private Paint roundPaint;    private Paint imagePaint;    private float mBorderWidth;    private int mBorderColor;    private float mRadius;    public CircleFrameLayout(Context context) {        this(context, null);    }    public CircleFrameLayout(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public CircleFrameLayout(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        if (attrs != null) {            TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.CircleFrameLayout);            mRadius = ta.getDimension(R.styleable.CircleFrameLayout_radius, 0);            mBorderWidth = ta.getDimension(R.styleable.CircleFrameLayout_borderwidth,0);            mBorderColor = ta.getColor(R.styleable.CircleFrameLayout_borderColor, Color.WHITE);            ta.recycle();        }        roundPaint = new Paint();        roundPaint.setColor(Color.WHITE);        roundPaint.setAntiAlias(true);        roundPaint.setStyle(Paint.Style.FILL);        roundPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));        imagePaint = new Paint();        imagePaint.setXfermode(null);    }    @Override    protected void dispatchDraw(Canvas canvas) {        canvas.saveLayer(new RectF(0, 0, canvas.getWidth(), canvas.getHeight()), imagePaint, Canvas.ALL_SAVE_FLAG);        super.dispatchDraw(canvas);        //绘制外圆环边框圆环        drawBorder(canvas);        drawTopLeft(canvas);        drawTopRight(canvas);        drawBottomLeft(canvas);        drawBottomRight(canvas);        canvas.restore();    }    private void drawBorder(Canvas canvas) {        if(mBorderWidth!=0&&getWidth()==getHeight()&&mRadius==getWidth()/2){            Paint paint=new Paint();            paint.setAntiAlias(true);            paint.setStrokeWidth(mBorderWidth);            paint.setColor(mBorderColor);            paint.setStyle(Paint.Style.STROKE);            canvas.drawCircle(getWidth()/2,getHeight()/2,getWidth()/2,paint);        }    }    private void drawTopLeft(Canvas canvas) {        if (mRadius > 0) {            Path path = new Path();            path.moveTo(0, mRadius);            path.lineTo(0, 0);            path.lineTo(mRadius, 0);            path.arcTo(new RectF(0, 0, mRadius * 2, mRadius * 2),                    -90, -90);            path.close();            canvas.drawPath(path, roundPaint);        }    }    private void drawTopRight(Canvas canvas) {        if (mRadius > 0) {            int width = getWidth();            Path path = new Path();            path.moveTo(width - mRadius, 0);            path.lineTo(width, 0);            path.lineTo(width, mRadius);            path.arcTo(new RectF(width - 2 * mRadius, 0, width,                    mRadius * 2), 0, -90);            path.close();            canvas.drawPath(path, roundPaint);        }    }    private void drawBottomLeft(Canvas canvas) {        if (mRadius > 0) {            int height = getHeight();            Path path = new Path();            path.moveTo(0, height - mRadius);            path.lineTo(0, height);            path.lineTo(mRadius, height);            path.arcTo(new RectF(0, height - 2 * mRadius,                    mRadius * 2, height), 90, 90);            path.close();            canvas.drawPath(path, roundPaint);        }    }    private void drawBottomRight(Canvas canvas) {        if (mRadius > 0) {            int height = getHeight();            int width = getWidth();            Path path = new Path();            path.moveTo(width - mRadius, height);            path.lineTo(width, height);            path.lineTo(width, height - mRadius);            path.arcTo(new RectF(width - 2 * mRadius, height - 2                    * mRadius, width, height), 0, 90);            path.close();            canvas.drawPath(path, roundPaint);        }    }}

配置文件:

    <declare-styleable name="CircleFrameLayout">        <attr name="radius" format="dimension" />        <attr name="borderwidth" format="dimension" />        <attr name="borderColor" format="color" />    </declare-styleable>

使用: 

            <com.wxl.circleimageviewdemo.widget.CircleFrameLayout                android:layout_width="300dp"                android:layout_height="300dp"                android:layout_margin="5dp"                app:borderwidth="5dp"                app:borderColor="@color/borderColor"                app:radius="150dp">                <ImageView                    android:layout_width="match_parent"                    android:layout_height="match_parent"                    android:src="@drawable/default_image"/>            </com.wxl.circleimageviewdemo.widget.CircleFrameLayout>


本文参考资料:https://github.com/angcyo/RoundAngleFrameLayout

参考资料料:http://blog.csdn.net/angcyo/article/details/51171299