Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2007 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.example.android.apis.graphics;
     18 
     19 import com.example.android.apis.R;
     20 
     21 import android.content.Context;
     22 import android.graphics.*;
     23 import android.os.Bundle;
     24 import android.view.View;
     25 
     26 public class ColorMatrixSample extends GraphicsActivity {
     27 
     28     @Override
     29     protected void onCreate(Bundle savedInstanceState) {
     30         super.onCreate(savedInstanceState);
     31         setContentView(new SampleView(this));
     32     }
     33 
     34     private static class SampleView extends View {
     35         private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
     36         private Bitmap mBitmap;
     37         private float mAngle;
     38 
     39         public SampleView(Context context) {
     40             super(context);
     41 
     42             mBitmap = BitmapFactory.decodeResource(context.getResources(),
     43                                                    R.drawable.balloons);
     44         }
     45 
     46         private static void setTranslate(ColorMatrix cm, float dr, float dg,
     47                                          float db, float da) {
     48             cm.set(new float[] {
     49                    2, 0, 0, 0, dr,
     50                    0, 2, 0, 0, dg,
     51                    0, 0, 2, 0, db,
     52                    0, 0, 0, 1, da });
     53         }
     54 
     55         private static void setContrast(ColorMatrix cm, float contrast) {
     56             float scale = contrast + 1.f;
     57                float translate = (-.5f * scale + .5f) * 255.f;
     58             cm.set(new float[] {
     59                    scale, 0, 0, 0, translate,
     60                    0, scale, 0, 0, translate,
     61                    0, 0, scale, 0, translate,
     62                    0, 0, 0, 1, 0 });
     63         }
     64 
     65         private static void setContrastTranslateOnly(ColorMatrix cm, float contrast) {
     66             float scale = contrast + 1.f;
     67                float translate = (-.5f * scale + .5f) * 255.f;
     68             cm.set(new float[] {
     69                    1, 0, 0, 0, translate,
     70                    0, 1, 0, 0, translate,
     71                    0, 0, 1, 0, translate,
     72                    0, 0, 0, 1, 0 });
     73         }
     74 
     75         private static void setContrastScaleOnly(ColorMatrix cm, float contrast) {
     76             float scale = contrast + 1.f;
     77                float translate = (-.5f * scale + .5f) * 255.f;
     78             cm.set(new float[] {
     79                    scale, 0, 0, 0, 0,
     80                    0, scale, 0, 0, 0,
     81                    0, 0, scale, 0, 0,
     82                    0, 0, 0, 1, 0 });
     83         }
     84 
     85         @Override protected void onDraw(Canvas canvas) {
     86             Paint paint = mPaint;
     87             float x = 20;
     88             float y = 20;
     89 
     90             canvas.drawColor(Color.WHITE);
     91 
     92             paint.setColorFilter(null);
     93             canvas.drawBitmap(mBitmap, x, y, paint);
     94 
     95             ColorMatrix cm = new ColorMatrix();
     96 
     97             mAngle += 2;
     98             if (mAngle > 180) {
     99                 mAngle = 0;
    100             }
    101 
    102             //convert our animated angle [-180...180] to a contrast value of [-1..1]
    103             float contrast = mAngle / 180.f;
    104 
    105             setContrast(cm, contrast);
    106             paint.setColorFilter(new ColorMatrixColorFilter(cm));
    107             canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint);
    108 
    109             setContrastScaleOnly(cm, contrast);
    110             paint.setColorFilter(new ColorMatrixColorFilter(cm));
    111             canvas.drawBitmap(mBitmap, x, y + mBitmap.getHeight() + 10, paint);
    112 
    113             setContrastTranslateOnly(cm, contrast);
    114             paint.setColorFilter(new ColorMatrixColorFilter(cm));
    115             canvas.drawBitmap(mBitmap, x, y + 2*(mBitmap.getHeight() + 10),
    116                               paint);
    117 
    118             invalidate();
    119         }
    120     }
    121 }
    122 
    123