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