Home | History | Annotate | Download | only in hwui
      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
     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"})
     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 {
     60         ColorView(Context c) {
     61             super(c);
     62         }
     63 
     64         @Override
     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 
     74         DirtyBitmapView(Context c) {
     75             super(c);
     76             mPaint = new Paint();
     77         }
     78 
     79         @Override
     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                     Canvas.EdgeType.BW));
     91             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f,
     92                     Canvas.EdgeType.BW));
     93             canvas.restore();
     94 
     95             canvas.save();
     96             canvas.scale(2.0f, 2.0f);
     97             canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
     98             Log.d(LOG_TAG, "clipRect = " + canvas.getClipBounds());
     99             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(50.0f, 50.0f, 60.0f, 60.0f,
    100                     Canvas.EdgeType.BW));
    101             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f,
    102                     Canvas.EdgeType.BW));
    103             canvas.restore();
    104 
    105             canvas.save();
    106             canvas.translate(20.0f, 20.0f);
    107             canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
    108             Log.d(LOG_TAG, "clipRect = " + canvas.getClipBounds());
    109             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(80.0f, 80.0f, 90.0f, 90.0f,
    110                     Canvas.EdgeType.BW));
    111             Log.d(LOG_TAG, "rejected = " + canvas.quickReject(25.0f, 5.0f, 30.0f, 10.0f,
    112                     Canvas.EdgeType.BW));
    113             canvas.restore();
    114 
    115             canvas.save();
    116             canvas.scale(2.0f, 2.0f);
    117             canvas.clipRect(20.0f, 0.0f, 40.0f, 20.0f);
    118 
    119             mPaint.setColor(0xff00ff00);
    120             canvas.drawRect(0.0f, 0.0f, 20.0f, 20.0f, mPaint);
    121 
    122             mPaint.setColor(0xff0000ff);
    123             canvas.drawRect(20.0f, 0.0f, 40.0f, 20.0f, mPaint);
    124 
    125             canvas.restore();
    126 
    127             final int restoreTo = canvas.save();
    128             canvas.saveLayerAlpha(0.0f, 100.0f, getWidth(), 150.0f, 127,
    129                     Canvas.HAS_ALPHA_LAYER_SAVE_FLAG | Canvas.CLIP_TO_LAYER_SAVE_FLAG);
    130             mPaint.setColor(0xff0000ff);
    131             canvas.drawRect(0.0f, 100.0f, 40.0f, 150.0f, mPaint);
    132             mPaint.setColor(0xff00ffff);
    133             canvas.drawRect(40.0f, 100.0f, 140.0f, 150.0f, mPaint);
    134             mPaint.setColor(0xffff00ff);
    135             canvas.drawRect(140.0f, 100.0f, 240.0f, 150.0f, mPaint);
    136             canvas.restoreToCount(restoreTo);
    137 
    138             super.dispatchDraw(canvas);
    139         }
    140     }
    141 }
    142