blob: 5ddcd200b4789216c7f14898087f2b1fb6d13338 [file] [log] [blame]
Romain Guy7fbcc042010-08-04 15:40:07 -07001/*
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
Romain Guyf607bdc2010-09-10 19:20:06 -070017package com.android.test.hwui;
Romain Guy7fbcc042010-08-04 15:40:07 -070018
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
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -080040 public static class PathsView extends View {
Romain Guy7fbcc042010-08-04 15:40:07 -070041 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
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -080053 public PathsView(Context c) {
Romain Guy7fbcc042010-08-04 15:40:07 -070054 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);
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -080063
Romain Guy7fbcc042010-08-04 15:40:07 -070064 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);
Romain Guy6c319ca2011-01-11 14:29:25 -080072 mMediumPaint.setColor(0xe00000ff);
Romain Guy7fbcc042010-08-04 15:40:07 -070073 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);
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -0800115
Romain Guy7fbcc042010-08-04 15:40:07 -0700116 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();
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -0800124
Romain Guy7fbcc042010-08-04 15:40:07 -0700125 canvas.save();
126 canvas.translate(200.0f, 360.0f);
127 canvas.drawPath(mPath, mSmallPaint);
128 canvas.drawRect(mPathBounds, mBoundsPaint);
Xavier Ducrohetd348b6e2010-12-20 08:22:47 -0800129
Romain Guy7fbcc042010-08-04 15:40:07 -0700130 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}