AlphaLayersActivity.java revision 0bbae0836426ba2704e38e7f90a9d0ca502ab71d
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
170bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guypackage com.google.android.test.hwui;
180bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
190bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.app.Activity;
200bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.content.Context;
210bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.graphics.Bitmap;
220bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.graphics.Canvas;
230bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.graphics.Paint;
240bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.os.Bundle;
250bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport android.view.View;
260bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
270bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guyimport static android.util.Log.d;
280bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
290bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guypublic class HwUiActivity extends Activity {
300bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    private static final String LOG_TAG = "HwUi";
310bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
320bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    @Override
330bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    protected void onCreate(Bundle savedInstanceState) {
340bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        super.onCreate(savedInstanceState);
350bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
360bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        setContentView(new DirtyBitmapView(this));
370bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
380bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
390bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    static int dipToPx(Context c, int dip) {
400bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        return (int) (c.getResources().getDisplayMetrics().density * dip + 0.5f);
410bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
420bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
430bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    static class DirtyBitmapView extends View {
440bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        private Bitmap mCache;
450bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
460bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        DirtyBitmapView(Context c) {
470bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            super(c);
480bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
490bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            final int width = dipToPx(c, 100);
500bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            final int height = dipToPx(c, 100);
510bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
520bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            mCache = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
530bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            logGenerationId("Dirty cache created", mCache);
540bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
550bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            Canvas canvas = new Canvas(mCache);
560bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            logGenerationId("Canvas cache created", mCache);
570bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
580bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            canvas.drawColor(0xffff0000);
590bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            logGenerationId("Cache filled", mCache);
600bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
610bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            Paint p = new Paint();
620bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            p.setColor(0xff0000ff);
630bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
640bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            canvas.drawRect(width / 2.0f, height / 2.0f, width, height, p);
650bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            logGenerationId("Cache modified", mCache);
660bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        }
670bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
680bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        private static void logGenerationId(String message, Bitmap b) {
690bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            d(LOG_TAG, message);
700bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            d(LOG_TAG, "  bitmap id=" + b.getGenerationId());
710bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        }
720bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
730bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        @Override
740bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        protected void onDraw(Canvas canvas) {
750bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            super.onDraw(canvas);
760bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy
770bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            canvas.drawBitmap(mCache, 0, 0, null);
780bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy            logGenerationId("Cache drawn", mCache);
790bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy        }
800bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy    }
810bbae0836426ba2704e38e7f90a9d0ca502ab71dRomain Guy}
82