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.animation.ArgbEvaluator;
     20 import android.animation.ObjectAnimator;
     21 import android.app.Activity;
     22 import android.content.Context;
     23 import android.graphics.Bitmap;
     24 import android.graphics.BitmapFactory;
     25 import android.graphics.Canvas;
     26 import android.graphics.ColorMatrix;
     27 import android.graphics.ColorMatrixColorFilter;
     28 import android.graphics.LightingColorFilter;
     29 import android.graphics.Paint;
     30 import android.graphics.PorterDuff;
     31 import android.graphics.PorterDuffColorFilter;
     32 import android.os.Bundle;
     33 import android.view.View;
     34 
     35 @SuppressWarnings({"UnusedDeclaration"})
     36 public class ColorFiltersMutateActivity extends Activity {
     37     @Override
     38     protected void onCreate(Bundle savedInstanceState) {
     39         super.onCreate(savedInstanceState);
     40         final BitmapsView view = new BitmapsView(this);
     41         setContentView(view);
     42     }
     43 
     44     static class BitmapsView extends View {
     45         private final Bitmap mBitmap1;
     46         private final Bitmap mBitmap2;
     47         private final Paint mColorMatrixPaint;
     48         private final Paint mLightingPaint;
     49         private final Paint mBlendPaint;
     50 
     51         private float mSaturation = 0.0f;
     52         private int mLightAdd = 0;
     53         private int mLightMul = 0;
     54         private int mPorterDuffColor = 0;
     55 
     56         BitmapsView(Context c) {
     57             super(c);
     58 
     59             mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
     60             mBitmap2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset2);
     61 
     62             mColorMatrixPaint = new Paint();
     63             final ColorMatrix colorMatrix = new ColorMatrix();
     64             colorMatrix.setSaturation(0);
     65             mColorMatrixPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
     66 
     67             mLightingPaint = new Paint();
     68             mLightingPaint.setColorFilter(new LightingColorFilter(0, 0));
     69 
     70             mBlendPaint = new Paint();
     71             mBlendPaint.setColorFilter(new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_OVER));
     72 
     73             ObjectAnimator sat = ObjectAnimator.ofFloat(this, "saturation", 1.0f);
     74             sat.setDuration(1000);
     75             sat.setRepeatCount(ObjectAnimator.INFINITE);
     76             sat.setRepeatMode(ObjectAnimator.REVERSE);
     77             sat.start();
     78 
     79             ObjectAnimator light = ObjectAnimator.ofInt(this, "lightAdd", 0x00101030);
     80             light.setEvaluator(new ArgbEvaluator());
     81             light.setDuration(1000);
     82             light.setRepeatCount(ObjectAnimator.INFINITE);
     83             light.setRepeatMode(ObjectAnimator.REVERSE);
     84             light.start();
     85 
     86             ObjectAnimator mult = ObjectAnimator.ofInt(this, "lightMul", 0x0060ffff);
     87             mult.setEvaluator(new ArgbEvaluator());
     88             mult.setDuration(1000);
     89             mult.setRepeatCount(ObjectAnimator.INFINITE);
     90             mult.setRepeatMode(ObjectAnimator.REVERSE);
     91             mult.start();
     92 
     93             ObjectAnimator color = ObjectAnimator.ofInt(this, "porterDuffColor", 0x7f990040);
     94             color.setEvaluator(new ArgbEvaluator());
     95             color.setDuration(1000);
     96             color.setRepeatCount(ObjectAnimator.INFINITE);
     97             color.setRepeatMode(ObjectAnimator.REVERSE);
     98             color.start();
     99         }
    100 
    101         public int getPorterDuffColor() {
    102             return mPorterDuffColor;
    103         }
    104 
    105         public void setPorterDuffColor(int porterDuffColor) {
    106             mPorterDuffColor = porterDuffColor;
    107             final PorterDuffColorFilter filter =
    108                     (PorterDuffColorFilter) mBlendPaint.getColorFilter();
    109             filter.setColor(mPorterDuffColor);
    110             invalidate();
    111         }
    112 
    113         public int getLightAdd() {
    114             return mLightAdd;
    115         }
    116 
    117         public void setLightAdd(int lightAdd) {
    118             mLightAdd = lightAdd;
    119             final LightingColorFilter filter =
    120                     (LightingColorFilter) mLightingPaint.getColorFilter();
    121             filter.setColorAdd(lightAdd);
    122             invalidate();
    123         }
    124 
    125         public int getLightMul() {
    126             return mLightAdd;
    127         }
    128 
    129         public void setLightMul(int lightMul) {
    130             mLightMul = lightMul;
    131             final LightingColorFilter filter =
    132                     (LightingColorFilter) mLightingPaint.getColorFilter();
    133             filter.setColorMultiply(lightMul);
    134             invalidate();
    135         }
    136 
    137         public void setSaturation(float saturation) {
    138             mSaturation = saturation;
    139             final ColorMatrixColorFilter filter =
    140                     (ColorMatrixColorFilter) mColorMatrixPaint.getColorFilter();
    141             final ColorMatrix m = filter.getColorMatrix();
    142             m.setSaturation(saturation);
    143             filter.setColorMatrix(m);
    144             invalidate();
    145         }
    146 
    147         public float getSaturation() {
    148             return mSaturation;
    149         }
    150 
    151         @Override
    152         protected void onDraw(Canvas canvas) {
    153             super.onDraw(canvas);
    154 
    155             canvas.drawARGB(255, 255, 255, 255);
    156 
    157             canvas.save();
    158             canvas.translate(120.0f, 50.0f);
    159             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mColorMatrixPaint);
    160 
    161             canvas.translate(0.0f, 50.0f + mBitmap1.getHeight());
    162             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mLightingPaint);
    163 
    164             canvas.translate(0.0f, 50.0f + mBitmap1.getHeight());
    165             canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBlendPaint);
    166             canvas.restore();
    167 
    168             canvas.save();
    169             canvas.translate(120.0f + mBitmap1.getWidth() + 120.0f, 50.0f);
    170             canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mColorMatrixPaint);
    171 
    172             canvas.translate(0.0f, 50.0f + mBitmap2.getHeight());
    173             canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mLightingPaint);
    174 
    175             canvas.translate(0.0f, 50.0f + mBitmap2.getHeight());
    176             canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mBlendPaint);
    177             canvas.restore();
    178         }
    179     }
    180 }
    181