Advertisement
Guest User

Untitled

a guest
May 25th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.00 KB | None | 0 0
  1. public class PaintObject extends View
  2. {
  3. Paint paint;
  4. public int x;
  5. public int y;
  6.  
  7. public PaintObject(Context context, int dx, int dy)
  8. {
  9. super(context);
  10.  
  11. paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  12. paint.setColor(getResources().getColor(R.color.blue));
  13.  
  14. this.x = dx;
  15. this.y = dy;
  16.  
  17. paints.add(this);
  18. }
  19.  
  20. public void update()
  21. {
  22. x += 5;
  23. y += 5;
  24. }
  25.  
  26. @Override
  27. public void onDraw(Canvas canvas)
  28. {
  29. if (!paints.contains(this))
  30. return;
  31.  
  32. canvas.drawRect(x, y, x + 20, y + 20, paint);
  33.  
  34. invalidate();
  35. }
  36.  
  37. for (PaintObject p : paints)
  38. {
  39. if (p.x > screenWidth || p.y > screenHeight)
  40. {
  41. paints.remove(p);
  42. p = null; // compiler says it's never used, but I'm making this for sure so GC will collect it
  43. }
  44. else
  45. p.update();
  46. }
  47.  
  48. layout.addView(new PaintObject (this, 10, 10)); //layout is current layout; xy are randomized in reality
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement