Android自定义view实现圆形waveview

所属分类: 软件编程 / Android 阅读数: 99
收藏 0 赞 0 分享

最近学习了贝塞尔曲线的一些知识,刚好项目中需要实现一个圆形进度,然后就将实现的waveView记录一下。需要使用的知识大概有自定义view、贝塞尔曲线、valueAnimator(属性动画)、Xfermode等。

以下为效果图:

废话不多说,直接上代码这里只是一些重要的代码。如果需要demo可以去下载。

下载地址

首先需要自定义view的属性:

<declare-styleable name="custom_wave_view_attr">

 <attr name="circle_color" format="color"></attr> //圆的颜色
 <attr name="circle_background_color" format="color"></attr> //圆的背景色
 <attr name="progress_wave_color" format="color"></attr> //水波纹的颜色
 <attr name="progress_text_size" format="dimension"></attr> //字体的大小
 <attr name="progress_text_color" format="color"></attr> //字体的颜色

</declare-styleable>

第二步自定义CustomWaveView

1、实现构造方法,在构造方法中获取属性值

TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.custom_wave_view_attr);
//圆的颜色
circle_color = ta.getColor(R.styleable.custom_wave_view_attr_circle_color,getResources().getColor(android.R.color.black));
//圆的背景色
circle_bg_color = ta.getColor(R.styleable.custom_wave_view_attr_circle_background_color,getResources().getColor(android.R.color.white));
//水波纹颜色
wave_color = ta.getColor(R.styleable.custom_wave_view_attr_progress_wave_color,getResources().getColor(android.R.color.holo_blue_dark));
//字体的颜色
text_color = ta.getColor(R.styleable.custom_wave_view_attr_progress_text_color,getResources().getColor(android.R.color.black));
//字体的大小
textSize = ta.getDimension(R.styleable.custom_wave_view_attr_progress_text_size,30f);
//释放资源
ta.recycle();

2、初始化画笔

//初始化背景圆画笔
mBgCirclePaint = new Paint();
//抗锯齿
mBgCirclePaint.setAntiAlias(true);
//设置背景圆的背景色
mBgCirclePaint.setColor(circle_bg_color);
//设置充满
mBgCirclePaint.setStyle(Paint.Style.FILL);
//初始化水波纹画笔
mWavePaint = new Paint();
//抗锯齿
mWavePaint.setAntiAlias(true);
//设置水波纹的背景色
mWavePaint.setColor(wave_color);
//设置充满
mWavePaint.setStyle(Paint.Style.FILL);
//使用Xfermode获取重叠部分
mWavePaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

3、绘制贝塞尔曲线。以下为原理图。

/**
* 初始化贝塞尔曲线上的点
*/
private void reset() {
 startP = new PointF(-width, height);
 nextP = new PointF(-width/2, height);
 threeP = new PointF(0, height);
 fourP = new PointF(width/2, height);
 endP = new PointF(width, height);
 controllerP1 = new PointF(-width/4, height);
 controllerP2 = new PointF(-width * 3/4, height);
 controllerP3 = new PointF(width/4, height);
 controllerP4 = new PointF(width * 3/4, height);
}

4、在onDraw方法中画贝塞尔曲线和圆

@Override
protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 //在透明画布上画背景圆
 mCanvas.drawCircle(width/2, height/2, radius, mBgCirclePaint);
 //贝塞尔曲线
 mPath.reset();
 mPath.moveTo(startP.x, startP.y);
 mPath.quadTo(controllerP1.x, controllerP1.y, nextP.x, nextP.y);
 mPath.quadTo(controllerP2.x, controllerP2.y, threeP.x, threeP.y);
 mPath.quadTo(controllerP3.x, controllerP3.y, fourP.x, fourP.y);
 mPath.quadTo(controllerP4.x, controllerP4.y, endP.x, endP.y);
 mPath.lineTo(endP.x, height);
 mPath.lineTo(-width, height);
 //在透明画布上绘制水波纹
 mCanvas.drawPath(mPath,mWavePaint);
 //将画好的圆绘制在画布上
 canvas.drawBitmap(mBitmap, 0, 0, null);
}

