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.Bitmap;
     22 import android.graphics.BitmapFactory;
     23 import android.graphics.Canvas;
     24 import android.graphics.ColorMatrix;
     25 import android.graphics.ColorMatrixColorFilter;
     26 import android.graphics.LightingColorFilter;
     27 import android.graphics.Paint;
     28 import android.graphics.PorterDuff;
     29 import android.graphics.PorterDuffColorFilter;
     30 import android.os.Bundle;
     31 import android.view.View;
     32 
     33 @SuppressWarnings({"UnusedDeclaration"})
     34 public class ColorFiltersActivity extends Activity {
     35     @Override
     36     protected void onCreate(Bundle savedInstanceState) {
     37         super.onCreate(savedInstanceState);
     38         final BitmapsView view = new BitmapsView(this);
     39         setContentView(view);
     40     }
     41 
     42     static class BitmapsView extends View {
     43         private final Bitmap mBitmap1;
     44         private final Bitmap mBitmap2;
     45         private final Paint mColorMatrixPaint;
     46         private final Paint mLightingPaint;
     47         private final Paint mBlendPaint;
     48 
     49         BitmapsView(Context c) {
     50             super(c);
     51 
     52             mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
     53             mBitmap2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset2);
     54 
     55             mColorMatrixPaint = new Paint();
     56             final ColorMatrix colorMatrix = new ColorMatrix();
     57             colorMatrix.setSaturation(0);
     58             mColorMatrixPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
     59 
     60             mLightingPaint = new Paint();
     61             mLightingPaint.setColorFilter(new LightingColorFilter(0x0060ffff, 0x00101030));
     62 
     63             mBlendPaint = new Paint();
     64             mBlendPaint.setColorFilter(new PorterDuffColorFilter(0x7f990040,
     65                     PorterDuff.Mode.SRC_OVER));
     66         }
     67 
     68         @Override
     69         protected void onDraw(Canvas canvas) {
     70             super.onDraw(canvas);
     71 
     72             canvas.drawARGB(255, 255, 255, 255);
     73 
     74             canvas.save();
     75             canvas.translate(120.0f, 50.0f);
     76             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mColorMatrixPaint);
     77 
     78             canvas.translate(0.0f, 50.0f + mBitmap1.getHeight());
     79             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mLightingPaint);
     80 
     81             canvas.translate(0.0f, 50.0f + mBitmap1.getHeight());
     82             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBlendPaint);
     83             canvas.restore();
     84 
     85             canvas.save();
     86             canvas.translate(120.0f + mBitmap1.getWidth() + 120.0f, 50.0f);
     87             canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mColorMatrixPaint);
     88 
     89             canvas.translate(0.0f, 50.0f + mBitmap2.getHeight());
     90             canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mLightingPaint);
     91 
     92             canvas.translate(0.0f, 50.0f + mBitmap2.getHeight());
     93             canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mBlendPaint);
     94             canvas.restore();
     95         }
     96     }
     97 }
     98