Android特效专辑(十二)——仿支付宝咻一咻功能实现波纹扩散特效,精细小巧的View

先来看看这个效果

Android特效专辑(十二)——仿支付宝咻一咻功能实现波纹扩散特效,精细小巧的View - 图1

这是我的在Only上添加的效果,说实话,Only现在都还只是半成品,台面都上不了,怪自己技术不行,也太懒了
PS:这个view也是我模仿了人家的效果,参考了人家的思路写的,不是纯手撸,罪过罪过,网上应该也能找到很多这样的效果,我只是加入了一些自己的需求在里面

我么新建一个工程——Whew

RoundImageView

这个之前讲过,网上 的粒子,把头像变成圆形的,这里就不多说了,直接撸代码吧!

  1. package com.lgl.whew;
  2. /**
  3. * 圆形头像
  4. * Created by LGL on 2016/1/12.
  5. */
  6. import android.content.Context;
  7. import android.content.res.TypedArray;
  8. import android.graphics.Bitmap;
  9. import android.graphics.Canvas;
  10. import android.graphics.Paint;
  11. import android.graphics.PorterDuff;
  12. import android.graphics.PorterDuffXfermode;
  13. import android.graphics.Rect;
  14. import android.graphics.drawable.BitmapDrawable;
  15. import android.graphics.drawable.Drawable;
  16. import android.graphics.drawable.NinePatchDrawable;
  17. import android.util.AttributeSet;
  18. import android.widget.ImageView;
  19. /**
  20. * 圆形ImageView,可设置最多两个宽度不同且颜色不同的圆形边框。
  21. *
  22. * 设置颜色在xml布局文件中由自定义属性配置参数指定
  23. */
  24. public class RoundImageView extends ImageView {
  25. private int mBorderThickness = 0;
  26. private Context mContext;
  27. private int defaultColor = 0xFFFFFFFF;
  28. // 如果只有其中一个有值,则只画一个圆形边框
  29. private int mBorderOutsideColor = 0;
  30. private int mBorderInsideColor = 0;
  31. // 控件默认长、宽
  32. private int defaultWidth = 0;
  33. private int defaultHeight = 0;
  34. public RoundImageView(Context context) {
  35. super(context);
  36. mContext = context;
  37. }
  38. public RoundImageView(Context context, AttributeSet attrs) {
  39. super(context, attrs);
  40. mContext = context;
  41. setCustomAttributes(attrs);
  42. }
  43. public RoundImageView(Context context, AttributeSet attrs, int defStyle) {
  44. super(context, attrs, defStyle);
  45. mContext = context;
  46. setCustomAttributes(attrs);
  47. }
  48. private void setCustomAttributes(AttributeSet attrs) {
  49. TypedArray a = mContext.obtainStyledAttributes(attrs,
  50. R.styleable.roundedimageview);
  51. mBorderThickness = a.getDimensionPixelSize(
  52. R.styleable.roundedimageview_border_thickness, 0);
  53. mBorderOutsideColor = a
  54. .getColor(R.styleable.roundedimageview_border_outside_color,
  55. defaultColor);
  56. mBorderInsideColor = a.getColor(
  57. R.styleable.roundedimageview_border_inside_color, defaultColor);
  58. }
  59. @Override
  60. protected void onDraw(Canvas canvas) {
  61. Drawable drawable = getDrawable();
  62. if (drawable == null) {
  63. return;
  64. }
  65. if (getWidth() == 0 || getHeight() == 0) {
  66. return;
  67. }
  68. this.measure(0, 0);
  69. if (drawable.getClass() == NinePatchDrawable.class)
  70. return;
  71. Bitmap b = ((BitmapDrawable) drawable).getBitmap();
  72. Bitmap bitmap = b.copy(Bitmap.Config.ARGB_8888, true);
  73. if (defaultWidth == 0) {
  74. defaultWidth = getWidth();
  75. }
  76. if (defaultHeight == 0) {
  77. defaultHeight = getHeight();
  78. }
  79. int radius = 0;
  80. if (mBorderInsideColor != defaultColor
  81. && mBorderOutsideColor != defaultColor) {// 定义画两个边框,分别为外圆边框和内圆边框
  82. radius = (defaultWidth < defaultHeight ? defaultWidth
  83. : defaultHeight) / 2 - 2 * mBorderThickness;
  84. // 画内圆
  85. drawCircleBorder(canvas, radius + mBorderThickness / 2,
  86. mBorderInsideColor);
  87. // 画外圆
  88. drawCircleBorder(canvas, radius + mBorderThickness
  89. + mBorderThickness / 2, mBorderOutsideColor);
  90. } else if (mBorderInsideColor != defaultColor
  91. && mBorderOutsideColor == defaultColor) {// 定义画一个边框
  92. radius = (defaultWidth < defaultHeight ? defaultWidth
  93. : defaultHeight) / 2 - mBorderThickness;
  94. drawCircleBorder(canvas, radius + mBorderThickness / 2,
  95. mBorderInsideColor);
  96. } else if (mBorderInsideColor == defaultColor
  97. && mBorderOutsideColor != defaultColor) {// 定义画一个边框
  98. radius = (defaultWidth < defaultHeight ? defaultWidth
  99. : defaultHeight) / 2 - mBorderThickness;
  100. drawCircleBorder(canvas, radius + mBorderThickness / 2,
  101. mBorderOutsideColor);
  102. } else {// 没有边框
  103. radius = (defaultWidth < defaultHeight ? defaultWidth
  104. : defaultHeight) / 2;
  105. }
  106. Bitmap roundBitmap = getCroppedRoundBitmap(bitmap, radius);
  107. canvas.drawBitmap(roundBitmap, defaultWidth / 2 - radius, defaultHeight
  108. / 2 - radius, null);
  109. }
  110. /**
  111. * 获取裁剪后的圆形图片
  112. */
  113. public Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
  114. Bitmap scaledSrcBmp;
  115. int diameter = radius * 2;
  116. // 为了防止宽高不相等,造成圆形图片变形,因此截取长方形中处于中间位置最大的正方形图片
  117. int bmpWidth = bmp.getWidth();
  118. int bmpHeight = bmp.getHeight();
  119. int squareWidth = 0, squareHeight = 0;
  120. int x = 0, y = 0;
  121. Bitmap squareBitmap;
  122. if (bmpHeight > bmpWidth) {// 高大于宽
  123. squareWidth = squareHeight = bmpWidth;
  124. x = 0;
  125. y = (bmpHeight - bmpWidth) / 2;
  126. // 截取正方形图片
  127. squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,
  128. squareHeight);
  129. } else if (bmpHeight < bmpWidth) {// 宽大于高
  130. squareWidth = squareHeight = bmpHeight;
  131. x = (bmpWidth - bmpHeight) / 2;
  132. y = 0;
  133. squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth,
  134. squareHeight);
  135. } else {
  136. squareBitmap = bmp;
  137. }
  138. if (squareBitmap.getWidth() != diameter
  139. || squareBitmap.getHeight() != diameter) {
  140. scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter,
  141. diameter, true);
  142. } else {
  143. scaledSrcBmp = squareBitmap;
  144. }
  145. Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(),
  146. scaledSrcBmp.getHeight(),
  147. Bitmap.Config.ARGB_8888);
  148. Canvas canvas = new Canvas(output);
  149. Paint paint = new Paint();
  150. Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(),
  151. scaledSrcBmp.getHeight());
  152. paint.setAntiAlias(true);
  153. paint.setFilterBitmap(true);
  154. paint.setDither(true);
  155. canvas.drawARGB(0, 0, 0, 0);
  156. canvas.drawCircle(scaledSrcBmp.getWidth() / 2,
  157. scaledSrcBmp.getHeight() / 2,
  158. scaledSrcBmp.getWidth() / 2,
  159. paint);
  160. paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
  161. canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
  162. bmp = null;
  163. squareBitmap = null;
  164. scaledSrcBmp = null;
  165. return output;
  166. }
  167. /**
  168. * 边缘画圆
  169. */
  170. private void drawCircleBorder(Canvas canvas, int radius, int color) {
  171. Paint paint = new Paint();
  172. /* 去锯齿 */
  173. paint.setAntiAlias(true);
  174. paint.setFilterBitmap(true);
  175. paint.setDither(true);
  176. paint.setColor(color);
  177. /* 设置paint的 style 为STROKE:空心 */
  178. paint.setStyle(Paint.Style.STROKE);
  179. /* 设置paint的外框宽度 */
  180. paint.setStrokeWidth(mBorderThickness);
  181. canvas.drawCircle(defaultWidth / 2, defaultHeight / 2, radius, paint);
  182. }
  183. }

