1 /* 2 * Copyright (C) 2007 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of 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, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.example.android.apis.graphics; 18 19 // Need the following import to get access to the app resources, since this 20 // class is in a sub-package. 21 //import com.example.android.apis.R; 22 23 import android.content.Context; 24 import android.graphics.*; 25 import android.os.Bundle; 26 import android.view.View; 27 28 public class Arcs extends GraphicsActivity { 29 30 @Override 31 protected void onCreate(Bundle savedInstanceState) { 32 super.onCreate(savedInstanceState); 33 setContentView(new SampleView(this)); 34 } 35 36 private static class SampleView extends View { 37 private Paint[] mPaints; 38 private Paint mFramePaint; 39 private boolean[] mUseCenters; 40 private RectF[] mOvals; 41 private RectF mBigOval; 42 private float mStart; 43 private float mSweep; 44 private int mBigIndex; 45 46 private static final float SWEEP_INC = 2; 47 private static final float START_INC = 15; 48 49 public SampleView(Context context) { 50 super(context); 51 52 mPaints = new Paint[4]; 53 mUseCenters = new boolean[4]; 54 mOvals = new RectF[4]; 55 56 mPaints[0] = new Paint(); 57 mPaints[0].setAntiAlias(true); 58 mPaints[0].setStyle(Paint.Style.FILL); 59 mPaints[0].setColor(0x88FF0000); 60 mUseCenters[0] = false; 61 62 mPaints[1] = new Paint(mPaints[0]); 63 mPaints[1].setColor(0x8800FF00); 64 mUseCenters[1] = true; 65 66 mPaints[2] = new Paint(mPaints[0]); 67 mPaints[2].setStyle(Paint.Style.STROKE); 68 mPaints[2].setStrokeWidth(4); 69 mPaints[2].setColor(0x880000FF); 70 mUseCenters[2] = false; 71 72 mPaints[3] = new Paint(mPaints[2]); 73 mPaints[3].setColor(0x88888888); 74 mUseCenters[3] = true; 75 76 mBigOval = new RectF(40, 10, 280, 250); 77 78 mOvals[0] = new RectF( 10, 270, 70, 330); 79 mOvals[1] = new RectF( 90, 270, 150, 330); 80 mOvals[2] = new RectF(170, 270, 230, 330); 81 mOvals[3] = new RectF(250, 270, 310, 330); 82 83 mFramePaint = new Paint(); 84 mFramePaint.setAntiAlias(true); 85 mFramePaint.setStyle(Paint.Style.STROKE); 86 mFramePaint.setStrokeWidth(0); 87 } 88 89 private void drawArcs(Canvas canvas, RectF oval, boolean useCenter, 90 Paint paint) { 91 canvas.drawRect(oval, mFramePaint); 92 canvas.drawArc(oval, mStart, mSweep, useCenter, paint); 93 } 94 95 @Override protected void onDraw(Canvas canvas) { 96 canvas.drawColor(Color.WHITE); 97 98 drawArcs(canvas, mBigOval, mUseCenters[mBigIndex], 99 mPaints[mBigIndex]); 100 101 for (int i = 0; i < 4; i++) { 102 drawArcs(canvas, mOvals[i], mUseCenters[i], mPaints[i]); 103 } 104 105 mSweep += SWEEP_INC; 106 if (mSweep > 360) { 107 mSweep -= 360; 108 mStart += START_INC; 109 if (mStart >= 360) { 110 mStart -= 360; 111 } 112 mBigIndex = (mBigIndex + 1) % mOvals.length; 113 } 114 invalidate(); 115 } 116 } 117 } 118 119