/documentation/RockLockTutorial/RockLock_03/src/com/marvin/rocklock/AnimationLayer.java

http://eyes-free.googlecode.com/ · Java · 100 lines · 66 code · 13 blank · 21 comment · 4 complexity · 48c7c578040735951f17ab55017c6f78 MD5 · raw file

  1. /*
  2. * Copyright (C) 2010 Google Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.marvin.rocklock;
  17. import android.content.Context;
  18. import android.graphics.Canvas;
  19. import android.graphics.Color;
  20. import android.graphics.Paint;
  21. import android.graphics.Path;
  22. import android.view.View;
  23. /**
  24. * Draws an arrow to give the user some visual indication of where they are
  25. * stroking.
  26. *
  27. * @author clchen@google.com (Charles L. Chen)
  28. */
  29. public class AnimationLayer extends View {
  30. private int dir = 5;
  31. private Path arrow = new Path();
  32. private Paint paint = new Paint();
  33. public AnimationLayer(Context context) {
  34. super(context);
  35. arrow.moveTo(0, -50);
  36. arrow.lineTo(-20, 60);
  37. arrow.lineTo(0, 50);
  38. arrow.lineTo(20, 60);
  39. arrow.close();
  40. }
  41. public void setDirection(int direction) {
  42. dir = direction;
  43. invalidate();
  44. }
  45. @Override
  46. public void onDraw(Canvas canvas) {
  47. canvas.drawColor(Color.BLACK);
  48. int w = canvas.getWidth();
  49. int h = canvas.getHeight();
  50. int cx = w / 2;
  51. int cy = (h * 2) / 3;
  52. paint.setColor(Color.WHITE);
  53. canvas.translate(cx, cy);
  54. switch (dir) {
  55. case 1:
  56. canvas.rotate(-45);
  57. break;
  58. case 2:
  59. canvas.rotate(0);
  60. break;
  61. case 3:
  62. canvas.rotate(45);
  63. break;
  64. case 4:
  65. canvas.rotate(-90);
  66. break;
  67. case 5:
  68. canvas.rotate(0);
  69. break;
  70. case 6:
  71. canvas.rotate(90);
  72. break;
  73. case 7:
  74. canvas.rotate(-135);
  75. break;
  76. case 8:
  77. canvas.rotate(180);
  78. break;
  79. case 9:
  80. canvas.rotate(135);
  81. break;
  82. }
  83. if ((dir >= 0) && (dir != 5)) {
  84. canvas.drawPath(arrow, paint);
  85. }
  86. }
  87. }