PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/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
 17package com.marvin.rocklock;
 18
 19import android.content.Context;
 20import android.graphics.Canvas;
 21import android.graphics.Color;
 22import android.graphics.Paint;
 23import android.graphics.Path;
 24import android.view.View;
 25
 26/**
 27 * Draws an arrow to give the user some visual indication of where they are
 28 * stroking.
 29 * 
 30 * @author clchen@google.com (Charles L. Chen)
 31 */
 32public class AnimationLayer extends View {
 33    private int dir = 5;
 34
 35    private Path arrow = new Path();
 36
 37    private Paint paint = new Paint();
 38
 39    public AnimationLayer(Context context) {
 40        super(context);
 41        arrow.moveTo(0, -50);
 42        arrow.lineTo(-20, 60);
 43        arrow.lineTo(0, 50);
 44        arrow.lineTo(20, 60);
 45        arrow.close();
 46    }
 47
 48    public void setDirection(int direction) {
 49        dir = direction;
 50        invalidate();
 51    }
 52
 53    @Override
 54    public void onDraw(Canvas canvas) {
 55        canvas.drawColor(Color.BLACK);
 56
 57        int w = canvas.getWidth();
 58        int h = canvas.getHeight();
 59        int cx = w / 2;
 60        int cy = (h * 2) / 3;
 61
 62        paint.setColor(Color.WHITE);
 63
 64        canvas.translate(cx, cy);
 65        switch (dir) {
 66            case 1:
 67                canvas.rotate(-45);
 68                break;
 69            case 2:
 70                canvas.rotate(0);
 71                break;
 72            case 3:
 73                canvas.rotate(45);
 74                break;
 75            case 4:
 76                canvas.rotate(-90);
 77                break;
 78            case 5:
 79                canvas.rotate(0);
 80                break;
 81            case 6:
 82                canvas.rotate(90);
 83                break;
 84            case 7:
 85                canvas.rotate(-135);
 86                break;
 87            case 8:
 88                canvas.rotate(180);
 89                break;
 90            case 9:
 91                canvas.rotate(135);
 92                break;
 93        }
 94
 95        if ((dir >= 0) && (dir != 5)) {
 96            canvas.drawPath(arrow, paint);
 97        }
 98    }
 99
100}