为ScrollView增加圆角的三种方式,及自定义属性【在Linearlayout中新增ScrollView支持滚动 后续】
获取圆角的几种方案如下:
方案一:
通过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);
}
为ScrollView增加圆角的三种方式,及自定义属性【在Linearlayout中新增ScrollView支持滚动 后续】的更多相关文章
- 三种方式实现观察者模式 及 Spring中的事件编程模型
观察者模式可以说是众多设计模式中,最容易理解的设计模式之一了,观察者模式在Spring中也随处可见,面试的时候,面试官可能会问,嘿,你既然读过Spring源码,那你说说Spring中运用的设计模式吧, ...
- iOS设置圆角的三种方式
第一种方法:通过设置layer的属性 最简单的一种,但是很影响性能,一般在正常的开发中使用很少. ? 1 2 3 4 5 6 7 UIImageView *imageView = [[UIImageV ...
- Android设置ScrollView回到顶部的三种方式 (转)
一.ScrollView.scrollTo(0,0) 直接置顶,瞬间回到顶部,没有滚动过程,其中Y值可以设置为大于0的值,使Scrollview停在指定位置; 二.ScrollView.fullSc ...
- 在Linearlayout中新增ScrollView支持滚动
https://blog.csdn.net/wenzhi20102321/article/details/53491176 1.一般只需要在布局中加个ScrollView即可 2.如果布局中包含lis ...
- JavaScript 基础——使用js的三种方式,js中的变量,js中的输出语句,js中的运算符;js中的分支结构
JavaScript 1.是什么:基于浏览器 基于(面向)对象 事件驱动 脚本语言 2.作用:表单验证,减轻服务器压力 添加野面动画效果 动态更改页面内容 Ajax网络请求 () 3.组成部分:ECM ...
- spring配置datasource三种方式
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcytp34 spring配置datasource三种方式 1.使用org.spri ...
- spring配置datasource三种方式及具体信息
1.使用org.springframework.jdbc.datasource.DriverManagerDataSource说明:DriverManagerDataSource建立连接是只要有连接就 ...
- Spring配置dataSource的三种方式 数据库连接池
1.使用org.springframework.jdbc.dataSource.DriverManagerDataSource 说明:DriverManagerDataSource建立连接是只要有连接 ...
- NGUI注册事件的三种方式
1.第一种方式 当一个元素要执行某个方法,而这个方法在此元素赋予的脚本上有,那么直接会调用此方法,但此方法的名称必须是内置的固定名称,例如OnClick,OnMouseOver,OnMouseOut等 ...
随机推荐
- acwing 890. 能被整除的数
#include<bits/stdc++.h> #define ll long long using namespace std; int m; int n,p[20]; int sum, ...
- Java程序设计(2021春)——第一章续笔记与思考
Java程序设计(2021春)--第一章续笔记与思考 目录 Java程序设计(2021春)--第一章续笔记与思考 Java数据类型 基本数据类型 引用类型 基本数据类型--整数类型的细节 基本数据类型 ...
- jar打包
1.jar文件? 学过java的同学应该都听过吧!所以第一站是:打包发布 2.如何把 java 程序编译成 .exe 文件? 通常回答只有两种: 1)一种是制作一个可执行的 JAR 文件包,然后就可以 ...
- PHP7与php5
php在2015年12月03日发布了7.0正式版,带来了许多新的特性,以下是不完全列表: 性能提升:PHP7比PHP5.6性能提升了两倍. Improved performance: PHP 7 is ...
- Flask(9)- 蓝图的基本使用
前言 在前面的例子中,所有的页面处理逻辑都是放在同一个文件中,随着业务代码的增加,将所有代码都放在单个程序文件中是非常不合适的 不仅会让阅读代码变得困难,而且会给后期维护带来麻烦 Flask 中使用蓝 ...
- Dart学习记录(一)——对象
1. 静态成员.方法 1.1 static 声明 1.2 静态.非静态方法可访问静态成员.调用方法:静态方法不可访问静态成员.调用方法: 1.3 静态成员.方法,属于类的 ,不用实例化对象就可使用,不 ...
- Kubernetes 1.13.3 部署 Prometheus+Grafana-7.5.2(最新版本踩坑)
本教程直接在 Kubernetes 1.13.3 版本上安装 Prometheus 和 Grafana-7.5.2,至于它们的原理和概念就不再赘述,这里就直接开始操作. Git 下载相关 YAML 文 ...
- C++ 标准模板库(STL)——算法(Algorithms)的用法及理解
C++ STL中的算法(Algorithms)作用于容器.它们提供了执行各种操作的方式,包括对容器内容执行初始化.排序.搜索和转换等操作.按照对容器内容的操作可将STL 中的算法大致分为四类: (1) ...
- 中国剩余定理简析(python实现)
中国剩余定理CRT 正整数m1,m2,...,mk两两互素,对b1,b2,...,bk的同余式组为 \[\begin{cases} x \equiv b_1\; mod \;m_1\\ x \equi ...
- python正则表达式应用
import re ab='''ms: [["", "\u7acb\u5373\u4e0b\u8f7d"], ["", "\u52 ...