• 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 android.content.Context;
20 import android.graphics.*;
21 import android.graphics.drawable.Drawable;
22 import android.graphics.drawable.ShapeDrawable;
23 import android.graphics.drawable.shapes.*;
24 import android.os.Bundle;
25 import android.view.*;
26 
27 public class ShapeDrawable1 extends GraphicsActivity {
28 
29     @Override
onCreate(Bundle savedInstanceState)30     protected void onCreate(Bundle savedInstanceState) {
31         super.onCreate(savedInstanceState);
32         setContentView(new SampleView(this));
33     }
34 
35     private static class SampleView extends View {
36         private ShapeDrawable[] mDrawables;
37 
makeSweep()38         private static Shader makeSweep() {
39             return new SweepGradient(150, 25,
40                 new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0xFFFF0000 },
41                 null);
42         }
43 
makeLinear()44         private static Shader makeLinear() {
45             return new LinearGradient(0, 0, 50, 50,
46                               new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF },
47                               null, Shader.TileMode.MIRROR);
48         }
49 
makeTiling()50         private static Shader makeTiling() {
51             int[] pixels = new int[] { 0xFFFF0000, 0xFF00FF00, 0xFF0000FF, 0};
52             Bitmap bm = Bitmap.createBitmap(pixels, 2, 2,
53                                             Bitmap.Config.ARGB_8888);
54 
55             return new BitmapShader(bm, Shader.TileMode.REPEAT,
56                                         Shader.TileMode.REPEAT);
57         }
58 
59         private static class MyShapeDrawable extends ShapeDrawable {
60             private Paint mStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
61 
MyShapeDrawable(Shape s)62             public MyShapeDrawable(Shape s) {
63                 super(s);
64                 mStrokePaint.setStyle(Paint.Style.STROKE);
65             }
66 
getStrokePaint()67             public Paint getStrokePaint() {
68                 return mStrokePaint;
69             }
70 
onDraw(Shape s, Canvas c, Paint p)71             @Override protected void onDraw(Shape s, Canvas c, Paint p) {
72                 s.draw(c, p);
73                 s.draw(c, mStrokePaint);
74             }
75         }
76 
SampleView(Context context)77         public SampleView(Context context) {
78             super(context);
79             setFocusable(true);
80 
81             float[] outerR = new float[] { 12, 12, 12, 12, 0, 0, 0, 0 };
82             RectF   inset = new RectF(6, 6, 6, 6);
83             float[] innerR = new float[] { 12, 12, 0, 0, 12, 12, 0, 0 };
84 
85             Path path = new Path();
86             path.moveTo(50, 0);
87             path.lineTo(0, 50);
88             path.lineTo(50, 100);
89             path.lineTo(100, 50);
90             path.close();
91 
92             mDrawables = new ShapeDrawable[7];
93             mDrawables[0] = new ShapeDrawable(new RectShape());
94             mDrawables[1] = new ShapeDrawable(new OvalShape());
95             mDrawables[2] = new ShapeDrawable(new RoundRectShape(outerR, null,
96                                                                  null));
97             mDrawables[3] = new ShapeDrawable(new RoundRectShape(outerR, inset,
98                                                                  null));
99             mDrawables[4] = new ShapeDrawable(new RoundRectShape(outerR, inset,
100                                                                  innerR));
101             mDrawables[5] = new ShapeDrawable(new PathShape(path, 100, 100));
102             mDrawables[6] = new MyShapeDrawable(new ArcShape(45, -270));
103 
104             mDrawables[0].getPaint().setColor(0xFFFF0000);
105             mDrawables[1].getPaint().setColor(0xFF00FF00);
106             mDrawables[2].getPaint().setColor(0xFF0000FF);
107             mDrawables[3].getPaint().setShader(makeSweep());
108             mDrawables[4].getPaint().setShader(makeLinear());
109             mDrawables[5].getPaint().setShader(makeTiling());
110             mDrawables[6].getPaint().setColor(0x88FF8844);
111 
112             PathEffect pe = new DiscretePathEffect(10, 4);
113             PathEffect pe2 = new CornerPathEffect(4);
114             mDrawables[3].getPaint().setPathEffect(
115                                                 new ComposePathEffect(pe2, pe));
116 
117             MyShapeDrawable msd = (MyShapeDrawable)mDrawables[6];
118             msd.getStrokePaint().setStrokeWidth(4);
119         }
120 
onDraw(Canvas canvas)121         @Override protected void onDraw(Canvas canvas) {
122 
123             int x = 10;
124             int y = 10;
125             int width = 300;
126             int height = 50;
127 
128             for (Drawable dr : mDrawables) {
129                 dr.setBounds(x, y, x + width, y + height);
130                 dr.draw(canvas);
131                 y += height + 5;
132             }
133         }
134     }
135 }
136 
137