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.Matrix;
23 import android.graphics.Paint;
24 import android.graphics.RadialGradient;
25 import android.graphics.Shader;
26 import android.graphics.SweepGradient;
27 import android.os.Bundle;
28 import android.view.View;
29 
30 @SuppressWarnings({"UnusedDeclaration"})
31 public class AdvancedGradientsActivity extends Activity {
32     @Override
onCreate(Bundle savedInstanceState)33     protected void onCreate(Bundle savedInstanceState) {
34         super.onCreate(savedInstanceState);
35 
36         setContentView(new GradientsView(this));
37     }
38 
39     public static class GradientsView extends View {
40         private final Paint mPaint;
41         private final SweepGradient mSweepGradient;
42         private final RadialGradient mRadialGradient;
43         private final Matrix mMatrix;
44         private final Matrix mMatrix2;
45         private final Matrix mMatrix3;
46 
GradientsView(Context c)47         public GradientsView(Context c) {
48             super(c);
49 
50             mSweepGradient = new SweepGradient(0.0f, 0.0f, 0xff000000, 0xffffffff);
51             mRadialGradient = new RadialGradient(0.0f, 0.0f, 100.0f, 0xff000000, 0xffffffff,
52                     Shader.TileMode.MIRROR);
53 
54             mMatrix = new Matrix();
55             mMatrix.setRotate(-45, 0.0f, 0.0f);
56             mMatrix.postTranslate(100.0f, 100.0f);
57 
58             mMatrix2 = new Matrix();
59             mMatrix2.setScale(1.0f, 2.0f);
60             mMatrix2.postRotate(-45, 0.0f, 0.0f);
61 
62             mMatrix3 = new Matrix();
63             mMatrix3.setTranslate(100.0f, 100.0f);
64 
65             mPaint = new Paint();
66         }
67 
68         @Override
onDraw(Canvas canvas)69         protected void onDraw(Canvas canvas) {
70             super.onDraw(canvas);
71             canvas.drawRGB(255, 255, 255);
72 
73             canvas.save();
74             canvas.translate(130.0f, 100.0f);
75 
76             mSweepGradient.setLocalMatrix(mMatrix3);
77             mPaint.setShader(mSweepGradient);
78             canvas.drawRect(0.0f, 0.0f, 200.0f, 200.0f, mPaint);
79 
80             canvas.translate(400.0f, 000.0f);
81 
82             mSweepGradient.setLocalMatrix(mMatrix);
83             mPaint.setShader(mSweepGradient);
84             canvas.drawRect(0.0f, 0.0f, 200.0f, 200.0f, mPaint);
85 
86             canvas.translate(400.0f, 000.0f);
87 
88             mSweepGradient.setLocalMatrix(mMatrix2);
89             mPaint.setShader(mSweepGradient);
90             canvas.drawRect(0.0f, 0.0f, 200.0f, 200.0f, mPaint);
91 
92             canvas.translate(-800.0f, 300.0f);
93 
94             mRadialGradient.setLocalMatrix(null);
95             mPaint.setShader(mRadialGradient);
96             canvas.drawRect(0.0f, 0.0f, 200.0f, 200.0f, mPaint);
97 
98             canvas.translate(400.0f, 000.0f);
99 
100             mRadialGradient.setLocalMatrix(mMatrix);
101             mPaint.setShader(mRadialGradient);
102             canvas.drawRect(0.0f, 0.0f, 200.0f, 200.0f, mPaint);
103 
104             canvas.translate(400.0f, 000.0f);
105 
106             mRadialGradient.setLocalMatrix(mMatrix2);
107             mPaint.setShader(mRadialGradient);
108             canvas.drawRect(0.0f, 0.0f, 200.0f, 200.0f, mPaint);
109 
110 
111             canvas.restore();
112         }
113     }
114 }
115