PathsActivity.java revision f607bdc167f66b3e7003acaa4736ae46d78c1492
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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.graphics.BitmapShader;
24import android.graphics.Canvas;
25import android.graphics.Paint;
26import android.graphics.Path;
27import android.graphics.RectF;
28import android.os.Bundle;
29import android.view.View;
30
31@SuppressWarnings({"UnusedDeclaration"})
32public class PathsActivity extends Activity {
33    @Override
34    protected void onCreate(Bundle savedInstanceState) {
35        super.onCreate(savedInstanceState);
36        final PathsView view = new PathsView(this);
37        setContentView(view);
38    }
39
40    static class PathsView extends View {
41        private final Bitmap mBitmap1;
42        private final Paint mSmallPaint;
43        private final Paint mMediumPaint;
44        private final Paint mLargePaint;
45        private final BitmapShader mShader;
46        private final Path mPath;
47        private final RectF mPathBounds;
48        private final Paint mBoundsPaint;
49        private final Bitmap mBitmap;
50        private final float mOffset;
51        private final Paint mLinePaint;
52
53        PathsView(Context c) {
54            super(c);
55
56            mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
57
58            mSmallPaint = new Paint();
59            mSmallPaint.setAntiAlias(true);
60            mSmallPaint.setColor(0xffff0000);
61            mSmallPaint.setStrokeWidth(1.0f);
62            mSmallPaint.setStyle(Paint.Style.STROKE);
63
64            mLinePaint = new Paint();
65            mLinePaint.setAntiAlias(true);
66            mLinePaint.setColor(0xffff00ff);
67            mLinePaint.setStrokeWidth(1.0f);
68            mLinePaint.setStyle(Paint.Style.STROKE);
69
70            mMediumPaint = new Paint();
71            mMediumPaint.setAntiAlias(true);
72            mMediumPaint.setColor(0xff0000ff);
73            mMediumPaint.setStrokeWidth(10.0f);
74            mMediumPaint.setStyle(Paint.Style.STROKE);
75
76            mLargePaint = new Paint();
77            mLargePaint.setAntiAlias(true);
78            mLargePaint.setColor(0xff00ff00);
79            mLargePaint.setStrokeWidth(15.0f);
80            mLargePaint.setStyle(Paint.Style.FILL);
81
82            mShader = new BitmapShader(mBitmap1, BitmapShader.TileMode.MIRROR,
83                    BitmapShader.TileMode.MIRROR);
84
85            mPath = new Path();
86            mPath.moveTo(0.0f, 0.0f);
87            mPath.cubicTo(0.0f, 0.0f, 100.0f, 150.0f, 100.0f, 200.0f);
88            mPath.cubicTo(100.0f, 200.0f, 50.0f, 300.0f, -80.0f, 200.0f);
89            mPath.cubicTo(-80.0f, 200.0f, 100.0f, 200.0f, 200.0f, 0.0f);
90
91            mPathBounds = new RectF();
92            mPath.computeBounds(mPathBounds, true);
93
94            mBoundsPaint = new Paint();
95            mBoundsPaint.setColor(0x4000ff00);
96
97            mOffset = mMediumPaint.getStrokeWidth();
98            final int width = (int) (mPathBounds.width() + mOffset * 3.0f + 0.5f);
99            final int height = (int) (mPathBounds.height() + mOffset * 3.0f + 0.5f);
100            mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
101            Canvas canvas = new Canvas(mBitmap);
102            canvas.translate(-mPathBounds.left + mOffset * 1.5f, -mPathBounds.top + mOffset * 1.5f);
103            canvas.drawPath(mPath, mMediumPaint);
104        }
105
106        @Override
107        protected void onDraw(Canvas canvas) {
108            super.onDraw(canvas);
109
110            canvas.drawARGB(255, 255, 255, 255);
111
112            canvas.save();
113            canvas.translate(200.0f, 60.0f);
114            canvas.drawPath(mPath, mSmallPaint);
115
116            canvas.translate(350.0f, 0.0f);
117            canvas.drawPath(mPath, mMediumPaint);
118
119            mLargePaint.setShader(mShader);
120            canvas.translate(350.0f, 0.0f);
121            canvas.drawPath(mPath, mLargePaint);
122            mLargePaint.setShader(null);
123            canvas.restore();
124
125            canvas.save();
126            canvas.translate(200.0f, 360.0f);
127            canvas.drawPath(mPath, mSmallPaint);
128            canvas.drawRect(mPathBounds, mBoundsPaint);
129
130            canvas.translate(350.0f, 0.0f);
131            canvas.drawBitmap(mBitmap, mPathBounds.left - mOffset * 1.5f,
132                    mPathBounds.top - mOffset * 1.5f, null);
133            canvas.drawRect(mPathBounds, mBoundsPaint);
134            canvas.drawLine(0.0f, -360.0f, 0.0f, 500.0f, mLinePaint);
135
136            mLargePaint.setShader(mShader);
137            canvas.translate(350.0f, 0.0f);
138            canvas.drawPath(mPath, mLargePaint);
139            canvas.drawRect(mPathBounds, mBoundsPaint);
140            mLargePaint.setShader(null);
141            canvas.restore();
142        }
143    }
144}
145