为ScrollView增加圆角的三种方式,及自定义属性【在Linearlayout中新增ScrollView支持滚动 后续】
阅读原文时间:2023年07月10日阅读:1

获取圆角的几种方案如下:
方案一:
通过shape来实现,给scrollView增加背景来实现
方案二:
通过自定义ScrollView,还要自定义属性,在dispatchDraw中不停的裁剪
方案三:
用Android 5.0新增的接口,给ScrollView添加setOutlineProvider监听来实现

【注意】:设置圆角时已经要给scrollview设置padding值,不然圆角没效果

demo:
方案一:

首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.xml




然后给scrollview新增background为上面的文件就行了

eg:https://www.cnblogs.com/MianActivity/p/5867776.html

方案二:
自定义布局:

package com.smartisanos.sara.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import com.smartisanos.sara.R;

public class RoundedRectLinearLayout extends LinearLayout {
private Path mClip;
private float mRadius;
private float mRadiusMarginTop;
private float mRadiusMarginLeft;
private float mRadiusMargeinRight;
private float mRadiusMargeinBottom;

public RoundedRectLinearLayout(Context context, AttributeSet attrs) {  
    super(context, attrs);  
    init(attrs);  
}

public RoundedRectLinearLayout(Context context, AttributeSet attrs,  
        int defStyle) {  
    super(context, attrs, defStyle);  
    init(attrs);  
}

private void init(AttributeSet attrs) {  
    if (attrs != null) {  
        TypedArray a = getContext().obtainStyledAttributes(attrs, R.styleable.RoundedRectListView, 0, 0);  
        mRadius = a.getDimensionPixelSize(R.styleable.RoundedRectListView\_radius, 0);  
        mRadiusMarginTop = a.getDimensionPixelSize(R.styleable.RoundedRectListView\_radius\_marginTop, 0);  
        mRadiusMarginLeft = a.getDimensionPixelSize(R.styleable.RoundedRectListView\_radius\_marginLeft, 0);  
        mRadiusMargeinRight = a.getDimensionPixelSize(R.styleable.RoundedRectListView\_radius\_marginRight, 0);  
        mRadiusMargeinBottom = a.getDimensionPixelSize(R.styleable.RoundedRectListView\_radius\_marginBottom, 0);  
        a.recycle();  
    }  
    setLayerType(View.LAYER\_TYPE\_SOFTWARE, null);  
}

@Override  
protected void onSizeChanged(int w, int h, int oldw, int oldh) {  
    super.onSizeChanged(w, h, oldw, oldh);  
    if (mRadius > 0) {  
        mClip = new Path();  
        RectF rectRound = new RectF(mRadiusMarginLeft, mRadiusMarginTop, w  
                - mRadiusMargeinRight, h - mRadiusMargeinBottom);  
        mClip.addRoundRect(rectRound, mRadius, mRadius, Direction.CW);  
    }  
}

@Override  
protected void dispatchDraw(Canvas canvas) {  
    int saveCount = canvas.save();  
    if (mRadius > 0) {  
        canvas.clipPath(mClip);  
    }  
    super.dispatchDraw(canvas);  
    canvas.restoreToCount(saveCount);  
}  

}

自定义属性:

在res/values/attrs.xml中


布局中:

把LinearLayout改为com.smartisanos.sara.widget.RoundedRectLinearLayout,同时:

<com.smartisanos.sara.widget.RoundedRectLinearLayout  
    android:layout\_width="match\_parent"  
    android:layout\_height="match\_parent"  
    android:background="@drawable/search\_result\_bg"  
    android:orientation="vertical"  
    android:paddingTop="7dp"  
    android:layout\_marginLeft="6dp"  
    android:layout\_marginRight="6dp"  
    app:radius="12dp"  
    app:radius\_marginLeft="@dimen/local\_search\_rect\_margin"  
    app:radius\_marginRight="@dimen/local\_search\_rect\_margin"  
    app:radius\_marginBottom="20dp" >

……

方案三:

    mSettingRound = IMEContext.getContext().getResources().getDimensionPixelSize(R.dimen.dialog\_keyboard\_setting\_round);  
    mScrollView = (ScrollView) mRootView.findViewById(R.id.dialog\_keyboard\_setting\_scroll);  
    if (Build.VERSION.SDK\_INT >= 21) {  
        mScrollView.setOutlineProvider(new ViewOutlineProvider() {  
            @Override  
            public void getOutline(View view, Outline outline) {  
                if (Build.VERSION.SDK\_INT >= 21) {  
                    outline.setRoundRect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom(), mSettingRound);  
                }  
            }  
        });  
        mScrollView.setClipToOutline(true);  
    }

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章