Android 自定义控件之圆形扩散View(DiffuseView)
- 实现效果
- 使用
- 属性方法
- 代码
- 源码下载
- 参考链接
实现效果
使用
XML中:
<com.airsaid.diffuseview.widget.DiffuseViewandroid:id="@+id/diffuseView"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1"app:diffuse_color="@color/colorAccent"app:diffuse_coreColor="@color/colorPrimaryDark"app:diffuse_coreImage="@android:drawable/ic_menu_search"app:diffuse_coreRadius="100"app:diffuse_maxWidth="300"app:diffuse_width="4"/>
代码中:
DiffuseView mDiffuseView = (DiffuseView) findViewById(R.id.diffuseView); mDiffuseView.start(); // 开始扩散 mDiffuseView.stop();// 停止扩散
属性&方法
属性名 | java方法 | 作用 |
---|---|---|
diffuse_color | setColor(int colorId) | 设置扩散圆颜色 |
diffuse_coreColor | setCoreColor(int colorId) | 设置中心圆颜色 |
diffuse_coreImage | setCoreImage(int imageId) | 设置中心圆图片 |
diffuse_coreRadius | setCoreRadius(int radius) | 设置中心圆半径 |
diffuse_maxWidth | setMaxWidth(int maxWidth) | 设置最大扩散宽度 |
diffuse_width | setDiffuseWidth(int width) | 设置扩散圆宽度,值越小越宽 |
代码
/*** Created by zhouyou on 2016/9/27.* Class desc:** 这是一个自定义圆圈扩散View*/ public class DiffuseView extends View {/** 扩散圆圈颜色 */private int mColor = getResources().getColor(R.color.colorAccent);/** 圆圈中心颜色 */private int mCoreColor = getResources().getColor(R.color.colorPrimary);/** 圆圈中心图片 */private Bitmap mBitmap;/** 中心圆半径 */private float mCoreRadius = 150;/** 扩散圆宽度 */private int mDiffuseWidth = 3;/** 最大宽度 */private Integer mMaxWidth = 255;/** 是否正在扩散中 */private boolean mIsDiffuse = false;// 透明度集合private List<Integer> mAlphas = new ArrayList<>();// 扩散圆半径集合private List<Integer> mWidths = new ArrayList<>();private Paint mPaint;public DiffuseView(Context context) {this(context, null);}public DiffuseView(Context context, AttributeSet attrs) {this(context, attrs, -1);}public DiffuseView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init();TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.DiffuseView, defStyleAttr, 0);mColor = a.getColor(R.styleable.DiffuseView_diffuse_color, mColor);mCoreColor = a.getColor(R.styleable.DiffuseView_diffuse_coreColor, mCoreColor);mCoreRadius = a.getFloat(R.styleable.DiffuseView_diffuse_coreRadius, mCoreRadius);mDiffuseWidth = a.getInt(R.styleable.DiffuseView_diffuse_width, mDiffuseWidth);mMaxWidth = a.getInt(R.styleable.DiffuseView_diffuse_maxWidth, mMaxWidth);int imageId = a.getResourceId(R.styleable.DiffuseView_diffuse_coreImage, -1);if(imageId != -1) mBitmap = BitmapFactory.decodeResource(getResources(), imageId);a.recycle();}private void init() {mPaint = new Paint();mPaint.setAntiAlias(true);mAlphas.add(255);mWidths.add(0);}@Overridepublic void invalidate() {if(hasWindowFocus()){super.invalidate();}}@Overridepublic void onDraw(Canvas canvas) {// 绘制扩散圆 mPaint.setColor(mColor);for (int i = 0; i < mAlphas.size(); i++) {// 设置透明度Integer alpha = mAlphas.get(i);mPaint.setAlpha(alpha);// 绘制扩散圆Integer width = mWidths.get(i);canvas.drawCircle(getWidth() / 2, getHeight() / 2, mCoreRadius + width, mPaint);if(alpha > 0 && width < mMaxWidth){mAlphas.set(i, alpha - 1);mWidths.set(i, width + 1);}}// 判断当扩散圆扩散到指定宽度时添加新扩散圆if (mWidths.get(mWidths.size() - 1) == mMaxWidth / mDiffuseWidth) {mAlphas.add(255);mWidths.add(0);}// 超过10个扩散圆,删除最外层if(mWidths.size() >= 10){mWidths.remove(0);mAlphas.remove(0);}// 绘制中心圆及图片mPaint.setAlpha(255);mPaint.setColor(mCoreColor);canvas.drawCircle(getWidth() / 2, getHeight() / 2, mCoreRadius, mPaint);if(mBitmap != null){canvas.drawBitmap(mBitmap, getWidth() / 2 - mBitmap.getWidth() / 2, getHeight() / 2 - mBitmap.getHeight() / 2, mPaint);}if(mIsDiffuse){invalidate();}}/*** 开始扩散*/public void start() {mIsDiffuse = true;invalidate();}/*** 停止扩散*/public void stop() {mIsDiffuse = false;}/*** 是否扩散中*/public boolean isDiffuse(){return mIsDiffuse;}/*** 设置扩散圆颜色*/public void setColor(int colorId){mColor = colorId;}/*** 设置中心圆颜色*/public void setCoreColor(int colorId){mCoreColor = colorId;}/*** 设置中心圆图片*/public void setCoreImage(int imageId){mBitmap = BitmapFactory.decodeResource(getResources(), imageId);}/*** 设置中心圆半径*/public void setCoreRadius(int radius){mCoreRadius = radius;}/*** 设置扩散圆宽度(值越小宽度越大)*/public void setDiffuseWidth(int width){mDiffuseWidth = width;}/*** 设置最大宽度*/public void setMaxWidth(int maxWidth){mMaxWidth = maxWidth;} }
源码下载
GitHub:https://github.com/Airsaid/DiffuseView
转载于:https://www.cnblogs.com/zhujiabin/p/7515592.html
Android 自定义控件之圆形扩散View(DiffuseView)相关推荐
- Android 自定义控件之圆形页面指示器CirclePageIndicator带划动效果
Android 自定义控件之圆形页面指示器CirclePageIndicator带划动效果 前言 感谢 效果图 目标 流程 自定义属性 自定义默认属性 自定义接口 创建控件类继承View 声明属性变量 ...
- android 自定义时钟,Android自定义控件之圆形时钟(续)
在上篇文章中,我向大家介绍了如何通过自定义View一步步画出一个漂亮的圆形时钟.如果你还没看的话,我不建议你接着往下看,因为这篇文章是接着上篇的文章,如果直接看的话可能会不知所云,所以还是建议你先看一 ...
- Android自定义控件NumberCircleProgressBar(圆形进度条)的实现
Android自定义控件NumberCircleProgressBar(圆形进度条)的实现
- Android自定义控件入门到精通--View树的布局
<Android自定义控件入门到精通>文章索引 ☞ https://blog.csdn.net/Jhone_csdn/article/details/118146683 <Andro ...
- Android自定义控件入门到精通--View树的测量流程
<Android自定义控件入门到精通>文章索引 ☞ https://blog.csdn.net/Jhone_csdn/article/details/118146683 <Andro ...
- Android自定义控件实现圆形图片
前言:我们都知道as(Android Studio)里面的所有可以使用图片的控件,都是方方正正的,如果我们做头像作用时,方方正正的就很奇怪,我们就想要一个圆的图片了.这里介绍一种自定义控件的方式来实现 ...
- Android自定义控件之圆形头像
重写ImageView public class CircleImageView extends ImageView {private static final ScaleType SCALE_TYP ...
- Android自定义控件面试题,自定义View面试总结
本着针对面试,不负责任的态度,写下<面试总结>系列.本系列记录面试过程中各个知识点,而不是入门系列,如果有不懂的自行学习. 自定义View三种方式,组合现有控件,继承现有控件,继承View ...
- Android实现边缘凹凸的View
转载 最近做项目的时候遇到一个卡劵的效果,由于自己觉得用图片来做的话可以会出现适配效果不好,再加上自己自定义view方面的知识比较薄弱,所以想试试用自定义View来实现.但是由于自己知识点薄弱,一开始 ...
最新文章
- LINUX 查找tomcat日志关键词
- c51单片机的语言,51单片机,stm32,arduino都是用什么语言进行编程的?
- UML 类图几种关系的总结
- H3C交换机S5500系列恢复控制台登录口令
- HBase基本概念与基本使用
- HDU2159 FATE(二维背包、带限制条件的背包问题)
- The Famous Clock
- 手机“开口”,化身“智能机器人”
- LAMMPS安装全套解决方案
- 延时等待的gcode
- 【专题5: 硬件设计】 之 【62.案例四:简易空气净化器,完整原理图】
- (Python2.7.x) Systrace 使用的坑,出现 ImportError: No module named XXX
- 微信公众号运营辅助工具汇总
- idea隐藏菜单栏Main Menu 恢复方法
- 推荐PC端一款非常好用的解压缩软件
- Python:实现simpson rule辛普森法则算法(附完整源码)
- 租用服务器怎么修改密码,把租的号改密码-租用的服务器怎么改密码?
- 【3D建模制作技巧分享】Maya模型如何导入zbrush
- PS渐变羽化制作单车
- 用计算机弹发如雪,计算器弹音乐谱子