GradientStopsActivity.java revision 42e1e0d482d774cf18a55773e434f02edb9e4462
1/*
2 * Copyright (C) 2012 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.Canvas;
22import android.graphics.LinearGradient;
23import android.graphics.Paint;
24import android.graphics.Shader;
25import android.os.Bundle;
26import android.view.View;
27
28@SuppressWarnings("UnusedDeclaration")
29public class GradientStopsActivity extends Activity {
30    @Override
31    protected void onCreate(Bundle savedInstanceState) {
32        super.onCreate(savedInstanceState);
33
34        setContentView(new GradientView(this));
35    }
36
37    private class GradientView extends View {
38        public GradientView(Context context) {
39            super(context);
40        }
41
42        @Override
43        protected void onDraw(Canvas canvas) {
44            int[] colors = new int[] { 0xffff0000, 0xff0000ff };
45            float[] positions = new float[] { 0.3f, 0.6f };
46            LinearGradient gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
47                    colors, positions, Shader.TileMode.CLAMP);
48
49            Paint paint = new Paint();
50            paint.setShader(gradient);
51
52            canvas.drawRect(0.0f, 0.0f, 256.0f, 50.0f, paint);
53
54            colors = new int[] { 0xffff0000, 0xff0000ff, 0xff00ff00 };
55            positions = new float[] { 0.3f, 0.6f, 1.0f };
56            gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
57                    colors, positions, Shader.TileMode.CLAMP);
58
59            paint.setShader(gradient);
60
61            canvas.translate(0.0f, 75.0f);
62            canvas.drawRect(0.0f, 0.0f, 256.0f, 50.0f, paint);
63
64            colors = new int[] { 0xffff0000, 0xff0000ff, 0xff00ff00 };
65            positions = new float[] { 0.0f, 0.3f, 0.6f };
66            gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
67                    colors, positions, Shader.TileMode.CLAMP);
68
69            paint.setShader(gradient);
70
71            canvas.translate(0.0f, 75.0f);
72            canvas.drawRect(0.0f, 0.0f, 256.0f, 50.0f, paint);
73
74            colors = new int[] { 0xff000000, 0xffffffff };
75            gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
76                    colors, null, Shader.TileMode.CLAMP);
77
78            paint.setShader(gradient);
79
80            canvas.translate(0.0f, 75.0f);
81            canvas.drawRect(0.0f, 0.0f, 256.0f, 50.0f, paint);
82
83            gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
84                    colors, null, Shader.TileMode.REPEAT);
85
86            paint.setShader(gradient);
87
88            canvas.translate(0.0f, 75.0f);
89            canvas.drawRect(0.0f, 0.0f, 768.0f, 50.0f, paint);
90
91            gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
92                    colors, null, Shader.TileMode.MIRROR);
93
94            paint.setShader(gradient);
95
96            canvas.translate(0.0f, 75.0f);
97            canvas.drawRect(0.0f, 0.0f, 768.0f, 50.0f, paint);
98
99            gradient = new LinearGradient(0.0f, 0.0f, 256.0f, 0.0f,
100                    colors, null, Shader.TileMode.CLAMP);
101
102            paint.setShader(gradient);
103
104            canvas.translate(0.0f, 75.0f);
105            canvas.drawRect(0.0f, 0.0f, 768.0f, 50.0f, paint);
106
107            gradient = new LinearGradient(0.0f, 0.0f, 768.0f, 0.0f,
108                    colors, null, Shader.TileMode.CLAMP);
109
110            paint.setShader(gradient);
111
112            canvas.translate(0.0f, 75.0f);
113            canvas.drawRect(0.0f, 0.0f, 768.0f, 50.0f, paint);
114        }
115    }
116}
117