这里值得注意的是,要使用这个必须自定义一些属性,我们在values下新建一个attr.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <resources>
  3. <declare-styleable name="roundedimageview">
  4. <attr name="border_thickness" format="dimension" />
  5. <attr name="border_inside_color" format="color" />
  6. <attr name="border_outside_color" format="color"></attr>
  7. </declare-styleable>
  8. </resources>

然后在xml文件中引入命名空间

  1. xmlns:imagecontrol="http://schemas.android.com/apk/res-auto"

我们直接看layout_mian.xml吧

layout_mian.xml

就一些布局咯

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:imagecontrol="http://schemas.android.com/apk/res-auto"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7. <RelativeLayout
  8. android:layout_width="match_parent"
  9. android:layout_height="wrap_content" >
  10. <com.lgl.whew.WhewView
  11. android:id="@+id/wv"
  12. android:layout_width="match_parent"
  13. android:layout_height="match_parent" />
  14. <com.lgl.whew.RoundImageView
  15. android:id="@+id/my_photo"
  16. android:layout_width="100dp"
  17. android:layout_height="100dp"
  18. android:layout_centerInParent="true"
  19. android:src="@drawable/myphoto"
  20. imagecontrol:border_inside_color="#bc0978"
  21. imagecontrol:border_outside_color="#ba3456"
  22. imagecontrol:border_thickness="1dp" />
  23. </RelativeLayout>
  24. </LinearLayout>

