Advertisement
Guest User

Untitled

a guest
Aug 14th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.24 KB | None | 0 0
  1. public class CViewN extends View {
  2.  
  3. int width;
  4. int height;
  5. int gapV;
  6. int x;
  7. int y;
  8. Paint vaweP;
  9. Paint cPaint1;
  10. Paint cPaint2;
  11. Paint cPaint3;
  12. Paint cPaint4;
  13. int alpha = 0;
  14.  
  15. RectF rectCircle;
  16.  
  17. float initialRadius;
  18. float radiusOffset;
  19.  
  20. ValueAnimator anim = ValueAnimator.ofFloat(0, 35);
  21.  
  22. int mActivePointerId = MotionEvent.INVALID_POINTER_ID;
  23. float mLastTouchX;
  24. float mLastTouchY;
  25.  
  26. float mPosX = 0f;
  27. float mPosY = 0f;
  28.  
  29. @Override
  30. public boolean onTouchEvent(MotionEvent ev){
  31. final int action = ev.getActionMasked();
  32.  
  33. switch (action){
  34. case MotionEvent.ACTION_DOWN:
  35. {
  36. final int pointerIndex = ev.getActionIndex();
  37. final float x = ev.getX(pointerIndex);
  38. final float y = ev.getY(pointerIndex);
  39.  
  40. // Remember where we started (for dragging)
  41. mLastTouchX = x;
  42. mLastTouchY = y;
  43.  
  44. mActivePointerId = ev.getPointerId(pointerIndex);
  45. break;
  46. }
  47.  
  48. case MotionEvent.ACTION_MOVE:
  49. {
  50. // Find the index of the active pointer and fetch its position
  51. final int pointerIndex = ev.findPointerIndex(mActivePointerId);
  52.  
  53. final float x = ev.getX(pointerIndex);
  54. final float y = ev.getY(pointerIndex);
  55.  
  56. // Calculate the distance moved
  57. final float dx = x - mLastTouchX;
  58. final float dy = y - mLastTouchY;
  59.  
  60. mPosX += dx;
  61. mPosY += dy;
  62.  
  63. // Remember this touch position for the next move event
  64. mLastTouchX = x;
  65. mLastTouchY = y;
  66.  
  67. invalidate();
  68.  
  69. break;
  70. }
  71.  
  72. case MotionEvent.ACTION_UP:
  73. {
  74. mActivePointerId = MotionEvent.INVALID_POINTER_ID;
  75. break;
  76. }
  77.  
  78. case MotionEvent.ACTION_CANCEL:
  79. {
  80. mActivePointerId = MotionEvent.INVALID_POINTER_ID;
  81. break;
  82. }
  83.  
  84. case MotionEvent.ACTION_POINTER_UP:
  85. {
  86. final int pointerIndex = ev.getActionIndex();
  87. final int pointerId = ev.getPointerId(pointerIndex);
  88.  
  89. if(pointerId == mActivePointerId){
  90. // This was our active pointer going up. Choose a new
  91. // active pointer and adjust accordingly.
  92.  
  93. final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
  94.  
  95. mLastTouchX = ev.getX(newPointerIndex);
  96. mLastTouchY = ev.getY(newPointerIndex);
  97. mActivePointerId = ev.getPointerId(newPointerIndex);
  98. }
  99.  
  100. break;
  101. }
  102. }
  103.  
  104. return true;
  105. }
  106.  
  107.  
  108. public CViewN(Context context){
  109. //this(context, null, 0);
  110. super(context);
  111. }
  112.  
  113. public CViewN(Context context, AttributeSet attrs){
  114. //this(context, attrs, 0);
  115. super(context, attrs);
  116. init(context, attrs);
  117. }
  118.  
  119. public CViewN(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
  120.  
  121. super(context, attrs, defStyleAttr);
  122. init(context, attrs);
  123. }
  124.  
  125. @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  126. public CViewN(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes){
  127. super(context, attrs, defStyleAttr, defStyleRes);
  128. init(context, attrs);
  129. }
  130.  
  131. @Override
  132. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
  133.  
  134. int widthSize = MeasureSpec.getSize(widthMeasureSpec);
  135. int heightSize = MeasureSpec.getSize(heightMeasureSpec);
  136. int widthMode = MeasureSpec.getMode(widthMeasureSpec);
  137. int heightMode = MeasureSpec.getMode(heightMeasureSpec);
  138.  
  139. int desiredWidth = 1000;
  140. int desiredHeight = 1500;
  141.  
  142. int width;
  143. int height;
  144.  
  145. //width
  146. if(widthMode == MeasureSpec.EXACTLY){
  147. //Must be this size
  148. width = widthSize;
  149.  
  150. }else if(widthMode == MeasureSpec.AT_MOST){
  151. //Can't be bigger than...
  152. width = Math.min(widthSize, desiredWidth);
  153.  
  154. }else{
  155. //Be whatever you want
  156. width = desiredWidth;
  157. }
  158.  
  159. //height
  160. if(heightMode == MeasureSpec.EXACTLY){
  161. //Must be this size
  162. height = heightSize;
  163.  
  164. }else if
  165. (heightMode == MeasureSpec.AT_MOST){
  166. //Can't be bigger than...
  167. height = Math.min(heightSize, desiredHeight);
  168.  
  169. }else{
  170. //Be whatever you want
  171. height = desiredHeight;
  172. }
  173.  
  174. //MUST CALL THIS
  175. setMeasuredDimension(width, height);
  176. }
  177.  
  178. @Override
  179. protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  180. super.onSizeChanged(w, h, oldw, oldh);
  181.  
  182. width = w;
  183. height = h;
  184. x = width/2;
  185. y = height/2;
  186. }
  187.  
  188.  
  189.  
  190. protected void init(Context context, @Nullable AttributeSet attrs) {
  191.  
  192. vaweP = new Paint(Paint.ANTI_ALIAS_FLAG);
  193. vaweP.setStyle(Paint.Style.STROKE);
  194. vaweP.setColor(Color.RED);
  195. vaweP.setStrokeWidth(5);
  196. gapV = 30;
  197. }
  198.  
  199. @Override
  200. protected void onAttachedToWindow(){
  201. super.onAttachedToWindow();
  202.  
  203. anim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
  204. @Override
  205. public void onAnimationUpdate(ValueAnimator valueAnimator) {
  206. radiusOffset = (float) valueAnimator.getAnimatedValue();
  207. alpha = (int) ((float) valueAnimator.getAnimatedValue());
  208. invalidate();
  209. }
  210. });
  211. anim.setDuration(1000);
  212. anim.setInterpolator(new LinearInterpolator());
  213. anim.setRepeatMode(ValueAnimator.RESTART);
  214. anim.setRepeatCount(ValueAnimator.INFINITE);
  215. anim.start();
  216.  
  217.  
  218. }
  219.  
  220. @Override
  221. protected void onDraw(Canvas canvas) {
  222. super.onDraw(canvas);
  223.  
  224. /* float currentRadius;
  225. currentRadius = initialRadius;
  226.  
  227. while (currentRadius < (width/2)) {
  228. canvas.drawCircle(x, y, currentRadius, vaweP);
  229. currentRadius += gapV;
  230. }*/
  231. float radius = initialRadius + radiusOffset + mPosY;
  232. for(int i = 0; i < 10;i++ ){
  233. canvas.drawCircle(x, y, radius, vaweP);
  234. radius = radius + 35;
  235. }
  236.  
  237. rectCircle = new RectF(0,0, width, height);
  238.  
  239. cPaint1 = new Paint(Paint.ANTI_ALIAS_FLAG);
  240. cPaint1.setColor(Color.GREEN);
  241. cPaint1.setAlpha(30);
  242.  
  243. cPaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
  244. cPaint2.setColor(Color.RED);
  245. cPaint2.setAlpha(30);
  246.  
  247. cPaint3 = new Paint(Paint.ANTI_ALIAS_FLAG);
  248. cPaint3.setColor(Color.BLUE);
  249. cPaint3.setAlpha(30);
  250.  
  251. cPaint4 = new Paint(Paint.ANTI_ALIAS_FLAG);
  252. cPaint4.setColor(Color.YELLOW);
  253. cPaint4.setAlpha(30);
  254.  
  255. //canvas.drawRoundRect(rectCircle, 10, 10, cPaint);
  256. canvas.drawArc(rectCircle, alpha, 90, true, cPaint1);
  257. canvas.drawArc(rectCircle, alpha + 90, 90, true, cPaint2);
  258. canvas.drawArc(rectCircle, alpha + 180, 90, true, cPaint3);
  259. canvas.drawArc(rectCircle, alpha + 270, 90, true, cPaint4);
  260.  
  261. }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement