• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.app.Activity;
24 import android.content.Context;
25 import android.graphics.*;
26 import android.os.Bundle;
27 import android.view.View;
28 
29 public class Arcs extends GraphicsActivity {
30 
31     @Override
onCreate(Bundle savedInstanceState)32     protected void onCreate(Bundle savedInstanceState) {
33         super.onCreate(savedInstanceState);
34         setContentView(new SampleView(this));
35     }
36 
37     private static class SampleView extends View {
38         private Paint[] mPaints;
39         private Paint mFramePaint;
40         private boolean[] mUseCenters;
41         private RectF[] mOvals;
42         private RectF mBigOval;
43         private float mStart;
44         private float mSweep;
45         private int mBigIndex;
46 
47         private static final float SWEEP_INC = 2;
48         private static final float START_INC = 15;
49 
SampleView(Context context)50         public SampleView(Context context) {
51             super(context);
52 
53             mPaints = new Paint[4];
54             mUseCenters = new boolean[4];
55             mOvals = new RectF[4];
56 
57             mPaints[0] = new Paint();
58             mPaints[0].setAntiAlias(true);
59             mPaints[0].setStyle(Paint.Style.FILL);
60             mPaints[0].setColor(0x88FF0000);
61             mUseCenters[0] = false;
62 
63             mPaints[1] = new Paint(mPaints[0]);
64             mPaints[1].setColor(0x8800FF00);
65             mUseCenters[1] = true;
66 
67             mPaints[2] = new Paint(mPaints[0]);
68             mPaints[2].setStyle(Paint.Style.STROKE);
69             mPaints[2].setStrokeWidth(4);
70             mPaints[2].setColor(0x880000FF);
71             mUseCenters[2] = false;
72 
73             mPaints[3] = new Paint(mPaints[2]);
74             mPaints[3].setColor(0x88888888);
75             mUseCenters[3] = true;
76 
77             mBigOval = new RectF(40, 10, 280, 250);
78 
79             mOvals[0] = new RectF( 10, 270,  70, 330);
80             mOvals[1] = new RectF( 90, 270, 150, 330);
81             mOvals[2] = new RectF(170, 270, 230, 330);
82             mOvals[3] = new RectF(250, 270, 310, 330);
83 
84             mFramePaint = new Paint();
85             mFramePaint.setAntiAlias(true);
86             mFramePaint.setStyle(Paint.Style.STROKE);
87             mFramePaint.setStrokeWidth(0);
88         }
89 
drawArcs(Canvas canvas, RectF oval, boolean useCenter, Paint paint)90         private void drawArcs(Canvas canvas, RectF oval, boolean useCenter,
91                               Paint paint) {
92             canvas.drawRect(oval, mFramePaint);
93             canvas.drawArc(oval, mStart, mSweep, useCenter, paint);
94         }
95 
onDraw(Canvas canvas)96         @Override protected void onDraw(Canvas canvas) {
97             canvas.drawColor(Color.WHITE);
98 
99             drawArcs(canvas, mBigOval, mUseCenters[mBigIndex],
100                      mPaints[mBigIndex]);
101 
102             for (int i = 0; i < 4; i++) {
103                 drawArcs(canvas, mOvals[i], mUseCenters[i], mPaints[i]);
104             }
105 
106             mSweep += SWEEP_INC;
107             if (mSweep > 360) {
108                 mSweep -= 360;
109                 mStart += START_INC;
110                 if (mStart >= 360) {
111                     mStart -= 360;
112                 }
113                 mBigIndex = (mBigIndex + 1) % mOvals.length;
114             }
115             invalidate();
116         }
117     }
118 }
119 
120