[关闭]
@Tyhj 2017-02-24T12:31:24.000000Z 字数 4072 阅读 1238

Android粒子特效

Android


本文固定链接:https://www.zybuluo.com/Tyhj/note/602758

特效之连接

废话不多说

  1. package myviews;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.os.Handler;
  7. import android.util.AttributeSet;
  8. import android.util.Log;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import java.util.ArrayList;
  12. import java.util.List;
  13. import java.util.Random;
  14. /**
  15. * Created by Tyhj on 2016/12/15.
  16. */
  17. public class Myviews extends View {
  18. float touchX=0;
  19. float touchY=0;
  20. boolean isTouch;
  21. Paint paint;
  22. int width;
  23. int height;
  24. List<Mycircle> mycircleList;
  25. int distance=70;
  26. float speed=1.7f;
  27. int count=75;
  28. float radius=3.5f;
  29. public Myviews(Context context, AttributeSet attrs) {
  30. super(context, attrs);
  31. }
  32. public Myviews(Context context) {
  33. super(context);
  34. }
  35. public Myviews(Context context, AttributeSet attrs, int defStyle) {
  36. super(context, attrs, defStyle);
  37. }
  38. @Override
  39. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  40. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  41. //获取布局大小
  42. width=MeasureSpec.getSize(widthMeasureSpec);
  43. height=MeasureSpec.getSize(heightMeasureSpec);
  44. initialize();
  45. }
  46. @Override
  47. protected void onDraw(Canvas canvas) {
  48. super.onDraw(canvas);
  49. paint=new Paint();
  50. paint.setAntiAlias(true);
  51. //设置颜色
  52. paint.setColor(Color.BLACK);
  53. //设置背景
  54. canvas.drawColor(Color.WHITE);
  55. for(int i=0;i<mycircleList.size();i++){
  56. if(mycircleList.get(i).startX>width||mycircleList.get(i).startX<0) {
  57. mycircleList.get(i).speedX = -mycircleList.get(i).speedX;
  58. }
  59. if(mycircleList.get(i).startY>height||mycircleList.get(i).startY<0) {
  60. mycircleList.get(i).speedY = -mycircleList.get(i).speedY;
  61. }
  62. mycircleList.get(i).startX=mycircleList.get(i).startX+mycircleList.get(i).speedX;
  63. mycircleList.get(i).startY=mycircleList.get(i).startY+mycircleList.get(i).speedY;
  64. //画圆
  65. canvas.drawCircle(mycircleList.get(i).startX,mycircleList.get(i).startY,mycircleList.get(i).radius,mycircleList.get(i).paint);
  66. }
  67. //画连接线
  68. for(int i=0;i<mycircleList.size()-1;i++){
  69. for(int j=i+1;j<mycircleList.size();j++){
  70. float x=mycircleList.get(i).startX-mycircleList.get(j).startX;
  71. float y=mycircleList.get(i).startY-mycircleList.get(j).startY;
  72. if(x*x+y*y<mycircleList.get(i).distance*distance*distance){
  73. canvas.drawLine(mycircleList.get(i).startX,mycircleList.get(i).startY,
  74. mycircleList.get(j).startX,mycircleList.get(j).startY,mycircleList.get(i).paint);
  75. }
  76. }
  77. }
  78. //点击时候处理
  79. if(isTouch){
  80. canvas.drawCircle(touchX,touchY,5,paint);
  81. for(int i=0;i<mycircleList.size();i++){
  82. float x=mycircleList.get(i).startX-touchX;
  83. float y=mycircleList.get(i).startY-touchY;
  84. if(x*x+y*y<12*distance*distance){
  85. canvas.drawLine(mycircleList.get(i).startX,mycircleList.get(i).startY,
  86. touchX,touchY,mycircleList.get(i).paint);
  87. }
  88. }
  89. }
  90. //刷新会重新执行这个方法,所以一直执行下去------重点
  91. invalidate();
  92. }
  93. @Override
  94. public boolean onTouchEvent(MotionEvent event) {
  95. switch (event.getAction()){
  96. case MotionEvent.ACTION_DOWN:
  97. isTouch=true;
  98. touchX=event.getX();
  99. touchY=event.getY();
  100. Log.e("x,y","DOWN");
  101. break;
  102. case MotionEvent.ACTION_MOVE:
  103. isTouch=true;
  104. touchX=event.getX();
  105. touchY=event.getY();
  106. Log.e("x,y","MOVE");
  107. break;
  108. case MotionEvent.ACTION_UP:
  109. isTouch=false;
  110. touchX=-1;
  111. touchY=-1;
  112. Log.e("x,y","UP");
  113. break;
  114. }
  115. return true;
  116. }
  117. private void initialize() {
  118. mycircleList=new ArrayList<Mycircle>();
  119. Random random = new Random(System.currentTimeMillis());
  120. for (int i=0;i<count;i++) {
  121. int distance=1;
  122. int alpha = random.nextInt(60)+40;
  123. float radius = this.radius*random.nextFloat()+0.5f;
  124. float startX = random.nextInt(width);
  125. float startY = random.nextInt(height);
  126. float speedX = speed - 2*speed*random.nextFloat();
  127. float speedY = speed - 2*speed*random.nextFloat();
  128. if(speedX==0)
  129. speedX=1;
  130. if(speedY==0)
  131. speedY=1;
  132. if(i%50==0)
  133. distance=4;
  134. else if(i%30==0)
  135. distance=3;
  136. else if(i%20==0)
  137. distance=2;
  138. Mycircle mycircle=new Mycircle(radius,startX,startY,speedX,speedY,alpha,distance);
  139. mycircleList.add(mycircle);
  140. }
  141. }
  142. class Mycircle{
  143. int distance;
  144. Paint paint;
  145. float radius;
  146. float startX;
  147. float startY;
  148. float speedX;
  149. float speedY;
  150. int alpha;
  151. public Mycircle(float radius, float startX, float startY, float speedX, float speedY, int alpha,int distance) {
  152. this.radius = radius;
  153. this.startX = startX;
  154. this.startY = startY;
  155. this.speedX = speedX;
  156. this.speedY = speedY;
  157. this.alpha = alpha;
  158. paint=new Paint();
  159. paint.setAntiAlias(true);
  160. //设置颜色
  161. paint.setColor(Color.BLACK);
  162. paint.setAlpha(alpha);
  163. this.distance=distance;
  164. }
  165. }
  166. }
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注