获取圆角的几种方案如下:
方案一:
通过shape来实现,给scrollView增加背景来实现
方案二:
通过自定义ScrollView,还要自定义属性,在dispatchDraw中不停的裁剪
方案三:
用Android 5.0新增的接口,给ScrollView添加setOutlineProvider监听来实现
【注意】:设置圆角时已经要给scrollview设置padding值,不然圆角没效果
demo:
方案一:
首先在res/drawable文件夹下,新建一个文件,命名为:shape_radius.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corners android:radius="@dimen/dialog_keyboard_setting_round"/> <solid android:color="#31e10a"/> </shape>
然后给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中
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RoundedRectListView"> <attr name="radius" format="dimension" /> <attr name="radius_marginTop" format="dimension" /> <attr name="radius_marginLeft" format="dimension" /> <attr name="radius_marginRight" format="dimension" /> <attr name="radius_marginBottom" format="dimension" /> </declare-styleable> </resources>
布局中:
把LinearLayout改为com.smartisanos.sara.widget.RoundedRectLinearLayout,同时:
<com.smartisanos.sara.widget.LocalSearchLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/result"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<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" > …… </com.smartisanos.sara.widget.RoundedRectLinearLayout>
</com.smartisanos.sara.widget.LocalSearchLayout>
方案三:
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); }