5、使用动画让贝塞尔曲线动起来

/**
* 开始动画 让startP的x点坐标在2S时间内循环移动到0点。
* depth---进度
* waveRipple----水波纹的振幅
*/
private void startAnimator() {
 animator = ValueAnimator.ofFloat(startP.x, 0);
 animator.setInterpolator(new LinearInterpolator());
 animator.setDuration(2000);
 //重复循环
 animator.setRepeatCount(ValueAnimator.INFINITE);
 animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
 @Override
 public void onAnimationUpdate(ValueAnimator animation) {
 startP.x = (Float) animation.getAnimatedValue();
 startP = new PointF(startP.x, height - depth);
 nextP = new PointF(startP.x + width/2, height - depth);
 threeP = new PointF(nextP.x + width/2, height - depth);
 fourP = new PointF(threeP.x + width/2, height - depth);
 endP = new PointF(fourP.x + width/2, height - depth);
 controllerP1 = new PointF(startP.x + width/4, height - depth + waveRipple);
 controllerP2 = new PointF(nextP.x + width/4, height - depth - waveRipple);
 controllerP3 = new PointF(threeP.x + width/4, height - depth + waveRipple);
 controllerP4 = new PointF(fourP.x + width/4, height - depth - waveRipple);
 invalidate();
 }
 });
 animator.start();
}

第三步在XML中使用自定义View

<com.criclewaveview_master.CustomWaveView
 android:id="@+id/custom_circle_wave_view"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 wave:circle_color = "@color/circle_color"
 android:layout_centerInParent="true"
 wave:circle_background_color = "@color/circle_bg_color"
 wave:progress_wave_color = "@color/colorAccent"
 wave:progress_text_size = "20sp"
 wave:progress_text_color = "@color/circle_color"/>

这样就完成了自定义WaveView。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android网络编程之获取网络上的Json数据实例

这篇文章主要介绍了Android网络编程之获取网络上的Json数据实例,本文用完整的代码实例讲解了在Android中读取网络中Json数据的方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中的windowSoftInputMode属性详解

这篇文章主要介绍了Android中的windowSoftInputMode属性详解,本文对windowSoftInputMode的9个属性做了详细总结,需要的朋友可以参考下
收藏 0 赞 0 分享

Android网络编程之UDP通信模型实例

这篇文章主要介绍了Android网络编程之UDP通信模型实例,本文给出了服务端代码和客户端代码,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中使用ListView实现漂亮的表格效果

这篇文章主要介绍了Android中使用ListView实现漂亮的表格效果,本文用详细的代码实例创建了一个股票行情表格,需要的朋友可以参考下
收藏 0 赞 0 分享

Android中刷新界面的二种方法

这篇文章主要介绍了Android中刷新界面的二种方法,本文使用Handler、postInvalidate两种方法实现界面刷新,需要的朋友可以参考下
收藏 0 赞 0 分享

Android SDK三种更新失败及其解决方法

这篇文章主要介绍了Android SDK三种更新失败及其解决方法,需要的朋友可以参考下
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(一)

Android3.0(API level 11)开始,Android设备不再需要专门的菜单键。随着这种变化,Android app应该取消对传统6项菜单的依赖。取而代之的是提供anction bar来提供基本的用户功能
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(二)

这次将继续上一篇文章没有讲完的Menu的学习,上下文菜单(Context menu)和弹出菜单(Popup menu)
收藏 0 赞 0 分享

Android学习笔记——Menu介绍(三)

今天继续昨天没有讲完的Menu的学习,主要是Popup Menu的学习,需要的朋友可以参考下
收藏 0 赞 0 分享

Android显示网络图片实例

这篇文章主要介绍了Android显示网络图片的方法,以实例形式展示了Android程序显示网络图片的方法,非常具有实用价值,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多