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