这样你就可以使用圆形图片了,我们接下来看波纹的绘制

WhewView

  1. package com.lgl.whew;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.util.AttributeSet;
  9. import android.view.View;
  10. /**
  11. * 模仿咻一咻
  12. *
  13. * @author LGL
  14. *
  15. */
  16. public class WhewView extends View {
  17. private Paint paint;
  18. private int maxWidth = 255;
  19. // 是否运行
  20. private boolean isStarting = false;
  21. private List<String> alphaList = new ArrayList<String>();
  22. private List<String> startWidthList = new ArrayList<String>();
  23. public WhewView(Context context, AttributeSet attrs, int defStyleAttr) {
  24. super(context, attrs, defStyleAttr);
  25. // TODO Auto-generated constructor stub
  26. init();
  27. }
  28. public WhewView(Context context, AttributeSet attrs) {
  29. super(context, attrs);
  30. // TODO Auto-generated constructor stub
  31. init();
  32. }
  33. public WhewView(Context context) {
  34. super(context);
  35. // TODO Auto-generated constructor stub
  36. init();
  37. }
  38. private void init() {
  39. paint = new Paint();
  40. // 设置博文的颜色
  41. paint.setColor(0x0059ccf5);
  42. alphaList.add("255");// 圆心的不透明度
  43. startWidthList.add("0");
  44. }
  45. @Override
  46. public void onDraw(Canvas canvas) {
  47. super.onDraw(canvas);
  48. setBackgroundColor(Color.TRANSPARENT);// 颜色:完全透明
  49. // 依次绘制 同心圆
  50. for (int i = 0; i < alphaList.size(); i++) {
  51. int alpha = Integer.parseInt(alphaList.get(i));
  52. // 圆半径
  53. int startWidth = Integer.parseInt(startWidthList.get(i));
  54. paint.setAlpha(alpha);
  55. // 这个半径决定你想要多大的扩散面积
  56. canvas.drawCircle(getWidth() / 2, getHeight() / 2, startWidth + 50,
  57. paint);
  58. // 同心圆扩散
  59. if (isStarting && alpha > 0 && startWidth < maxWidth) {
  60. alphaList.set(i, (alpha - 1) + "");
  61. startWidthList.set(i, (startWidth + 1) + "");
  62. }
  63. }
  64. if (isStarting
  65. && Integer
  66. .parseInt(startWidthList.get(startWidthList.size() - 1)) == maxWidth / 5) {
  67. alphaList.add("255");
  68. startWidthList.add("0");
  69. }
  70. // 同心圆数量达到10个,删除最外层圆
  71. if (isStarting && startWidthList.size() == 10) {
  72. startWidthList.remove(0);
  73. alphaList.remove(0);
  74. }
  75. // 刷新界面
  76. invalidate();
  77. }
  78. // 执行动画
  79. public void start() {
  80. isStarting = true;
  81. }
  82. // 停止动画
  83. public void stop() {
  84. isStarting = false;
  85. }
  86. // 判断是都在不在执行
  87. public boolean isStarting() {
  88. return isStarting;
  89. }
  90. }

