1 /*
2  * Copyright (C) 2011 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.animation.ObjectAnimator;
20 import android.app.Activity;
21 import android.content.Context;
22 import android.graphics.Canvas;
23 import android.graphics.Color;
24 import android.graphics.Paint;
25 import android.graphics.drawable.ColorDrawable;
26 import android.os.Bundle;
27 import android.view.View;
28 import android.widget.FrameLayout;
29 import android.widget.LinearLayout;
30 import android.widget.ProgressBar;
31 import android.widget.SeekBar;
32 
33 @SuppressWarnings({"UnusedDeclaration"})
34 public class PointsActivity extends Activity {
35 
36     float mSeekValue = .5f;
37 
38     @Override
onCreate(Bundle savedInstanceState)39     protected void onCreate(Bundle savedInstanceState) {
40         super.onCreate(savedInstanceState);
41         getWindow().setBackgroundDrawable(new ColorDrawable(0xff000000));
42         SeekBar slider = new SeekBar(this);
43         LinearLayout container = new LinearLayout(this);
44         container.setOrientation(LinearLayout.VERTICAL);
45         setContentView(container);
46 
47         container.addView(slider);
48         slider.setMax(100);
49         slider.setProgress(50);
50         FrameLayout frame = new FrameLayout(this);
51         final RenderingView gpuView = new RenderingView(this, Color.GREEN);
52         frame.addView(gpuView);
53         final RenderingView swView = new RenderingView(this, Color.RED);
54         swView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
55         frame.addView(swView);
56         container.addView(frame);
57 
58         slider.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
59             @Override
60             public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
61                 mSeekValue = (float)progress / 100.0f;
62                 float gpuAlpha = Math.min(2.0f * mSeekValue, 1f);
63                 gpuView.setAlpha(gpuAlpha);
64                 float swAlpha = Math.min((1 - mSeekValue) * 2.0f, 1f);
65                 System.out.println("(gpuAlpha, swAlpha = " + gpuAlpha + ", " + swAlpha);
66                 swView.setAlpha(swAlpha);
67             }
68 
69             @Override
70             public void onStartTrackingTouch(SeekBar seekBar) {
71             }
72 
73             @Override
74             public void onStopTrackingTouch(SeekBar seekBar) {
75             }
76         });
77     }
78 
79     @Override
onDestroy()80     protected void onDestroy() {
81         super.onDestroy();
82     }
83 
84     public static class RenderingView extends View {
85 
86         private int mColor;
87 
RenderingView(Context c, int color)88         public RenderingView(Context c, int color) {
89             super(c);
90             mColor = color;
91         }
92 
drawPoints(Canvas canvas, Paint p, float xOffset, float yOffset)93         private void drawPoints(Canvas canvas, Paint p, float xOffset, float yOffset) {
94         }
95 
96         @Override
onDraw(Canvas canvas)97         protected void onDraw(Canvas canvas) {
98             super.onDraw(canvas);
99             Paint p = new Paint();
100             p.setColor(mColor);
101 
102             float yOffset = 0;
103             for (int i = 0; i < 2; ++i) {
104                 float xOffset = 0;
105 
106                 p.setStrokeWidth(0f);
107                 p.setStrokeCap(Paint.Cap.SQUARE);
108                 canvas.drawPoint(100 + xOffset, 100 + yOffset, p);
109                 xOffset += 5;
110 
111                 p.setStrokeWidth(1f);
112                 canvas.drawPoint(100 + xOffset, 100 + yOffset, p);
113                 xOffset += 15;
114 
115                 p.setStrokeWidth(20);
116                 canvas.drawPoint(100 + xOffset, 100 + yOffset, p);
117                 xOffset += 30;
118 
119                 p.setStrokeCap(Paint.Cap.ROUND);
120                 canvas.drawPoint(100 + xOffset, 100 + yOffset, p);
121 
122                 p.setAntiAlias(true);
123                 yOffset += 30;
124             }
125 
126         }
127     }
128 }
129