DrawPoints.java revision 5c11852110eeb03dc5a69111354b383f98d15336
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
17package com.example.android.apis.graphics;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.*;
22import android.os.Bundle;
23import android.view.View;
24
25public class DrawPoints extends GraphicsActivity {
26
27    @Override
28    protected void onCreate(Bundle savedInstanceState) {
29        super.onCreate(savedInstanceState);
30        setContentView(new SampleView(this));
31    }
32
33    private static class SampleView extends View {
34        private Paint   mPaint = new Paint();
35        private float[] mPts;
36
37        private static final float SIZE = 300;
38        private static final int SEGS = 32;
39        private static final int X = 0;
40        private static final int Y = 1;
41
42        private void buildPoints() {
43            final int ptCount = (SEGS + 1) * 2;
44            mPts = new float[ptCount * 2];
45
46            float value = 0;
47            final float delta = SIZE / SEGS;
48            for (int i = 0; i <= SEGS; i++) {
49                mPts[i*4 + X] = SIZE - value;
50                mPts[i*4 + Y] = 0;
51                mPts[i*4 + X + 2] = 0;
52                mPts[i*4 + Y + 2] = value;
53                value += delta;
54            }
55        }
56
57        public SampleView(Context context) {
58            super(context);
59
60            buildPoints();
61        }
62
63        @Override protected void onDraw(Canvas canvas) {
64            Paint paint = mPaint;
65
66            canvas.translate(10, 10);
67
68            canvas.drawColor(Color.WHITE);
69
70            paint.setColor(Color.RED);
71            paint.setStrokeWidth(0);
72            canvas.drawLines(mPts, paint);
73
74            paint.setColor(Color.BLUE);
75            paint.setStrokeWidth(3);
76            canvas.drawPoints(mPts, paint);
77        }
78    }
79}
80
81