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 ...
随机推荐
- css实现左(右)侧固定宽度,右(左)侧宽度自适应 ---清除浮动
老话长谈,css的不固定适应布局 不管是面试还是在平时的工作中,这样的布局形式一直都在用着,很常见,所以今天我就拿出来在唠叨一下, 既是给自己一个备忘存储,也是一个学习巩固的参考,知道大家都会,还 ...
- tomcat中gzip压缩
在tomcat中压缩文件,修改server.xml文件中的配置 <Connector port="8080" protocol="HTTP/1.1" co ...
- C#面向对象(继承)
- 从零开始自己搭建复杂网络2(以Tensorflow为例)
从零开始自己搭建复杂网络(以DenseNet为例) DenseNet 是一种具有密集连接的卷积神经网络.在该网络中,任何两层之间都有直接的连接,也就是说,网络每一层的输入都是前面所有层输出的并集, 而 ...
- 【mysql】表备份
几个讲得比较好的资料: http://www.cnblogs.com/liangshaoye/p/5464794.html:讲解了热备,温备,冷备,增量备份,差异备份等多种概念. http://www ...
- TLiteSQLMonitor 使用方法
- 区间dp好题cf149d 括号匹配
见题解链接https://blog.csdn.net/sdjzping/article/details/19160013 #include<bits/stdc++.h> using nam ...
- python 全栈开发,Day116(可迭代对象,type创建动态类,偏函数,面向对象的封装,获取外键数据,组合搜索,领域驱动设计(DDD))
昨日内容回顾 1. 三个类 ChangeList,封装列表页面需要的所有数据. StarkConfig,生成URL和视图对应关系 + 默认配置 AdminSite,用于保存 数据库类 和 处理该类的对 ...
- windows Service程序的安装、启动、卸载命令
安装:%SystemRoot%\Microsoft.NET\Framework\v4.0.30319\installutil.exe ServiceTest.exe 启动:Net Start serv ...
- Visual Studio强大的帮助工具--Resharper安装与使用
ReSharper是一个JetBrains公司出品的代码生成工具,其能帮助Microsoft Visual Studio成为一个更佳的IDE.使用ReSharper,你可以进行深度代码分析,智能代码协 ...