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