Android自定义View实现水波纹动画

2 下载量 55 浏览量 更新于2024-09-01 收藏 120KB PDF 举报
"Android 自定义view实现水波纹动画效果" 在Android开发中,自定义View是一种常见的技术,它允许开发者创建独特的用户界面元素和动画效果。本实例关注的是如何使用自定义View来实现水波纹的动画效果。水波纹动画可以增添应用程序的视觉吸引力,尤其是在游戏或动态壁纸等场景中。 首先,我们要理解实现水波纹动画的两种主要方式: 1. **标准正余弦水波纹**:这种方法依赖于数学中的正余弦函数来模拟水波的起伏。通过调整函数参数(如周期、振幅和初始相位),可以创造出不同形态的水波纹。在Android中,我们可以在onDraw()方法中计算每个波纹点的坐标,然后不断更新这些点的位置来实现动画效果。 - **onMeasure()**: 这是第一个被调用的方法,用于测量View的大小。在这里,我们可以设定初始参数,如计算周期因子,以确保水波纹在整个View的宽度上均匀分布。 - **onSizeChanged()**: 在View尺寸改变后调用,可以获取到View的实际宽度和高度,这对于计算水波纹的坐标至关重要。 - **onDraw()**: 这是关键的绘制方法,我们将根据正余弦函数计算的坐标在Canvas上绘制水波纹。 2. **非标准圆形液柱水波纹**:与正余弦函数不同,这种效果可能涉及到更复杂的图像处理技术,如PorterDuffXfermode。PorterDuffXfermode是一种图像混合模式,它可以用来合并两个图形,实现各种视觉效果,包括水波纹的扩散和混合。实现这种效果通常需要更多图像处理的知识,包括理解和应用不同的绘图模式。 为了实现这两种水波纹效果,你需要熟悉Android的图形绘制API,包括Canvas、Paint和Path类的使用。同时,理解正余弦函数以及如何将其应用于二维坐标系统也是非常重要的。在编写代码时,需要在onDraw()方法中使用一个循环,不断地更新波纹的位置并重绘View,以实现动画的连续效果。 在实际开发中,你可以根据需求选择继承自View、TextView或ImageView等类,然后重写上述关键方法。在这个例子中,选择直接继承自View是最简单的选择,因为它提供了基本的绘图功能,适合实现自定义动画。 最后,记得在AndroidManifest.xml中为你的自定义View设置合适的权限,并在布局文件中引用这个自定义View,以便在运行时可以看到动画效果。通过不断实践和优化,你可以创造出更加逼真的水波纹动画,满足产品设计人员的需求,提升用户体验。
2014-12-31 上传
总共分为三层:一层为圆形边线,一层为进度边线,一层用来显示标识进度节点。 public class CircleProgressBar extends View { private int maxProgress = 100; private int progress = 15; private int progressStrokeWidth = 2; private int marxArcStorkeWidth = 16; // 画圆所在的距形区域 RectF oval; Paint paint; public CircleProgressBar(Context context, AttributeSet attrs) { super(context, attrs); // TODO Auto-generated constructor stub oval = new RectF(); paint = new Paint(); } @Override protected void onDraw(Canvas canvas) { // TODO 自动生成的方法存根 super.onDraw(canvas); int width = this.getWidth(); int height = this.getHeight(); width = (width > height) ? height : width; height = (width > height) ? height : width; paint.setAntiAlias(true); // 设置画笔为抗锯齿 paint.setColor(Color.WHITE); // 设置画笔颜色 canvas.drawColor(Color.TRANSPARENT); // 白色背景 paint.setStrokeWidth(progressStrokeWidth); // 线宽 paint.setStyle(Style.STROKE); oval.left = marxArcStorkeWidth / 2; // 左上角x oval.top = marxArcStorkeWidth / 2; // 左上角y oval.right = width - marxArcStorkeWidth / 2; // 左下角x oval.bottom = height - marxArcStorkeWidth / 2; // 右下角y canvas.drawArc(oval, -90, 360, false, paint); // 绘制白色圆圈,即进度条背景 paint.setColor(Color.rgb(0x57, 0x87, 0xb6)); paint.setStrokeWidth(marxArcStorkeWidth); canvas.drawArc(oval, -90, ((float) progress / maxProgress) * 360, false, paint); // 绘制进度圆弧,这里是蓝色 paint.setStrokeWidth(1); String text = progress + "%"; int textHeight = height / 4; paint.setTextSize(textHeight); int textWidth = (int) paint.measureText(text, 0, text.length()); paint.setStyle(Style.FILL); canvas.drawText(text, width / 2 - textWidth / 2, height / 2 + textHeight / 2, paint); } public int getMaxProgress() { return maxProgress; } public void setMaxProgress(int maxProgress) { this.maxProgress = maxProgress; } /** * 设置进度 * * @param progress * 进度百分比 * @param view * 标识进度的节点视图 */ public void setProgress(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.invalidate(); } /** * 非UI线程调用 */ public void setProgressNotInUiThread(int progress, View view) { this.progress = progress; view.setAnimation(pointRotationAnima(0, (int) (((float) 360 / maxProgress) * progress))); this.postInvalidate(); } /** * 进度标注点的动画 * * @param fromDegrees * @param toDegrees * @return */ private Animation pointRotationAnima(float fromDegrees, float toDegrees) { int initDegress = 306;// 进度点起始位置(图片偏移约54度) RotateAnimation animation = new RotateAnimation(fromDegrees, initDegress + toDegrees, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(1);// 设置动画执行时间 animation.setRepeatCount(1);// 设置重复执行次数 animation.setFillAfter(true);// 设置动画结束后是否停留在结束位置 return animation; } }