自定义有倒角的linearlayout

1: 参考:https://www.cnblogs.com/everhad/p/6161083.html

 

package kodulf.swiperefreshlayoutrecyclerviewdemo;

import android.content.Context;
import android.graphics.Bitmap;
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.LinearLayout;

/**
 * Created by Kodulf on 2019/1/12.
 */
public class MySecondLinearLayout extends LinearLayout {

    public MySecondLinearLayout(Context context) {
        this(context, null);
    }

    public MySecondLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MySecondLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    // 四个角的x,y半径
    private float[] radiusArray = { 50f, 50f, 50f, 50f, 0f, 0f, 0f, 0f };

    @Override
    public void draw(Canvas canvas) {
        //创建bitmap
        final Bitmap composedBitmap;
        final Bitmap originalBitmap;
        //创建画布
        final Canvas composedCanvas;
        final Canvas originalCanvas;
        final Paint paint;
        final int height;
        final int width;

        width = getWidth();

        height = getHeight();
        //ARGB_4444 代表16位Alpha的位图
        //ARGB_8888 代表32位ARGB位图
        composedBitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);
        originalBitmap = Bitmap.createBitmap(width, height,
                Bitmap.Config.ARGB_8888);

        composedCanvas = new Canvas(composedBitmap);
        originalCanvas = new Canvas(originalBitmap);

        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setColor(Color.BLACK);

        super.draw(originalCanvas);

        composedCanvas.drawARGB(0, 0, 0, 0);

        Bitmap bitmapFrame= makeRoundRectFrame(width, height);

        composedCanvas.drawBitmap(bitmapFrame, 0, 0, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

        composedCanvas.drawBitmap(originalBitmap, 0, 0, paint);

        canvas.drawBitmap(composedBitmap, 0, 0, new Paint());

    }

    private Bitmap makeRoundRectFrame(int w, int h) {
        Bitmap bm = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(bm);
        Path path = new Path();
        path.addRoundRect(new RectF(0, 0, w, h), radiusArray, Path.Direction.CW);
        Paint bitmapPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        bitmapPaint.setColor(Color.GREEN); // 颜色随意,不要有透明度。
        c.drawPath(path, bitmapPaint);
        return bm;
    }

}

 

 

方法2: 参考:https://blog.csdn.net/qq_33453910/article/details/78616447 

 

package kodulf.swiperefreshlayoutrecyclerviewdemo;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
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.util.AttributeSet;
import android.widget.LinearLayout;

/**
 * Created by Kodulf on 2019/1/12.
 */
public class MyLinearLayout extends LinearLayout {

    private final float density = getContext().getResources().getDisplayMetrics().density;
    private float roundness;
    private static final int DEFAULT_RECT_ROUND_RADIUS = 20;

    public MyLinearLayout(Context context) {
        this(context, null);
    }

    public MyLinearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyLinearLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取attr文件下,名为RoundedCornerImageView
        TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.mView, defStyleAttr, 0);
        //获取值
        roundness = ta.getDimensionPixelSize(R.styleable.mView_round_radius, DEFAULT_RECT_ROUND_RADIUS);
        ta.recycle();
    }


        @Override
        public void draw(Canvas canvas) {
            //创建bitmap
            final Bitmap composedBitmap;
            final Bitmap originalBitmap;
            //创建画布
            final Canvas composedCanvas;
            final Canvas originalCanvas;
            final Paint paint;
            final int height;
            final int width;

            width = getWidth();

            height = getHeight();
            //ARGB_4444 代表16位Alpha的位图
            //ARGB_8888 代表32位ARGB位图
            composedBitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);
            originalBitmap = Bitmap.createBitmap(width, height,
                    Bitmap.Config.ARGB_8888);

            composedCanvas = new Canvas(composedBitmap);
            originalCanvas = new Canvas(originalBitmap);

            paint = new Paint();
            paint.setAntiAlias(true);
            paint.setColor(Color.BLACK);

            super.draw(originalCanvas);

            composedCanvas.drawARGB(0, 0, 0, 0);
            //指定RectF对象以及圆角半径来实现,该方法是绘制圆形的主要方法,同时也可以通过设置画笔的空心效果来绘制空心的圆形
            //thi,roundness 分别是x,y方向的圆角半径
            composedCanvas.drawRoundRect(new RectF(0, 0, width, height),
                    this.roundness, this.roundness, paint);

            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

            composedCanvas.drawBitmap(originalBitmap, 0, 0, paint);

            canvas.drawBitmap(composedBitmap, 0, 0, new Paint());
        }

}

 

 

values文件夹里面

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="mView">
        <attr name="round_radius" format="dimension" />
        <attr name="round_color" format="color" />
        <attr name="text_color" format="color" />
        <attr name="text_size" format="dimension" />
    </declare-styleable>
</resources>

attr文件:

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值