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