ColorFiltersActivity.java revision f607bdc167f66b3e7003acaa4736ae46d78c1492
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
17package com.android.test.hwui;
18
19import android.app.Activity;
20import android.content.Context;
21import android.graphics.Bitmap;
22import android.graphics.BitmapFactory;
23import android.graphics.Canvas;
24import android.graphics.ColorMatrix;
25import android.graphics.ColorMatrixColorFilter;
26import android.graphics.LightingColorFilter;
27import android.graphics.Paint;
28import android.graphics.PorterDuff;
29import android.graphics.PorterDuffColorFilter;
30import android.os.Bundle;
31import android.view.View;
32
33@SuppressWarnings({"UnusedDeclaration"})
34public 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