English | Site Directory

Android - An Open Handset Alliance Project

ColorMatrixSample.java

samples/ApiDemos/src/com/google/android/samples/graphics/

Original ColorMatrixSample.java

/* 
 * Copyright (C) 2007 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.samples.graphics;

import com.google.android.samples.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.*;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;

public class ColorMatrixSample extends Activity {

    @Override
    protected void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(new SampleView(this));
    }

    private static class SampleView extends View {
        private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        private ColorMatrix mCM = new ColorMatrix();
        private Bitmap mBitmap;
        private float mSaturation;
        private float mAngle;

        public SampleView(Context context) {
            super(context);
            setFocusable(true);

            mBitmap = BitmapFactory.decodeResource(context.getResources(),
                                                   R.drawable.balloons);
        }

        @Override protected void onDraw(Canvas canvas) {
            Paint paint = mPaint;
            float x = 20;
            float y = 20;

            canvas.drawColor(Color.WHITE);

            paint.setColorFilter(null);
            canvas.drawBitmap(mBitmap, x, y, paint);

            mCM.setSaturation(mSaturation);
            paint.setColorFilter(new ColorMatrixFilter(mCM));
            canvas.drawBitmap(mBitmap, x + mBitmap.getWidth() + 10, y, paint);
            mSaturation += 1.f/10;
            if (mSaturation > 1) {
                mSaturation = -1;
            }

            mCM.setRGB2YUV();
            ColorMatrix cm = new ColorMatrix();
            cm.setRotate(0, mAngle);
            mCM.postConcat(cm);
            cm.setYUV2RGB();
            mCM.postConcat(cm);
            paint.setColorFilter(new ColorMatrixFilter(mCM));
            canvas.drawBitmap(mBitmap, x, y + mBitmap.getHeight() + 10, paint);
            mAngle += 10;
            if (mAngle > 180) {
                mAngle = -180;
            }

            invalidate();
        }

        @Override public boolean onKeyDown(int keyCode, KeyEvent event) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_S:
//                    mDoSat = !mDoSat;
                    invalidate();
                    return true;
            }
            return super.onKeyDown(keyCode, event);
        }
    }
}
Build m5-rc15i - 10 Jun 2008 13:54