Home | History | Annotate | Download | only in hwui
      1 /*
      2  * Copyright (C) 2010 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.android.test.hwui;
     18 
     19 import android.app.Activity;
     20 import android.content.Context;
     21 import android.graphics.Canvas;
     22 import android.graphics.Paint;
     23 import android.graphics.Path;
     24 import android.os.Bundle;
     25 import android.util.Log;
     26 import android.view.View;
     27 
     28 import java.util.ArrayList;
     29 import java.util.Random;
     30 
     31 @SuppressWarnings({"UnusedDeclaration"})
     32 public class PathsCacheActivity extends Activity {
     33     private Path mPath;
     34 
     35     private final Random mRandom = new Random();
     36     @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
     37     private final ArrayList<Path> mPathList = new ArrayList<Path>();
     38 
     39     @Override
     40     protected void onCreate(Bundle savedInstanceState) {
     41         super.onCreate(savedInstanceState);
     42 
     43         mPath = makePath();
     44 
     45         final PathsView view = new PathsView(this);
     46         setContentView(view);
     47     }
     48 
     49     private static Path makePath() {
     50         Path path = new Path();
     51         buildPath(path);
     52         return path;
     53     }
     54 
     55     private static void buildPath(Path path) {
     56         path.moveTo(0.0f, 0.0f);
     57         path.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f);
     58         path.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f);
     59         path.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f);
     60     }
     61 
     62     private static Path makeLargePath() {
     63         Path path = new Path();
     64         buildLargePath(path);
     65         return path;
     66     }
     67 
     68     private static void buildLargePath(Path path) {
     69         path.moveTo(0.0f, 0.0f);
     70         path.cubicTo(0.0f, 0.0f, 10000.0f, 15000.0f, 10000.0f, 20000.0f);
     71         path.cubicTo(10000.0f, 20000.0f, 5000.0f, 30000.0f, -8000.0f, 20000.0f);
     72         path.cubicTo(-8000.0f, 20000.0f, 10000.0f, 20000.0f, 20000.0f, 0.0f);
     73     }
     74 
     75     public class PathsView extends View {
     76         private final Paint mMediumPaint;
     77 
     78         public PathsView(Context c) {
     79             super(c);
     80 
     81             mMediumPaint = new Paint();
     82             mMediumPaint.setAntiAlias(true);
     83             mMediumPaint.setColor(0xe00000ff);
     84             mMediumPaint.setStrokeWidth(10.0f);
     85             mMediumPaint.setStyle(Paint.Style.STROKE);
     86         }
     87 
     88         @Override
     89         protected void onDraw(Canvas canvas) {
     90             super.onDraw(canvas);
     91 
     92             Log.d("OpenGLRenderer", "Start frame");
     93 
     94             canvas.drawARGB(255, 255, 255, 255);
     95 
     96             canvas.save();
     97             canvas.translate(550.0f, 60.0f);
     98             canvas.drawPath(mPath, mMediumPaint);
     99 
    100             mPath.reset();
    101             buildPath(mPath);
    102 
    103             canvas.translate(30.0f, 30.0f);
    104             canvas.drawPath(mPath, mMediumPaint);
    105             canvas.drawPath(mPath, mMediumPaint);
    106 
    107             canvas.restore();
    108 
    109             for (int i = 0; i < mRandom.nextInt(20); i++) {
    110                 Path path = makePath();
    111                 int r = mRandom.nextInt(10);
    112                 if (r == 5 || r == 3) {
    113                     mPathList.add(path);
    114                 } else if (r == 7) {
    115                     path = makeLargePath();
    116                     mPathList.add(path);
    117                 }
    118 
    119                 canvas.save();
    120                 canvas.translate(450.0f + mRandom.nextInt(200), mRandom.nextInt(200));
    121                 canvas.drawPath(path, mMediumPaint);
    122                 canvas.restore();
    123             }
    124 
    125             int r = mRandom.nextInt(100);
    126             if (r == 50) {
    127                 mPathList.clear();
    128             }
    129 
    130             invalidate();
    131         }
    132     }
    133 }
    134