Guest User

Untitled

a guest
Jan 4th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.25 KB | None | 0 0
  1. package com.canvas.animacija.animacijasuncevogsistema;
  2.  
  3. import android.support.v7.app.AppCompatActivity;
  4. i
  5. mport android.os.Bundle;
  6.  
  7. public class MainActivity extends AppCompatActivity {
  8.  
  9. AnimationLayout animation;
  10.  
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14.  
  15. animation = new AnimationLayout(this);
  16.  
  17. setContentView(animation);
  18. }
  19.  
  20. @Override
  21. protected void onPause() {
  22. super.onPause();
  23. animation.pause();
  24. }
  25.  
  26. @Override
  27. protected void onResume() {
  28. super.onResume();
  29. animation.resume();
  30. }
  31. }
  32.  
  33. package com.canvas.animacija.animacijasuncevogsistema;
  34.  
  35. import android.content.Context;
  36. import android.graphics.Canvas;
  37. import android.graphics.Color;
  38. import android.graphics.Paint;
  39. import android.view.SurfaceHolder;
  40. import android.view.SurfaceView;
  41.  
  42. public class AnimationLayout extends SurfaceView implements Runnable {
  43.  
  44. Thread thread;
  45. boolean canDraw;
  46.  
  47. Canvas canvas;
  48. SurfaceHolder holder;
  49.  
  50. Paint yellow_fill_and_stroke, red_fill, green_fill, blue_fill, magenta_fill;
  51.  
  52. int circle1_x, circle1_y, circle2_x, circle2_y, circle3_x, circle3_y, circle4_x, circle4_y,
  53. circle5_x, circle5_y;
  54. int radius1, radius2, radius3, radius4, radius5;
  55.  
  56. public AnimationLayout(Context context) {
  57. super(context);
  58.  
  59. thread = null;
  60. canDraw = false;
  61.  
  62. holder = getHolder();
  63.  
  64. circle1_x = toPxs(180);
  65. circle1_y = toPxs(250);
  66. radius1 = toPxs(30);
  67.  
  68. circle2_x = toPxs(180);
  69. circle2_y = toPxs(240) - radius1;
  70. radius2 = toPxs(7);
  71.  
  72. }
  73.  
  74. @Override
  75. public void run() {
  76.  
  77. prepareBrushes();
  78.  
  79. while (canDraw) {
  80.  
  81. if (!holder.getSurface().isValid()) {
  82. continue;
  83. }
  84.  
  85. canvas = holder.lockCanvas();
  86. canvas.drawCircle(circle1_x, circle1_y, radius1, yellow_fill_and_stroke);
  87. canvas.drawCircle(circle2_x, circle2_y, radius2, red_fill);
  88. moveToRight(circle2_x, circle2_y, radius2, 10);
  89. holder.unlockCanvasAndPost(canvas);
  90.  
  91. }
  92.  
  93. }
  94.  
  95. public void pause() {
  96.  
  97. canDraw = false;
  98.  
  99. while (true) {
  100. try {
  101. thread.join();
  102. break;
  103. } catch (InterruptedException ie) {
  104. ie.printStackTrace();
  105. }
  106. }
  107.  
  108. thread = null;
  109.  
  110. }
  111.  
  112. public void resume() {
  113.  
  114. canDraw = true;
  115. thread = new Thread(this, "Animation Thread");
  116. thread.start();
  117.  
  118. }
  119.  
  120. private void prepareBrushes() {
  121. yellow_fill_and_stroke = new Paint();
  122. yellow_fill_and_stroke.setColor(Color.YELLOW);
  123. yellow_fill_and_stroke.setStyle(Paint.Style.FILL_AND_STROKE);
  124. yellow_fill_and_stroke.setStrokeWidth(toPxs(3));
  125.  
  126. red_fill = new Paint();
  127. red_fill.setColor(Color.RED);
  128. red_fill.setStyle(Paint.Style.FILL);
  129.  
  130. green_fill = new Paint();
  131. green_fill.setColor(Color.GREEN);
  132. green_fill.setStyle(Paint.Style.FILL);
  133.  
  134. blue_fill = new Paint();
  135. blue_fill.setColor(Color.BLUE);
  136. blue_fill.setStyle(Paint.Style.FILL);
  137.  
  138. magenta_fill = new Paint();
  139. magenta_fill.setColor(Color.MAGENTA);
  140. magenta_fill.setStyle(Paint.Style.FILL);
  141. }
  142.  
  143. private int toPxs(int dps) {
  144. return (int) (dps * getResources().getDisplayMetrics().density);
  145. }
  146.  
  147. private void moveToRight(int coordinate_x, int coordinate_y, int radius, int speed) {
  148. int d_speed = toPxs(speed);
  149.  
  150. if (coordinate_x >= coordinate_y - radius/2 && coordinate_y >= coordinate_x + radius/2 ) {
  151. coordinate_x += d_speed;
  152. coordinate_y += d_speed;
  153. }
  154.  
  155. if (coordinate_x >= coordinate_y + radius/2 && coordinate_y < coordinate_x + radius/2) {
  156. coordinate_x -= d_speed;
  157. coordinate_y += d_speed;
  158. }
  159.  
  160. if (coordinate_x < coordinate_y + radius/2 && coordinate_y >= coordinate_x - radius/2) {
  161. coordinate_x -= d_speed;
  162. coordinate_y -= d_speed;
  163. }
  164.  
  165. if (coordinate_x < coordinate_y - radius/2 && coordinate_y < coordinate_x - radius/2) {
  166. coordinate_x += d_speed;
  167. coordinate_y -= d_speed;
  168. }
  169. }
  170. }
Add Comment
Please, Sign In to add comment