Android特效专辑(十)——点击水波纹效果实现,逻辑清晰实现简单

这次做的东西呢,和上篇有点类似,就是用比较简单的逻辑思路去实现一些比较好玩的特效,最近也是比较忙,所以博客更新的速度还得看时间去推演,但是也能保证一周三更的样子,现在也还是以小功能,或者说是一些小入门级别的博客为主,我也不算是什么很厉害的人,很多细节的支持处理的仍然还是不到位,所以也是一直在弥补,话不多说,来看看今天的效果

Android特效专辑(十)——点击水波纹效果实现,逻辑清晰实现简单 - 图1

实现起来很简单吧,那我们就来看一下他是怎么实现的咯!

OnclickRuning

  1. package com.lgl.onclickruning;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Paint.Style;
  7. import android.os.Handler;
  8. import android.util.AttributeSet;
  9. import android.util.Log;
  10. import android.view.MotionEvent;
  11. import android.view.View;
  12. /**
  13. * 点击水波纹涟漪动画效果
  14. *
  15. * @author LGL
  16. *
  17. */
  18. public class Runing extends View {
  19. // 画笔
  20. private Paint mPaint;
  21. // 内圆宽度
  22. private int strokeWidth;
  23. // 圆心x
  24. private int cx;
  25. // 圆心y
  26. private int cy;
  27. // 半径
  28. private int radius;
  29. // Handler消息
  30. private static final int FLUSH = 0;
  31. public Runing(Context context, AttributeSet attrs) {
  32. super(context, attrs);
  33. init();
  34. }
  35. private void init() {
  36. // 初始化画笔
  37. mPaint = new Paint();
  38. // 抗锯齿
  39. mPaint.setAntiAlias(true);
  40. // 设置颜色
  41. mPaint.setColor(Color.BLUE);
  42. // 设置空心
  43. mPaint.setStyle(Style.STROKE);
  44. // 设置内圆的宽度
  45. mPaint.setStrokeWidth(strokeWidth);
  46. // 设置透明度 0-255
  47. mPaint.setAlpha(255);
  48. // 初始值
  49. strokeWidth = 0;
  50. radius = 0;
  51. }
  52. /**
  53. * 绘制
  54. */
  55. @Override
  56. protected void onDraw(Canvas canvas) {
  57. super.onDraw(canvas);
  58. // 绘制圆环
  59. canvas.drawCircle(cx, cy, radius, mPaint);
  60. }
  61. /**
  62. * 触摸事件
  63. */
  64. @Override
  65. public boolean onTouchEvent(MotionEvent event) {
  66. // 判断手势按下和抬起
  67. switch (event.getAction()) {
  68. case MotionEvent.ACTION_DOWN:
  69. /**
  70. * 按下去开始画圆,也就是起涟漪,所以我们首先得获取到按下的坐标,事实上,我们要做这样操作的开发,都必须先提前拿到该有的坐标
  71. */
  72. cx = (int) event.getX();
  73. cy = (int) event.getY();
  74. Log.i("坐标", "圆心x:" + cx + "圆心y:" + cy);
  75. break;
  76. }
  77. // 初始化
  78. init();
  79. // 发送
  80. handler.sendEmptyMessage(FLUSH);
  81. return true;
  82. }
  83. /**
  84. * 刷新状态
  85. */
  86. private void flush() {
  87. // 半径每次+10
  88. radius += 10;
  89. // 线条的宽度每次都是半径的四分之一
  90. strokeWidth = radius / 4;
  91. // 重新设置给画笔
  92. mPaint.setStrokeWidth(strokeWidth);
  93. // 颜色渐变,每次减少20的色值
  94. int nextAlpha = mPaint.getAlpha() - 20;
  95. // 避免等于负数
  96. if (nextAlpha < 20) {
  97. // 直接设置为透明
  98. nextAlpha = 0;
  99. }
  100. // 继续重新设置给画笔
  101. mPaint.setAlpha(nextAlpha);
  102. }
  103. private Handler handler = new Handler() {
  104. public void handleMessage(android.os.Message msg) {
  105. switch (msg.what) {
  106. case FLUSH:
  107. // 更改参数状态
  108. flush();
  109. // 刷新 执行我们的绘制方法
  110. invalidate();
  111. // 继续验证透明度,只要不为0就一直发送,直到透明
  112. if (mPaint.getAlpha() != 0) {
  113. handler.sendEmptyMessageDelayed(FLUSH, 100);
  114. }
  115. break;
  116. }
  117. }
  118. };
  119. }

layout_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent" >
  5. <com.lgl.onclickruning.Runing
  6. android:layout_width="match_parent"
  7. android:layout_height="match_parent" />
  8. </RelativeLayout>

代码其实很简单,我们简单的逻辑就可以实现了,但是别小看了这个思维,你可以用这个逻辑去实现更多有趣的特效,这里就期待你的挖掘了,嘻嘻

Demo下载地址:http://download.csdn.net/detail/qq_26787115/9433534