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