android圆角功能,非常好用,可以用在图片,视频,gif等上面
使用方式:直接在xml中使用即可。
<com.base.baseview.RoundLayout
android:id="@+id/example_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:layout_gravity="center_horizontal"
app:attr_round_corner="7dp">
import android.content.Context;
import android.content.res.TypedArray;
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.graphics.Region;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout; /**
* 圆角布局
* 支持自定义属性见<>declare-styleable name="RoundLayout"</>
* <p>
* 需要圆角的View作为<>RoundLayout</>的子View即可,设置<>attr_round_corner</>圆角半径
*/
public class RoundLayout extends RelativeLayout {
public float[] radii = new float[8]; // top-left, top-right, bottom-right, bottom-left
public Path mClipPath; // 剪裁区域路径
public Paint mPaint; // 画笔
public int mStrokeColor; // 描边颜色
public int mStrokeWidth; // 描边半径
public boolean mClipBackground; // 是否剪裁背景
public Region mAreaRegion; // 内容区域
public RectF mLayer; // 画布图层大小
private int mRoundCorner; // 圆角大小 public RoundLayout(Context context, int radius) {
this(context);
this.mRoundCorner = PixelUtils.dip2px(context, radius);
} public RoundLayout(Context context) {
this(context, null);
} public RoundLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public RoundLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(context, attrs);
} @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mLayer.set(0, 0, w, h);
refreshRegion(this);
} @Override
protected void dispatchDraw(Canvas canvas) {
canvas.saveLayer(mLayer, null, Canvas.ALL_SAVE_FLAG);
super.dispatchDraw(canvas);
onClipDraw(canvas);
canvas.restore();
} @Override
public void draw(Canvas canvas) {
refreshRegion(this);
if (mClipBackground) {
canvas.save();
canvas.clipPath(mClipPath);
super.draw(canvas);
canvas.restore();
} else {
super.draw(canvas);
}
} public void initAttrs(Context context, AttributeSet attrs) {
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.RoundLayout);
mStrokeColor = ta.getColor(R.styleable.RoundLayout_attr_stroke_color, Color.WHITE);
mStrokeWidth = ta.getDimensionPixelSize(R.styleable.RoundLayout_attr_stroke_width, 0);
mClipBackground = ta.getBoolean(R.styleable.RoundLayout_attr_clip_background, false);
if (mRoundCorner == 0) {
mRoundCorner = ta.getDimensionPixelSize(R.styleable.RoundLayout_attr_round_corner, 4);
} int roundCornerTopLeft = ta.getDimensionPixelSize(
R.styleable.RoundLayout_attr_round_corner_top_left, mRoundCorner);
int roundCornerTopRight = ta.getDimensionPixelSize(
R.styleable.RoundLayout_attr_round_corner_top_right, mRoundCorner);
int roundCornerBottomLeft = ta.getDimensionPixelSize(
R.styleable.RoundLayout_attr_round_corner_bottom_left, mRoundCorner);
int roundCornerBottomRight = ta.getDimensionPixelSize(
R.styleable.RoundLayout_attr_round_corner_bottom_right, mRoundCorner);
ta.recycle(); radii[0] = roundCornerTopLeft;
radii[1] = roundCornerTopLeft; radii[2] = roundCornerTopRight;
radii[3] = roundCornerTopRight; radii[4] = roundCornerBottomRight;
radii[5] = roundCornerBottomRight; radii[6] = roundCornerBottomLeft;
radii[7] = roundCornerBottomLeft; mLayer = new RectF();
mClipPath = new Path();
mAreaRegion = new Region();
mPaint = new Paint();
mPaint.setColor(Color.WHITE);
mPaint.setAntiAlias(true); setLayerType(View.LAYER_TYPE_HARDWARE, null);
} public void refreshRegion(View view) {
int w = (int) mLayer.width();
int h = (int) mLayer.height();
RectF areas = new RectF();
areas.left = view.getPaddingLeft();
areas.top = view.getPaddingTop();
areas.right = w - view.getPaddingRight();
areas.bottom = h - view.getPaddingBottom();
mClipPath.reset(); mClipPath.addRoundRect(areas, radii, Path.Direction.CW);
Region clip = new Region((int) areas.left, (int) areas.top,
(int) areas.right, (int) areas.bottom);
mAreaRegion.setPath(mClipPath, clip);
} public void onClipDraw(Canvas canvas) {
if (mStrokeWidth > 0) {
// 将与描边区域重叠的内容裁剪掉
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_OUT));
mPaint.setColor(Color.WHITE);
mPaint.setStrokeWidth(mStrokeWidth * 2);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(mClipPath, mPaint);
// 绘制描边
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OVER));
mPaint.setColor(mStrokeColor);
mPaint.setStyle(Paint.Style.STROKE);
canvas.drawPath(mClipPath, mPaint);
}
mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
mPaint.setColor(Color.WHITE);
mPaint.setStyle(Paint.Style.FILL);
canvas.drawPath(mClipPath, mPaint);
}
}
喜欢的朋友点个赞吧,真的是非常好用!
android圆角功能,非常好用,可以用在图片,视频,gif等上面的更多相关文章
- Android P 功能和 API
Android P 功能和 API Android P 为用户和开发者引入众多新特性和新功能. 本文重点介绍面向开发者的新功能. 要了解新 API,请阅读 API 差异报告或访问 Android AP ...
- [译]:Xamarin.Android平台功能——位置服务
返回索引目录 原文链接:Location Services. 译文链接:Xamarin.Android平台功能--位置服务 本部分介绍位置服务以及与如何使用位置提供商服务 Location Servi ...
- Android表情功能
Android表情功能 标签(空格分隔): 未分类 转载自:android edittext插入表情(基于socket方式),并对文中不正确的内容进行整理和修正 [TOC] 涉及知识点: Androi ...
- Cocos2d-x使用android拍照功能加载照片内存过大,通过另存照片尺寸大小解决
使用2dx调用android拍照功能,拍照结束后在2dx界面显示拍照照片,如果不对照片做处理,会出现内存过大的问题,导致程序崩溃,如果仅仅另存拍照照片,则照片质量大小均下降,导致照片不够清晰,后来发现 ...
- Android定位功能
不说废话,直接说说实现android定位有关的API吧. 这些API都在android.location包下,一共有三个接口和八个类.它们配合使用即可实现定位功能. 三个接口: GpsStatus.L ...
- Android定位功能(二)
在前文Android定位功能(一)中,已经大致介绍了一下在Android平台中,和定位功能相关的类,并举例获取了位置信息.但是前文是基于Criteria定制了一个标准,通过getBestProvide ...
- GlideDemo【Glide3.7.0版本的简单使用以及圆角功能】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本Demo主要记录Glide3.7.0版本的简单运用和实现圆角方案. 效果图 代码分析 Glide的centerCrop()和fit ...
- GlideNewDemo【Glide4.7.1版本的简单使用以及圆角功能】
版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 简单记录下Glide4.7.1版本的使用和实现圆角方案. 注意:关于详细使用请仔细阅读<官方指南>. 效果图 使用步骤 ...
- Delphi xe7 up1 调用android振动功能
Delphi xe7 up1 调用android振动功能 振动用到以下4个单元: Androidapi.JNI.App,Androidapi.JNIBridge,Androidapi.JNI.Os,A ...
随机推荐
- SCons: 替代 make 和 makefile 及 javac 的极好用的c、c++、java 构建工具
http://scons.org/ https://www.ibm.com/developerworks/cn/linux/l-cn-scons/index.html 后附:另外,WAF是一个基于sc ...
- unbuntu 16.04.2 安装 Eclipse C++开发环境
1.安装JAVA (1)首先添加源: sudo gedit /etc/apt/sources.list 在打开的文件中添加如下内容并保存: deb http://ppa.launchpad.net/w ...
- excel导出的时候从程序后台写到excel里的是文本,所以无法在excel中计算怎么办?
文章引用自:http://www.cnblogs.com/rayray/p/3414452.html excel导出的时候从程序后台写到excel里的是文本,所以无法在excel中计算怎么办? 需要导 ...
- mac安装navicat mysql破解版
下载破解中文版http://m6.pc6.com/xuh6/navicat12027pre.zip 完成下载后无法正常进行安装,此时应该打开命令行执行 sudo spctl --master-disa ...
- python易错题之lambda 以及 for循环中内嵌函数
li = [] for x in range(10): print(x) //在函数没有执行前(li[0]()),for 循环中x已经执行完,x会一直为 9 def fun(): print(x) / ...
- 《剑指offer》-找到数组中重复的数字
题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...
- poj 3734 方块涂色 求红色 绿色方块都为偶数的方案数 (矩阵快速幂)
N个方块排成一列 用红,蓝,绿,黄4种颜色去涂色,求红色方块 和绿色方块个数同时为偶数的 方案数 对10007取余 Sample Input 212Sample Output 2//(蓝,黄)6//( ...
- 三步解决阿里云绑定公网IP地址失败解决方案
1.客户端设置为阿里云服务器的公有地址: 2.服务端设置为阿里云服务器的私有地址: 3.设置阿里云的管理规则: 第一步 第二步 第三.四步
- Codeforces 744C Hongcow Buys a Deck of Cards 状压dp (看题解)
Hongcow Buys a Deck of Cards 啊啊啊, 为什么我连这种垃圾dp都写不出来.. 不是应该10分钟就该秒掉的题吗.. 从dp想到暴力然后gg, 没有想到把省下的红色开成一维. ...
- BZOJ4974 八月月赛 Problem D 字符串大师 KMP
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ4974 - 八月月赛 Problem D 题意概括 一个串T是S的循环节,当且仅当存在正整数k,使得 ...