这里我们看到,对外有几个方法,一个开始动画,一个停止动画,一个检测是否正在运行

MainActivity

这里就是我们的需求了,我反编译了一下支付宝的APK,并没有找到他的咻一咻的音效,就在他的raw目录下随便找了一个,我们现在是需要这样一个需求

  • 点击图片执行动画,并且每隔五分钟响一次
  • 再次点击图片,停止动画,停止音效

我们先新建一个raw文件夹把音效拷贝进去吧

  1. package com.lgl.whew;
  2. import android.app.Activity;
  3. import android.media.AudioManager;
  4. import android.media.SoundPool;
  5. import android.os.Bundle;
  6. import android.os.Handler;
  7. import android.view.View;
  8. import android.view.View.OnClickListener;
  9. public class MainActivity extends Activity {
  10. private WhewView wv;
  11. private RoundImageView my_photo;
  12. private static final int Nou = 1;
  13. // 声明一个SoundPool
  14. private SoundPool sp;
  15. // 定义一个整型用load();来设置suondIDf
  16. private int music;
  17. private Handler handler = new Handler() {
  18. public void handleMessage(android.os.Message msg) {
  19. if (msg.what == Nou) {
  20. // 每隔10s响一次
  21. handler.sendEmptyMessageDelayed(Nou, 5000);
  22. sp.play(music, 1, 1, 0, 0, 1);
  23. }
  24. }
  25. };
  26. @Override
  27. protected void onCreate(Bundle savedInstanceState) {
  28. super.onCreate(savedInstanceState);
  29. setContentView(R.layout.activity_main);
  30. initView();
  31. }
  32. private void initView() {
  33. // 第一个参数为同时播放数据流的最大个数,第二数据流类型,第三为声音质量
  34. sp = new SoundPool(10, AudioManager.STREAM_SYSTEM, 5);
  35. // 把你的声音素材放到res/raw里,第2个参数即为资源文件,第3个为音乐的优先级
  36. music = sp.load(this, R.raw.hongbao_gq, 1);
  37. wv = (WhewView) findViewById(R.id.wv);
  38. my_photo = (RoundImageView) findViewById(R.id.my_photo);
  39. my_photo.setOnClickListener(new OnClickListener() {
  40. @Override
  41. public void onClick(View v) {
  42. if(wv.isStarting()){
  43. //如果动画正在运行就停止,否则就继续执行
  44. wv.stop();
  45. //结束进程
  46. handler.removeMessages(Nou);
  47. }else{
  48. // 执行动画
  49. wv.start();
  50. handler.sendEmptyMessage(Nou);
  51. }
  52. }
  53. });
  54. }
  55. }

相信这里的逻辑不是很难吧,对了,我们在结束activity的时候也是要销毁这个进程的,不然…你懂的

  1. @Override
  2. protected void onDestroy() {
  3. // TODO Auto-generated method stub
  4. super.onDestroy();
  5. handler.removeMessages(Nou);
  6. }

我们运行一下,想听效果的可以下载Demo运行一下,我们这里做一个简单的演示

Android特效专辑(十二)——仿支付宝咻一咻功能实现波纹扩散特效,精细小巧的View - 图2

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