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