• 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.app.Activity;
20 import android.content.Context;
21 import android.graphics.*;
22 import android.os.Bundle;
23 import android.view.KeyEvent;
24 import android.view.View;
25 
26 public class PathEffects extends GraphicsActivity {
27 
28     @Override
onCreate(Bundle savedInstanceState)29     protected void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(new SampleView(this));
32     }
33 
34     private static class SampleView extends View {
35         private Paint mPaint;
36         private Path mPath;
37         private PathEffect[] mEffects;
38         private int[] mColors;
39         private float mPhase;
40 
makeDash(float phase)41         private static PathEffect makeDash(float phase) {
42             return new DashPathEffect(new float[] { 15, 5, 8, 5 }, phase);
43         }
44 
makeEffects(PathEffect[] e, float phase)45         private static void makeEffects(PathEffect[] e, float phase) {
46             e[0] = null;     // no effect
47             e[1] = new CornerPathEffect(10);
48             e[2] = new DashPathEffect(new float[] {10, 5, 5, 5}, phase);
49             e[3] = new PathDashPathEffect(makePathDash(), 12, phase,
50                                           PathDashPathEffect.Style.ROTATE);
51             e[4] = new ComposePathEffect(e[2], e[1]);
52             e[5] = new ComposePathEffect(e[3], e[1]);
53         }
54 
SampleView(Context context)55         public SampleView(Context context) {
56             super(context);
57             setFocusable(true);
58             setFocusableInTouchMode(true);
59 
60             mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
61             mPaint.setStyle(Paint.Style.STROKE);
62             mPaint.setStrokeWidth(6);
63 
64             mPath = makeFollowPath();
65 
66             mEffects = new PathEffect[6];
67 
68             mColors = new int[] { Color.BLACK, Color.RED, Color.BLUE,
69                                   Color.GREEN, Color.MAGENTA, Color.BLACK
70                                 };
71         }
72 
onDraw(Canvas canvas)73         @Override protected void onDraw(Canvas canvas) {
74             canvas.drawColor(Color.WHITE);
75 
76             RectF bounds = new RectF();
77             mPath.computeBounds(bounds, false);
78             canvas.translate(10 - bounds.left, 10 - bounds.top);
79 
80             makeEffects(mEffects, mPhase);
81             mPhase += 1;
82             invalidate();
83 
84             for (int i = 0; i < mEffects.length; i++) {
85                 mPaint.setPathEffect(mEffects[i]);
86                 mPaint.setColor(mColors[i]);
87                 canvas.drawPath(mPath, mPaint);
88                 canvas.translate(0, 28);
89             }
90         }
91 
onKeyDown(int keyCode, KeyEvent event)92         @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
93             switch (keyCode) {
94                 case KeyEvent.KEYCODE_DPAD_CENTER:
95                     mPath = makeFollowPath();
96                     return true;
97             }
98             return super.onKeyDown(keyCode, event);
99         }
100 
makeFollowPath()101         private static Path makeFollowPath() {
102             Path p = new Path();
103             p.moveTo(0, 0);
104             for (int i = 1; i <= 15; i++) {
105                 p.lineTo(i*20, (float)Math.random() * 35);
106             }
107             return p;
108         }
109 
makePathDash()110         private static Path makePathDash() {
111             Path p = new Path();
112             p.moveTo(4, 0);
113             p.lineTo(0, -4);
114             p.lineTo(8, -4);
115             p.lineTo(12, 0);
116             p.lineTo(8, 4);
117             p.lineTo(0, 4);
118             return p;
119         }
120     }
121 }
122 
123