10bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy/*
20bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * Copyright (C) 2010 The Android Open Source Project
30bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy *
40bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * Licensed under the Apache License, Version 2.0 (the "License");
50bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * you may not use this file except in compliance with the License.
60bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * You may obtain a copy of the License at
70bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy *
80bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy *      http://www.apache.org/licenses/LICENSE-2.0
90bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy *
100bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * Unless required by applicable law or agreed to in writing, software
110bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * distributed under the License is distributed on an "AS IS" BASIS,
120bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * See the License for the specific language governing permissions and
140bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy * limitations under the License.
150bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy */
160bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
17f607bdc167f66b3e7003acaa4736ae46d78c1492Romain Guypackage com.android.test.hwui;
180bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
190bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.app.Activity;
200bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.content.Context;
210bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.graphics.Canvas;
220bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.graphics.Paint;
230bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.os.Bundle;
249d5316e3f56d138504565ff311145ac01621dff4Romain Guyimport android.util.Log;
25bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyimport android.view.Gravity;
260bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.view.View;
27bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyimport android.view.animation.AlphaAnimation;
28bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyimport android.view.animation.Animation;
29bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guyimport android.widget.FrameLayout;
300bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
319d5316e3f56d138504565ff311145ac01621dff4Romain Guy@SuppressWarnings({"UnusedDeclaration"})
32d55a86120dd1e8ebcc6906c9ffd463f7460348daRomain Guypublic class AlphaLayersActivity extends Activity {
330bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    private static final String LOG_TAG = "HwUi";
340bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
350bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    @Override
360bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    protected void onCreate(Bundle savedInstanceState) {
370bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        super.onCreate(savedInstanceState);
380bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
39bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        DirtyBitmapView container = new DirtyBitmapView(this);
40bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
41bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        ColorView color = new ColorView(this);
42bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        container.addView(color, new DirtyBitmapView.LayoutParams(
43bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy                dipToPx(this, 100), dipToPx(this, 100), Gravity.CENTER));
44bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
45bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        AlphaAnimation a = new AlphaAnimation(1.0f, 0.0f);
46bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        a.setDuration(2000);
47bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        a.setRepeatCount(Animation.INFINITE);
48bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        a.setRepeatMode(Animation.REVERSE);
49bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        color.startAnimation(a);
50bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
51bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        setContentView(container);
520bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
530bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
5485bf02fc16784d935fb9eebfa9cb20fe46ff7951Romain Guy    @SuppressWarnings({"UnusedDeclaration"})
550bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    static int dipToPx(Context c, int dip) {
560bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        return (int) (c.getResources().getDisplayMetrics().density * dip + 0.5f);
570bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
580bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
59bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    static class ColorView extends View {
60bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        ColorView(Context c) {
61bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            super(c);
62bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        }
63bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
64bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        @Override
65bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        protected void onDraw(Canvas canvas) {
66bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            super.onDraw(canvas);
67bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            canvas.drawRGB(0, 255, 0);
68bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        }
69bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    }
70bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
71bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy    static class DirtyBitmapView extends FrameLayout {
729d5316e3f56d138504565ff311145ac01621dff4Romain Guy        private final Paint mPaint;
739d5316e3f56d138504565ff311145ac01621dff4Romain Guy
740bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        DirtyBitmapView(Context c) {
750bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            super(c);
769d5316e3f56d138504565ff311145ac01621dff4Romain Guy            mPaint = new Paint();
770bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        }
780bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
790bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        @Override
80bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy        public void dispatchDraw(Canvas canvas) {
819d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.drawRGB(255, 255, 255);
829d5316e3f56d138504565ff311145ac01621dff4Romain Guy
839d5316e3f56d138504565ff311145ac01621dff4Romain Guy            mPaint.setColor(0xffff0000);
849d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.drawRect(200.0f, 0.0f, 220.0f, 20.0f, mPaint);
859d5316e3f56d138504565ff311145ac01621dff4Romain Guy
869d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.save();
879d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
889d5316e3f56d138504565ff311145ac01621dff4Romain Guy            Log.d(LOG_TAG, "clipRect = " + canvas.getClipBounds());
89c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy            Log.d(LOG_TAG, "rejected = " + canvas.quickReject(100.0f, 100.0f, 110.0f, 110.0f,
90c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy                    Canvas.EdgeType.BW));
91c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy            Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f,
92c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy                    Canvas.EdgeType.BW));
939d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.restore();
949d5316e3f56d138504565ff311145ac01621dff4Romain Guy
959d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.save();
969d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.scale(2.0f, 2.0f);
979d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
989d5316e3f56d138504565ff311145ac01621dff4Romain Guy            Log.d(LOG_TAG, "clipRect = " + canvas.getClipBounds());
99c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy            Log.d(LOG_TAG, "rejected = " + canvas.quickReject(50.0f, 50.0f, 60.0f, 60.0f,
100c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy                    Canvas.EdgeType.BW));
101c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy            Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f,
102c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy                    Canvas.EdgeType.BW));
1039d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.restore();
1049d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1059d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.save();
1069d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.translate(20.0f, 20.0f);
1079d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
1089d5316e3f56d138504565ff311145ac01621dff4Romain Guy            Log.d(LOG_TAG, "clipRect = " + canvas.getClipBounds());
109c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy            Log.d(LOG_TAG, "rejected = " + canvas.quickReject(80.0f, 80.0f, 90.0f, 90.0f,
110c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy                    Canvas.EdgeType.BW));
111c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy            Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f,
112c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy                    Canvas.EdgeType.BW));
1139d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.restore();
114c7d53494f1fbd9f9d74af89053ff9fdb1ccbac6cRomain Guy
115bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            canvas.save();
1169d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.scale(2.0f, 2.0f);
1179d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
1189d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1199d5316e3f56d138504565ff311145ac01621dff4Romain Guy            mPaint.setColor(0xff00ff00);
1209d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.drawRect(0.0f, 0.0f, 20.0f, 20.0f, mPaint);
1219d5316e3f56d138504565ff311145ac01621dff4Romain Guy
1229d5316e3f56d138504565ff311145ac01621dff4Romain Guy            mPaint.setColor(0xff0000ff);
1239d5316e3f56d138504565ff311145ac01621dff4Romain Guy            canvas.drawRect(20.0f, 0.0f, 40.0f, 20.0f, mPaint);
124bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
125bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            canvas.restore();
126bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
127bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            final int restoreTo = canvas.save();
128bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            canvas.saveLayerAlpha(0.0f, 100.0f, getWidth(), 150.0f, 127,
129bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy                    Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
130bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            mPaint.setColor(0xff0000ff);
131bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            canvas.drawRect(0.0f, 100.0f, 40.0f, 150.0f, mPaint);
132bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            mPaint.setColor(0xff00ffff);
133bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            canvas.drawRect(40.0f, 100.0f, 140.0f, 150.0f, mPaint);
134bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            mPaint.setColor(0xffff00ff);
135bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            canvas.drawRect(140.0f, 100.0f, 240.0f, 150.0f, mPaint);
136bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            canvas.restoreToCount(restoreTo);
137bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy
138bd6b79b40247aea7bfe13d0831c6c0472df6c636Romain Guy            super.dispatchDraw(canvas);
1390bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        }
1400bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
1410bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy}
142