Guest User

Untitled

a guest
Aug 20th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.45 KB | None | 0 0
  1. Animate a Drawable in a custom view
  2. public class CheckerBoard extends View {
  3.  
  4. public enum State implements Parcelable {
  5. EMPTY(0), WHITE(1), BLACK(2);
  6. }
  7.  
  8. private final State[][] boardStates = new State[SIZE][SIZE];
  9.  
  10. @Override
  11. protected void onDraw(Canvas canvas) {
  12. canvas.drawColor(bgColor);
  13. for (int y = 0; y < SIZE; y++) {
  14. for (int x = 0; x < SIZE; x++) {
  15. if ((y % 2 == 0 && x % 2 != 0) || (y % 2 != 0 && x % 2 == 0)) {
  16. drawRect(x, y, canvas);
  17. drawPawn(x, y, canvas);
  18. }
  19. }
  20. }
  21. }
  22.  
  23. private void drawRect(int x, int y, Canvas c) {
  24. }
  25.  
  26. private void drawPawn(int x, int y, Canvas c) {
  27. }
  28.  
  29. private void init() {
  30. setupBoard();
  31. pawnLinePaint.setStyle(Paint.Style.STROKE);
  32. wPawnDrawable.getPaint().setColor(wColor);
  33. wPawnDrawable.getPaint().setShadowLayer(tileSize + 2, 4, 4, Color.GRAY);
  34. bPawnDrawable.getPaint().setColor(bColor);
  35. bPawnDrawable.getPaint().setShadowLayer(tileSize + 2, 4, 4, Color.GRAY);
  36. playerState = startState;
  37. }
  38.  
  39. public boolean onTouchEvent(MotionEvent event) {
  40. switch (event.getAction()) {
  41. case MotionEvent.ACTION_DOWN:
  42. int x = (int) (event.getX() / tileSize);
  43. int y = (int) (event.getY() / tileSize);
  44. if (selection[0] >= 0) { // A tile is already selected
  45. if (isValidMove(selection[0], selection[1], x, y)) {
  46. makeMove(x, y);
  47. clearSelection();
  48. switchPlayer();
  49. invalidate();
  50. }
  51.  
  52. } else { // New selection
  53. if (isValidSelection(x, y)) {
  54. selection[0] = x;
  55. selection[1] = y;
  56. invalidate();
  57. }
  58. }
  59.  
  60. return true;
  61. default:
  62. return super.onTouchEvent(event);
  63. }
  64. }
  65.  
  66. private void makeMove(int x, int y) {
  67. // Move the pawn to the new square
  68. boardStates[y][x] = boardStates[selection[1]][selection[0]];
  69. // Old square is now empty
  70. boardStates[selection[1]][selection[0]] = State.EMPTY;
  71. }
  72.  
  73. private void switchPlayer() {
  74. playerState = playerState == State.WHITE ? State.BLACK : State.WHITE;
  75. }
  76.  
  77. public CheckerBoard(Context context) {
  78. super(context);
  79. init();
  80. }
  81.  
  82. public CheckerBoard(Context context, AttributeSet attrs) {
  83. super(context, attrs);
  84. init();
  85. }
  86.  
  87. public CheckerBoard(Context context, AttributeSet attrs, int defStyle) {
  88. super(context, attrs, defStyle);
  89. init();
  90. }
  91.  
  92.  
  93. private class Pawn extends ShapeDrawable {
  94. public Pawn() {
  95. super(new OvalShape());
  96. }
  97.  
  98. public void drawWithCircles(Canvas canvas, float x, float y){
  99. super.draw(canvas);
  100. canvas.drawCircle(x * tileSize + pawnDiameter, y * tileSize + pawnDiameter, pawnDiameter - pawnPadding,
  101. pawnLinePaint);
  102. canvas.drawCircle(x * tileSize + pawnDiameter, y * tileSize + pawnDiameter, pawnDiameter - pawnPadding * 6,
  103. pawnLinePaint);
  104. canvas.drawCircle(x * tileSize + pawnDiameter, y * tileSize + pawnDiameter, pawnDiameter - pawnPadding * 8,
  105. pawnLinePaint);
  106. }
  107. }
  108.  
  109. }
Add Comment
Please, Sign In to add comment