FileDocCategorySizeDatePackage
DrawPoints.javaAPI DocGoogle Android v1.5 Example2598Sun Nov 11 13:01:04 GMT 2007com.google.android.samples.graphics

DrawPoints.java

/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.samples.graphics;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.View;

import java.util.Random;

public class DrawPoints extends Activity {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(new SampleView(this));
    }
    
    private static class SampleView extends View {
        private Paint   mPaint = new Paint();
        private float[] mPts = new float[1000];

        public SampleView(Context context) {
            super(context);
            
            Random rand = new Random();
            final int N = mPts.length >> 1;
            for (int i = 0; i < N; i++) {
                mPts[(i << 1) + 0] = rand.nextFloat() * 320;    // X
                mPts[(i << 1) + 1] = rand.nextFloat() * 240;    // Y
            }
        }
        
        @Override protected void onDraw(Canvas canvas) {
            Paint paint = mPaint;

            canvas.drawColor(Color.WHITE);
            
            paint.setAntiAlias(true);

            // ROUND cap + width > 0 ... Squares (pretty slow)
            paint.setColor(Color.BLUE);
            paint.setStrokeCap(Paint.Cap.SQUARE);
            paint.setStrokeWidth(10);
            canvas.drawPoints(mPts, paint);

            // SQUARE cap + width > 0 ... Circles (very slow)
            paint.setColor(Color.GREEN);
            paint.setStrokeWidth(6);
            paint.setStrokeCap(Paint.Cap.ROUND);
            canvas.drawPoints(mPts, paint);

            // antialias + width == 0 ... blurry pixels (pretty fast)
            paint.setColor(Color.RED);
            paint.setStrokeWidth(0);
            canvas.drawPoints(mPts, paint);

            // no-antialias + width == 0 ... single pixels (very fast)
            paint.setColor(Color.BLACK);
            paint.setAntiAlias(false);
            canvas.drawPoints(mPts, paint);
        }
    }
}