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.KeyEvent;
     23 import android.view.View;
     24 
     25 public class Sweep 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(Paint.ANTI_ALIAS_FLAG);
     35         private float mRotate;
     36         private Matrix mMatrix = new Matrix();
     37         private Shader mShader;
     38         private boolean mDoTiming;
     39 
     40         public SampleView(Context context) {
     41             super(context);
     42             setFocusable(true);
     43             setFocusableInTouchMode(true);
     44 
     45             float x = 160;
     46             float y = 100;
     47             mShader = new SweepGradient(x, y, new int[] { Color.GREEN,
     48                                                   Color.RED,
     49                                                   Color.BLUE,
     50                                                   Color.GREEN }, null);
     51             mPaint.setShader(mShader);
     52         }
     53 
     54         @Override protected void onDraw(Canvas canvas) {
     55             Paint paint = mPaint;
     56             float x = 160;
     57             float y = 100;
     58 
     59             canvas.drawColor(Color.WHITE);
     60 
     61             mMatrix.setRotate(mRotate, x, y);
     62             mShader.setLocalMatrix(mMatrix);
     63             mRotate += 3;
     64             if (mRotate >= 360) {
     65                 mRotate = 0;
     66             }
     67             invalidate();
     68 
     69             if (mDoTiming) {
     70                 long now = System.currentTimeMillis();
     71                 for (int i = 0; i < 20; i++) {
     72                     canvas.drawCircle(x, y, 80, paint);
     73                 }
     74                 now = System.currentTimeMillis() - now;
     75                 android.util.Log.d("skia", "sweep ms = " + (now/20.));
     76             }
     77             else {
     78                 canvas.drawCircle(x, y, 80, paint);
     79             }
     80         }
     81 
     82         @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
     83             switch (keyCode) {
     84                 case KeyEvent.KEYCODE_D:
     85                     mPaint.setDither(!mPaint.isDither());
     86                     invalidate();
     87                     return true;
     88                 case KeyEvent.KEYCODE_T:
     89                     mDoTiming = !mDoTiming;
     90                     invalidate();
     91                     return true;
     92             }
     93             return super.onKeyDown(keyCode, event);
     94         }
     95     }
     96 }
     97 
     98