使用方式:直接在xml中使用即可。

<com.base.baseview.RoundLayoutandroid: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-leftpublic 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);}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);mLayer.set(0, 0, w, h);refreshRegion(this);}@Overrideprotected void dispatchDraw(Canvas canvas) {canvas.saveLayer(mLayer, null, Canvas.ALL_SAVE_FLAG);super.dispatchDraw(canvas);onClipDraw(canvas);canvas.restore();}@Overridepublic 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等上面相关推荐

  1. android 圆角图片

    今天,简单讲讲Android圆角图片的使用. 最近,感觉好忙,换了工作,新的APP要重新阅读,对我是一个考验.所有这段时间的博客可能不会很详细了,因为我比较忙,而且还有很多内容要写.这篇写完后,我会写 ...

  2. Android P(3)---Android P 功能和 API

    Android P 功能和 API Android P 为用户和开发者引入众多新特性和新功能. 本文重点介绍面向开发者的新功能. 要了解新 API,请阅读 API 差异报告或访问 Android AP ...

  3. android 圆角边框边框渐变,Android中用shape做渐变,边框,圆角等效果

    以前没接触到shape的时候,做圆角,渐变等效果都是依赖图片效果:如果对PS不熟悉,光是做图就要花去大把时间. 废话不讲了,把总结的内容记录下来,俺们不是在写博客,算是做个云笔记吧. shape用法与 ...

  4. Android 圆角圆形ImageView(超简单实现)

    前言:今天偶然看到我之前写过的一篇博客 Android项目中遇到的坑之(Android圆角圆形图 一),我在想,这不就是在模仿ImageView么,我为什么要模仿,直接拿来用不是更好么?我能直接在Im ...

  5. Android分享功能,微博、QQ、QQ空间等社交平台分享之入门与进阶

    前言 如何能迅速做一个分享功能,那主要就是根据第一个步骤来做就好了,因为友盟的sdk包已经集成很多的内容了,只要你有相应的app和平台账号就可以能马上分享.进阶的内容就是从第三部分开始,这些内容不难, ...

  6. 【Android】Android开发启动app弹出一张广告图片,Dialog可以查看大图,查看某个图片功能...

    作者:程序员小冰,GitHub主页:https://github.com/QQ986945193 新浪微博:http://weibo.com/mcxiaobing 首先给大家看一下我们今天这个最终实现 ...

  7. Android实现分享代码,Android 分享功能的实现代码

    Android 分享功能的实现代码 一个Activity中,取出设备上安装的所有支持分享动作的Activity,在grid中显示. 实例代码: /** * 分享activity */ public c ...

  8. android支持平台,Android 平台功能

    Android 平台功能Android Platform Features 12/11/2019 本文内容 开发 Xamarin.Forms 适用于 Android 的应用程序需要 Visual St ...

  9. GlideDemo【Glide3.7.0版本的简单使用以及圆角功能】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 本Demo主要记录Glide3.7.0版本的简单运用和实现圆角方案. 效果图 代码分析 Glide的centerCrop()和fit ...

最新文章

  1. vsFTPd 服务器初学者指南
  2. BootStrap 提示框
  3. android跌倒检测,Android跌倒检测
  4. 001 Hello Security 的框架搭建
  5. 阿里巴巴宣布架构调整:集中发力推进三大战略 程立任集团CTO
  6. 超实用!SKETCH大师最常用的3个实战小技巧
  7. 潜在失效模式和后果分析 FMEA 10分钟教程
  8. Python 玩转数据 17 - Pandas 数据处理 合并 pd.merge() df1.merge(df2)
  9. 一篇好文之Android文本软键盘全解
  10. mac 安装Xshell4
  11. 1.(4)数据结构之链表的操作,判空,求长度,排序
  12. 2G、3G无线路由器拨号连接过程
  13. “贵人”相助,亚马逊云科技APN成员乘风破浪
  14. springsecurity实现MD5验证用户登录
  15. 深度学习图片分类增强数据集的方法汇总
  16. 京东618自动浏览叠蛋糕app
  17. hdf heg 批量拼接_MODIS处理工具MRT已被HEG代替
  18. 继续魔改TCP BBR
  19. 计算机游戏本和商务本的区别,商务本和游戏本有什么区别
  20. HTML+JS 前端雪花飘落

热门文章

  1. 自动设置IP地址的BAT
  2. Hyperledger Fabric链码修改与测试(一)
  3. 【Ray Trace from Groud Up】光线追踪代码实现解析
  4. STM32 I/O口不能正常输出高低电平问题的解决方案
  5. 南通大学计算机学院本科考研,2017年南通大学计算机科学与技术学院919C考研题库...
  6. Ubuntu18.04 安装Proverif 和Syther_tool
  7. 第4课 从键盘读入变量的值及保留小数位问题
  8. 模块的included()
  9. bgp整机柜租用有什么好处?
  10. 六 光圈值变化的不同效果