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 onCreate(Bundle savedInstanceState)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 SampleView(Context context)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 onDraw(Canvas canvas)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 mPaint.setShader(mShader); 64 mRotate += 3; 65 if (mRotate >= 360) { 66 mRotate = 0; 67 } 68 invalidate(); 69 70 if (mDoTiming) { 71 long now = System.currentTimeMillis(); 72 for (int i = 0; i < 20; i++) { 73 canvas.drawCircle(x, y, 80, paint); 74 } 75 now = System.currentTimeMillis() - now; 76 android.util.Log.d("skia", "sweep ms = " + (now/20.)); 77 } 78 else { 79 canvas.drawCircle(x, y, 80, paint); 80 } 81 } 82 onKeyDown(int keyCode, KeyEvent event)83 @Override public boolean onKeyDown(int keyCode, KeyEvent event) { 84 switch (keyCode) { 85 case KeyEvent.KEYCODE_D: 86 mPaint.setDither(!mPaint.isDither()); 87 invalidate(); 88 return true; 89 case KeyEvent.KEYCODE_T: 90 mDoTiming = !mDoTiming; 91 invalidate(); 92 return true; 93 } 94 return super.onKeyDown(keyCode, event); 95 } 96 } 97 } 98 99