196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy/*
296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * Copyright (C) 2010 The Android Open Source Project
396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy *
496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * Licensed under the Apache License, Version 2.0 (the "License");
596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * you may not use this file except in compliance with the License.
696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * You may obtain a copy of the License at
796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy *
896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy *      http://www.apache.org/licenses/LICENSE-2.0
996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy *
1096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * Unless required by applicable law or agreed to in writing, software
1196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * distributed under the License is distributed on an "AS IS" BASIS,
1296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * See the License for the specific language governing permissions and
1496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy * limitations under the License.
1596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy */
1696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
1796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guypackage com.android.test.hwui;
1896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
1996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport android.app.Activity;
2096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport android.content.Context;
2196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport android.graphics.Canvas;
2296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport android.graphics.Paint;
2396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport android.graphics.Path;
2496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport android.os.Bundle;
2596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport android.util.Log;
2696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport android.view.View;
2796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
2896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport java.util.ArrayList;
2996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guyimport java.util.Random;
3096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
3196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy@SuppressWarnings({"UnusedDeclaration"})
3296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guypublic class PathsCacheActivity extends Activity {
3396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    private Path mPath;
3496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
3596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    private final Random mRandom = new Random();
3696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    private final ArrayList<Path> mPathList = new ArrayList<Path>();
3796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
3896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    @Override
3996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    protected void onCreate(Bundle savedInstanceState) {
4096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        super.onCreate(savedInstanceState);
4196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
4296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        mPath = makePath();
4396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
4496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        final PathsView view = new PathsView(this);
4596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        setContentView(view);
4696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    }
4796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
4896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    private Path makePath() {
4996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        Path path = new Path();
5096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        buildPath(path);
5196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        return path;
5296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    }
5396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
5496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    private void buildPath(Path path) {
5596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        path.moveTo(0.0f, 0.0f);
5696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        path.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f);
5796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        path.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f);
5896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        path.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f);
5996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    }
6096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
6196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    public class PathsView extends View {
6296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        private final Paint mMediumPaint;
6396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
6496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        public PathsView(Context c) {
6596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            super(c);
6696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
6796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            mMediumPaint = new Paint();
6896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            mMediumPaint.setAntiAlias(true);
6996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            mMediumPaint.setColor(0xe00000ff);
7096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            mMediumPaint.setStrokeWidth(10.0f);
7196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            mMediumPaint.setStyle(Paint.Style.STROKE);
7296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        }
7396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
7496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        @Override
7596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        protected void onDraw(Canvas canvas) {
7696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            super.onDraw(canvas);
7796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
7896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            Log.d("OpenGLRenderer", "Start frame");
7996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
8096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            canvas.drawARGB(255, 255, 255, 255);
8196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
8296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            canvas.save();
8396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            canvas.translate(550.0f, 60.0f);
8496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            canvas.drawPath(mPath, mMediumPaint);
8596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
8696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            mPath.reset();
8796ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            buildPath(mPath);
8896ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
8996ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            canvas.translate(30.0f, 30.0f);
9096ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            canvas.drawPath(mPath, mMediumPaint);
9196ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            canvas.drawPath(mPath, mMediumPaint);
9296ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
9396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            canvas.restore();
9496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy
954bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy            for (int i = 0; i < mRandom.nextInt(20); i++) {
964bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                Path path = makePath();
974bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                int r = mRandom.nextInt(10);
984bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                if (r == 5 || r == 3) {
994bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                    mPathList.add(path);
1004bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                }
1014bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy
1024bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                canvas.save();
1034bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                canvas.translate(450.0f + mRandom.nextInt(200), mRandom.nextInt(200));
1044bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                canvas.drawPath(path, mMediumPaint);
1054bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                canvas.restore();
1064bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy            }
1074bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy
1084bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy            int r = mRandom.nextInt(100);
1094bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy            if (r == 50) {
1104bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy                mPathList.clear();
1114bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy            }
1124bcb7467a174ed03a67b0c62950c555813ddf00dRomain Guy
11396ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy            invalidate();
11496ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy        }
11596ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy    }
11696ebc6b5097ab73eef45e094241e444f4c21bfccRomain Guy}
117