圆角或圆形的RelativeLayout、LinearLayout、ImageView等

 第一步:新建圆形View代理类RoundViewDelegate.class

如果想改为圆形view,修改rect_adius为很大的数字,比如10000

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.view.View;

/**
 * 圆角View 代理
 *如果想改为圆形view,修改rect_adius为很大的数字,比如10000
 */
public class RoundViewDelegate{
    private final RectF roundRect = new RectF();
    private float rect_adius = 10;  //单位为像素
    private final Paint maskPaint = new Paint();
    private final Paint zonePaint = new Paint();
    private View mView;
    private Context mContext;

    public RoundViewDelegate(View view, Context context){
        this.mView = view;
        this.mContext = context;
        init();
    }

    private void init(){
        maskPaint.setAntiAlias(true);
        maskPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        //
        zonePaint.setAntiAlias(true);
        zonePaint.setColor(Color.WHITE);
    }

    /**
     * 从新设置圆角
     * @param adius
     */
    public void setRectAdius(float adius) {
        rect_adius = adius;
        if(mView != null){
            mView.invalidate();
        }
    }

    /**
     * 圆角区域设置
     * @param width
     * @param height
     */
    public void roundRectSet(int width,int height){
        roundRect.set(0, 0, width, height);
    }

    /**
     * 画布区域裁剪
     * @param canvas
     */
    public void canvasSetLayer(Canvas canvas){
        canvas.saveLayer(roundRect, zonePaint, Canvas.ALL_SAVE_FLAG);
        canvas.drawRoundRect(roundRect, rect_adius, rect_adius, zonePaint);
        //
        canvas.saveLayer(roundRect, maskPaint, Canvas.ALL_SAVE_FLAG);
    }
}

第二步:新建 RoundRelativeLayout..class自定义控件类

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.RelativeLayout;

/**
 * 自定义控件:圆角RelativeLayout
 */
public class RoundRelativeLayout extends RelativeLayout {
    private RoundViewDelegate mRoundViewDelegate;

    public RoundRelativeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        if (mRoundViewDelegate == null) {
            mRoundViewDelegate = new RoundViewDelegate(this, getContext());
        }
    }

    public RoundRelativeLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        if (mRoundViewDelegate == null) {
            mRoundViewDelegate = new RoundViewDelegate(this, getContext());
        }
    }

    public RoundRelativeLayout(Context context) {
        super(context);
        if (mRoundViewDelegate == null) {
            mRoundViewDelegate = new RoundViewDelegate(this, getContext());
        }
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right,
                            int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int w = getWidth();
        int h = getHeight();
        mRoundViewDelegate.roundRectSet(w, h);
    }

    @Override
    public void draw(Canvas canvas) {
        mRoundViewDelegate.canvasSetLayer(canvas);
        super.draw(canvas);
        canvas.restore();
    }

}

 想要其它的圆角自定义控件,只需要新建一个类,复制上面的代码,修改下继承就行了。

第三步:在布局文件中使用自定义控件

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#fff"
    tools:context="com.animator.san.myapplication.MainActivity">

    <com.xiong.a.roundview.RoundRelativeLayout
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:background="#9E2A9F">

        <com.xiong.a.roundview.RoundImageView
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_centerInParent="true"
            android:background="@mipmap/test" />
    </com.xiong.a.roundview.RoundRelativeLayout>


</RelativeLayout>

注意修改自定义控件包名为你自己的。

第四步:运行效果截图





  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值