自定义有倒角的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.youkuaiyun.com/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文件:

 

 

### 在自定义 ViewGroup 中添加 LinearLayout 的方法或实现方式 在 Android 开发中,自定义 `ViewGroup` 是一种强大的功能,允许开发者创建复杂的布局结构。如果需要在自定义的 `ViewGroup` 中添加 `LinearLayout`,可以通过以下方式实现[^1]。 首先,创建一个自定义的 `ViewGroup` 类,并继承自 `ViewGroup` 或其他合适的父类。接着,在该类的构造函数中初始化 `LinearLayout` 并将其添加到自定义的 `ViewGroup` 中。 以下是具体的实现代码示例: ```java public class CustomViewGroup extends ViewGroup { private LinearLayout linearLayout; public CustomViewGroup(Context context) { this(context, null); } public CustomViewGroup(Context context, AttributeSet attrs) { super(context, attrs); // 初始化 LinearLayout linearLayout = new LinearLayout(context); linearLayout.setOrientation(LinearLayout.VERTICAL); // 设置为垂直方向 LayoutParams layoutParams = new LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT ); linearLayout.setLayoutParams(layoutParams); // 将 LinearLayout 添加到自定义的 ViewGroup 中 addView(linearLayout); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { // 测量子视图 measureChildren(widthMeasureSpec, heightMeasureSpec); // 获取测量后的宽高 int maxWidth = MeasureSpec.getSize(widthMeasureSpec); int maxHeight = 0; for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { maxHeight += child.getMeasuredHeight(); } } // 设置自定义 ViewGroup 的宽高 setMeasuredDimension(maxWidth, maxHeight); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int currentTop = 0; for (int i = 0; i < getChildCount(); i++) { View child = getChildAt(i); if (child.getVisibility() != GONE) { int childWidth = child.getMeasuredWidth(); int childHeight = child.getMeasuredHeight(); // 布局子视图 child.layout(0, currentTop, childWidth, currentTop + childHeight); currentTop += childHeight; } } } } ``` #### 说明 - 上述代码展示了如何在自定义的 `ViewGroup` 中添加 `LinearLayout`[^2]。 - 在 `CustomViewGroup` 的构造函数中,初始化了一个 `LinearLayout`,并设置了其方向为垂直(`VERTICAL`)[^3]。 - 通过 `addView()` 方法将 `LinearLayout` 添加到自定义的 `ViewGroup` 中。 - 实现了 `onMeasure()` 和 `onLayout()` 方法,确保子视图能够正确地测量和布局[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值