• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.Bitmap;
22 import android.graphics.BitmapFactory;
23 import android.graphics.BitmapShader;
24 import android.graphics.Canvas;
25 import android.graphics.Paint;
26 import android.graphics.Path;
27 import android.graphics.RectF;
28 import android.os.Bundle;
29 import android.view.View;
30 
31 @SuppressWarnings({"UnusedDeclaration"})
32 public class PathsActivity extends Activity {
33     @Override
onCreate(Bundle savedInstanceState)34     protected void onCreate(Bundle savedInstanceState) {
35         super.onCreate(savedInstanceState);
36         final PathsView view = new PathsView(this);
37         setContentView(view);
38     }
39 
40     public 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 
PathsView(Context c)53         public 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(0xe00000ff);
73             mMediumPaint.setStrokeWidth(10.0f);
74             mMediumPaint.setStyle(Paint.Style.STROKE);
75 
76             mLargePaint = new Paint();
77             mLargePaint.setAntiAlias(true);
78             mLargePaint.setColor(0x7f00ff00);
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             canvas.setBitmap(null);
105         }
106 
107         @Override
onDraw(Canvas canvas)108         protected void onDraw(Canvas canvas) {
109             super.onDraw(canvas);
110 
111             canvas.drawARGB(255, 255, 255, 255);
112 
113             canvas.save();
114             canvas.translate(200.0f, 60.0f);
115             canvas.drawPath(mPath, mSmallPaint);
116 
117             canvas.translate(350.0f, 0.0f);
118             canvas.drawPath(mPath, mMediumPaint);
119 
120             mLargePaint.setShader(mShader);
121             canvas.translate(350.0f, 0.0f);
122             canvas.drawPath(mPath, mLargePaint);
123             mLargePaint.setShader(null);
124             canvas.restore();
125 
126             canvas.save();
127             canvas.translate(200.0f, 360.0f);
128             canvas.drawPath(mPath, mSmallPaint);
129             canvas.drawRect(mPathBounds, mBoundsPaint);
130 
131             canvas.translate(350.0f, 0.0f);
132             canvas.drawBitmap(mBitmap, mPathBounds.left - mOffset * 1.5f,
133                     mPathBounds.top - mOffset * 1.5f, null);
134             canvas.drawRect(mPathBounds, mBoundsPaint);
135             canvas.drawLine(0.0f, -360.0f, 0.0f, 500.0f, mLinePaint);
136 
137             mLargePaint.setShader(mShader);
138             canvas.translate(350.0f, 0.0f);
139             canvas.drawPath(mPath, mLargePaint);
140             canvas.drawRect(mPathBounds, mBoundsPaint);
141             mLargePaint.setShader(null);
142             canvas.restore();
143         }
144     }
145 }
146