Advertisement
Tatarize

Drawables Moving Example

Sep 30th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.46 KB | None | 0 0
  1. package viewmotionexample.ballanimation;
  2.  
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Canvas;
  6. import android.graphics.Color;
  7. import android.graphics.Paint;
  8. import android.graphics.Rect;
  9. import android.graphics.drawable.Drawable;
  10. import android.util.AttributeSet;
  11. import android.view.MotionEvent;
  12. import android.view.View;
  13.  
  14. import java.util.ArrayList;
  15. import java.util.Random;
  16.  
  17.  
  18. /**
  19. * Created by David Olsen, 2015
  20. * Public Domain. Use for anything.
  21. */
  22.  
  23. public class CirclesView extends View {
  24. Random random = new Random();
  25. ArrayList<BallDrawing> circles = new ArrayList<>();
  26. Activity activity;
  27. Paint backgroundPaint = new Paint();
  28.  
  29. public CirclesView(Context context) {
  30. super(context);
  31. this.activity = (Activity) context;
  32. init();
  33. }
  34.  
  35. public CirclesView(Context context, AttributeSet attrs) {
  36. super(context, attrs);
  37. this.activity = (Activity) context;
  38. init();
  39. }
  40.  
  41. public CirclesView(Context context, AttributeSet attrs, int defStyleAttr) {
  42. super(context, attrs, defStyleAttr);
  43. this.activity = (Activity) context;
  44. init();
  45. }
  46.  
  47. @Override
  48. public boolean onTouchEvent(MotionEvent event) {
  49. int action = event.getAction();
  50. if (action == MotionEvent.ACTION_UP) {
  51. circles.add(new BallDrawing(event.getX(),event.getY()));
  52. }
  53. return true;
  54. }
  55.  
  56. Drawable drawable;
  57. Rect drawableBounds = null;
  58.  
  59. private void init() {
  60. backgroundPaint.setColor(Color.BLUE);
  61. drawable = getResources().getDrawable(R.drawable.bal);
  62. if (drawable != null) {
  63. drawable.setBounds(0,0,40,40);
  64. drawableBounds = drawable.getBounds();
  65. }
  66. Thread t = new Thread(new Runnable() {
  67. @Override
  68. public void run() {
  69.  
  70. while (true) {
  71. try {
  72. Thread.sleep(100);
  73. tick();
  74.  
  75. } catch (InterruptedException e) {
  76. return;
  77. }
  78. }
  79. }
  80. });
  81. t.start();
  82. }
  83.  
  84. public void tick() {
  85. activity.runOnUiThread(new Runnable() {
  86. @Override
  87. public void run() {
  88. for (BallDrawing circle : circles) {
  89. circle.translate(random.nextInt(10) - 5, random.nextInt(10) - 5);
  90. }
  91. }
  92. });
  93.  
  94. }
  95.  
  96.  
  97. @Override
  98. protected void onDraw(Canvas canvas) {
  99. for (BallDrawing c : circles) {
  100. c.draw(canvas);
  101. }
  102. }
  103.  
  104. private class BallDrawing {
  105. public float cx, cy;
  106.  
  107. public BallDrawing(float cx, float cy) {
  108. this.cx = cx;
  109. this.cy = cy;
  110. }
  111.  
  112. public void translate(int dx, int dy) {
  113. invalidate((int)cx + drawableBounds.left,(int)cy + drawableBounds.top, (int)cx+1 + drawableBounds.right, (int)cy+1 + drawableBounds.bottom);
  114. cx += dx;
  115. cy += dy;
  116. invalidate((int)cx + drawableBounds.left,(int)cy + drawableBounds.top, (int)cx + 1 + drawableBounds.right, (int)cy+1 + drawableBounds.bottom);
  117. }
  118.  
  119.  
  120. public void draw(Canvas canvas) {
  121. canvas.save();
  122. canvas.translate(cx,cy);
  123. drawable.draw(canvas);
  124. canvas.restore();
  125. }
  126. }
  127.  
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement