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.graphics.drawable.*; 22 import android.os.Bundle; 23 import android.view.*; 24 25 public class RoundRects 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 Path mPath; 35 private Paint mPaint; 36 private Rect mRect; 37 private GradientDrawable mDrawable; 38 39 public SampleView(Context context) { 40 super(context); 41 setFocusable(true); 42 43 mPath = new Path(); 44 mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); 45 mRect = new Rect(0, 0, 120, 120); 46 47 mDrawable = new GradientDrawable(GradientDrawable.Orientation.TL_BR, 48 new int[] { 0xFFFF0000, 0xFF00FF00, 49 0xFF0000FF }); 50 mDrawable.setShape(GradientDrawable.RECTANGLE); 51 mDrawable.setGradientRadius((float)(Math.sqrt(2) * 60)); 52 } 53 54 static void setCornerRadii(GradientDrawable drawable, float r0, 55 float r1, float r2, float r3) { 56 drawable.setCornerRadii(new float[] { r0, r0, r1, r1, 57 r2, r2, r3, r3 }); 58 } 59 60 @Override protected void onDraw(Canvas canvas) { 61 62 mDrawable.setBounds(mRect); 63 64 float r = 16; 65 66 canvas.save(); 67 canvas.translate(10, 10); 68 mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); 69 setCornerRadii(mDrawable, r, r, 0, 0); 70 mDrawable.draw(canvas); 71 canvas.restore(); 72 73 canvas.save(); 74 canvas.translate(10 + mRect.width() + 10, 10); 75 mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT); 76 setCornerRadii(mDrawable, 0, 0, r, r); 77 mDrawable.draw(canvas); 78 canvas.restore(); 79 80 canvas.translate(0, mRect.height() + 10); 81 82 canvas.save(); 83 canvas.translate(10, 10); 84 mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT); 85 setCornerRadii(mDrawable, 0, r, r, 0); 86 mDrawable.draw(canvas); 87 canvas.restore(); 88 89 canvas.save(); 90 canvas.translate(10 + mRect.width() + 10, 10); 91 mDrawable.setGradientType(GradientDrawable.LINEAR_GRADIENT); 92 setCornerRadii(mDrawable, r, 0, 0, r); 93 mDrawable.draw(canvas); 94 canvas.restore(); 95 96 canvas.translate(0, mRect.height() + 10); 97 98 canvas.save(); 99 canvas.translate(10, 10); 100 mDrawable.setGradientType(GradientDrawable.RADIAL_GRADIENT); 101 setCornerRadii(mDrawable, r, 0, r, 0); 102 mDrawable.draw(canvas); 103 canvas.restore(); 104 105 canvas.save(); 106 canvas.translate(10 + mRect.width() + 10, 10); 107 mDrawable.setGradientType(GradientDrawable.SWEEP_GRADIENT); 108 setCornerRadii(mDrawable, 0, r, 0, r); 109 mDrawable.draw(canvas); 110 canvas.restore(); 111 } 112 } 113 } 114