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.Paint;
23 import android.os.Bundle;
24 import android.util.Log;
25 import android.view.Gravity;
26 import android.view.View;
27 import android.view.animation.AlphaAnimation;
28 import android.view.animation.Animation;
29 import android.widget.FrameLayout;
30 
31 @SuppressWarnings({"UnusedDeclaration"})
32 public class AlphaLayersActivity extends Activity {
33     private static final String LOG_TAG = "HwUi";
34 
35     @Override
onCreate(Bundle savedInstanceState)36     protected void onCreate(Bundle savedInstanceState) {
37         super.onCreate(savedInstanceState);
38 
39         DirtyBitmapView container = new DirtyBitmapView(this);
40 
41         ColorView color = new ColorView(this);
42         container.addView(color, new DirtyBitmapView.LayoutParams(
43                 dipToPx(this, 100), dipToPx(this, 100), Gravity.CENTER));
44 
45         AlphaAnimation a = new AlphaAnimation(1.0f, 0.0f);
46         a.setDuration(2000);
47         a.setRepeatCount(Animation.INFINITE);
48         a.setRepeatMode(Animation.REVERSE);
49         color.startAnimation(a);
50 
51         setContentView(container);
52     }
53 
54     @SuppressWarnings({"UnusedDeclaration"})
dipToPx(Context c, int dip)55     static int dipToPx(Context c, int dip) {
56         return (int) (c.getResources().getDisplayMetrics().density * dip + 0.5f);
57     }
58 
59     static class ColorView extends View {
ColorView(Context c)60         ColorView(Context c) {
61             super(c);
62         }
63 
64         @Override
onDraw(Canvas canvas)65         protected void onDraw(Canvas canvas) {
66             super.onDraw(canvas);
67             canvas.drawRGB(0, 255, 0);
68         }
69     }
70 
71     static class DirtyBitmapView extends FrameLayout {
72         private final Paint mPaint;
73 
DirtyBitmapView(Context c)74         DirtyBitmapView(Context c) {
75             super(c);
76             mPaint = new Paint();
77         }
78 
79         @Override
dispatchDraw(Canvas canvas)80         public void dispatchDraw(Canvas canvas) {
81             canvas.drawRGB(255, 255, 255);
82 
83             mPaint.setColor(0xffff0000);
84             canvas.drawRect(200.0f, 0.0f, 220.0f, 20.0f, mPaint);
85 
86             canvas.save();
87             canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
88             Log.d(LOG_TAG, "clipRect = " + canvas.getClipBounds());
89             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(100.0f, 100.0f, 110.0f, 110.0f));
90             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f));
91             canvas.restore();
92 
93             canvas.save();
94             canvas.scale(2.0f, 2.0f);
95             canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
96             Log.d(LOG_TAG, "clipRect = " + canvas.getClipBounds());
97             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(50.0f, 50.0f, 60.0f, 60.0f));
98             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f));
99             canvas.restore();
100 
101             canvas.save();
102             canvas.translate(20.0f, 20.0f);
103             canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
104             Log.d(LOG_TAG, "clipRect = " + canvas.getClipBounds());
105             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(80.0f, 80.0f, 90.0f, 90.0f));
106             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f));
107             canvas.restore();
108 
109             canvas.save();
110             canvas.scale(2.0f, 2.0f);
111             canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
112 
113             mPaint.setColor(0xff00ff00);
114             canvas.drawRect(0.0f, 0.0f, 20.0f, 20.0f, mPaint);
115 
116             mPaint.setColor(0xff0000ff);
117             canvas.drawRect(20.0f, 0.0f, 40.0f, 20.0f, mPaint);
118 
119             canvas.restore();
120 
121             final int restoreTo = canvas.save();
122             canvas.saveLayerAlpha(0.0f, 100.0f, getWidth(), 150.0f, 127,
123                     Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
124             mPaint.setColor(0xff0000ff);
125             canvas.drawRect(0.0f, 100.0f, 40.0f, 150.0f, mPaint);
126             mPaint.setColor(0xff00ffff);
127             canvas.drawRect(40.0f, 100.0f, 140.0f, 150.0f, mPaint);
128             mPaint.setColor(0xffff00ff);
129             canvas.drawRect(140.0f, 100.0f, 240.0f, 150.0f, mPaint);
130             canvas.restoreToCount(restoreTo);
131 
132             super.dispatchDraw(canvas);
133         }
134     }
135 }
136