113656743cc21bac43676568314366497346713eeRomain Guy/*
213656743cc21bac43676568314366497346713eeRomain Guy * Copyright (C) 2010 The Android Open Source Project
313656743cc21bac43676568314366497346713eeRomain Guy *
413656743cc21bac43676568314366497346713eeRomain Guy * Licensed under the Apache License, Version 2.0 (the "License");
513656743cc21bac43676568314366497346713eeRomain Guy * you may not use this file except in compliance with the License.
613656743cc21bac43676568314366497346713eeRomain Guy * You may obtain a copy of the License at
713656743cc21bac43676568314366497346713eeRomain Guy *
813656743cc21bac43676568314366497346713eeRomain Guy *      http://www.apache.org/licenses/LICENSE-2.0
913656743cc21bac43676568314366497346713eeRomain Guy *
1013656743cc21bac43676568314366497346713eeRomain Guy * Unless required by applicable law or agreed to in writing, software
1113656743cc21bac43676568314366497346713eeRomain Guy * distributed under the License is distributed on an "AS IS" BASIS,
1213656743cc21bac43676568314366497346713eeRomain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313656743cc21bac43676568314366497346713eeRomain Guy * See the License for the specific language governing permissions and
1413656743cc21bac43676568314366497346713eeRomain Guy * limitations under the License.
1513656743cc21bac43676568314366497346713eeRomain Guy */
1613656743cc21bac43676568314366497346713eeRomain Guy
1713656743cc21bac43676568314366497346713eeRomain Guypackage com.android.test.hwui;
1813656743cc21bac43676568314366497346713eeRomain Guy
1913656743cc21bac43676568314366497346713eeRomain Guyimport android.animation.ArgbEvaluator;
2013656743cc21bac43676568314366497346713eeRomain Guyimport android.animation.ObjectAnimator;
2113656743cc21bac43676568314366497346713eeRomain Guyimport android.app.Activity;
2213656743cc21bac43676568314366497346713eeRomain Guyimport android.content.Context;
2313656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.Bitmap;
2413656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.BitmapFactory;
2513656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.Canvas;
2613656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.ColorMatrix;
2713656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.ColorMatrixColorFilter;
2813656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.LightingColorFilter;
2913656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.Paint;
3013656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.PorterDuff;
3113656743cc21bac43676568314366497346713eeRomain Guyimport android.graphics.PorterDuffColorFilter;
3213656743cc21bac43676568314366497346713eeRomain Guyimport android.os.Bundle;
3313656743cc21bac43676568314366497346713eeRomain Guyimport android.view.View;
3413656743cc21bac43676568314366497346713eeRomain Guy
3513656743cc21bac43676568314366497346713eeRomain Guy@SuppressWarnings({"UnusedDeclaration"})
3613656743cc21bac43676568314366497346713eeRomain Guypublic class ColorFiltersMutateActivity extends Activity {
3713656743cc21bac43676568314366497346713eeRomain Guy    @Override
3813656743cc21bac43676568314366497346713eeRomain Guy    protected void onCreate(Bundle savedInstanceState) {
3913656743cc21bac43676568314366497346713eeRomain Guy        super.onCreate(savedInstanceState);
4013656743cc21bac43676568314366497346713eeRomain Guy        final BitmapsView view = new BitmapsView(this);
4113656743cc21bac43676568314366497346713eeRomain Guy        setContentView(view);
4213656743cc21bac43676568314366497346713eeRomain Guy    }
4313656743cc21bac43676568314366497346713eeRomain Guy
4413656743cc21bac43676568314366497346713eeRomain Guy    static class BitmapsView extends View {
4513656743cc21bac43676568314366497346713eeRomain Guy        private final Bitmap mBitmap1;
4613656743cc21bac43676568314366497346713eeRomain Guy        private final Bitmap mBitmap2;
4713656743cc21bac43676568314366497346713eeRomain Guy        private final Paint mColorMatrixPaint;
4813656743cc21bac43676568314366497346713eeRomain Guy        private final Paint mLightingPaint;
4913656743cc21bac43676568314366497346713eeRomain Guy        private final Paint mBlendPaint;
5013656743cc21bac43676568314366497346713eeRomain Guy
5113656743cc21bac43676568314366497346713eeRomain Guy        private float mSaturation = 0.0f;
5213656743cc21bac43676568314366497346713eeRomain Guy        private int mLightAdd = 0;
5313656743cc21bac43676568314366497346713eeRomain Guy        private int mLightMul = 0;
5413656743cc21bac43676568314366497346713eeRomain Guy        private int mPorterDuffColor = 0;
5513656743cc21bac43676568314366497346713eeRomain Guy
5613656743cc21bac43676568314366497346713eeRomain Guy        BitmapsView(Context c) {
5713656743cc21bac43676568314366497346713eeRomain Guy            super(c);
5813656743cc21bac43676568314366497346713eeRomain Guy
5913656743cc21bac43676568314366497346713eeRomain Guy            mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
6013656743cc21bac43676568314366497346713eeRomain Guy            mBitmap2 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset2);
6113656743cc21bac43676568314366497346713eeRomain Guy
6213656743cc21bac43676568314366497346713eeRomain Guy            mColorMatrixPaint = new Paint();
6313656743cc21bac43676568314366497346713eeRomain Guy            final ColorMatrix colorMatrix = new ColorMatrix();
6413656743cc21bac43676568314366497346713eeRomain Guy            colorMatrix.setSaturation(0);
6513656743cc21bac43676568314366497346713eeRomain Guy            mColorMatrixPaint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));
6613656743cc21bac43676568314366497346713eeRomain Guy
6713656743cc21bac43676568314366497346713eeRomain Guy            mLightingPaint = new Paint();
6813656743cc21bac43676568314366497346713eeRomain Guy            mLightingPaint.setColorFilter(new LightingColorFilter(0, 0));
6913656743cc21bac43676568314366497346713eeRomain Guy
7013656743cc21bac43676568314366497346713eeRomain Guy            mBlendPaint = new Paint();
7113656743cc21bac43676568314366497346713eeRomain Guy            mBlendPaint.setColorFilter(new PorterDuffColorFilter(0, PorterDuff.Mode.SRC_OVER));
7213656743cc21bac43676568314366497346713eeRomain Guy
7313656743cc21bac43676568314366497346713eeRomain Guy            ObjectAnimator sat = ObjectAnimator.ofFloat(this, "saturation", 1.0f);
7413656743cc21bac43676568314366497346713eeRomain Guy            sat.setDuration(1000);
7513656743cc21bac43676568314366497346713eeRomain Guy            sat.setRepeatCount(ObjectAnimator.INFINITE);
7613656743cc21bac43676568314366497346713eeRomain Guy            sat.setRepeatMode(ObjectAnimator.REVERSE);
7713656743cc21bac43676568314366497346713eeRomain Guy            sat.start();
7813656743cc21bac43676568314366497346713eeRomain Guy
7913656743cc21bac43676568314366497346713eeRomain Guy            ObjectAnimator light = ObjectAnimator.ofInt(this, "lightAdd", 0x00101030);
8013656743cc21bac43676568314366497346713eeRomain Guy            light.setEvaluator(new ArgbEvaluator());
8113656743cc21bac43676568314366497346713eeRomain Guy            light.setDuration(1000);
8213656743cc21bac43676568314366497346713eeRomain Guy            light.setRepeatCount(ObjectAnimator.INFINITE);
8313656743cc21bac43676568314366497346713eeRomain Guy            light.setRepeatMode(ObjectAnimator.REVERSE);
8413656743cc21bac43676568314366497346713eeRomain Guy            light.start();
8513656743cc21bac43676568314366497346713eeRomain Guy
8613656743cc21bac43676568314366497346713eeRomain Guy            ObjectAnimator mult = ObjectAnimator.ofInt(this, "lightMul", 0x0060ffff);
8713656743cc21bac43676568314366497346713eeRomain Guy            mult.setEvaluator(new ArgbEvaluator());
8813656743cc21bac43676568314366497346713eeRomain Guy            mult.setDuration(1000);
8913656743cc21bac43676568314366497346713eeRomain Guy            mult.setRepeatCount(ObjectAnimator.INFINITE);
9013656743cc21bac43676568314366497346713eeRomain Guy            mult.setRepeatMode(ObjectAnimator.REVERSE);
9113656743cc21bac43676568314366497346713eeRomain Guy            mult.start();
9213656743cc21bac43676568314366497346713eeRomain Guy
9313656743cc21bac43676568314366497346713eeRomain Guy            ObjectAnimator color = ObjectAnimator.ofInt(this, "porterDuffColor", 0x7f990040);
9413656743cc21bac43676568314366497346713eeRomain Guy            color.setEvaluator(new ArgbEvaluator());
9513656743cc21bac43676568314366497346713eeRomain Guy            color.setDuration(1000);
9613656743cc21bac43676568314366497346713eeRomain Guy            color.setRepeatCount(ObjectAnimator.INFINITE);
9713656743cc21bac43676568314366497346713eeRomain Guy            color.setRepeatMode(ObjectAnimator.REVERSE);
9813656743cc21bac43676568314366497346713eeRomain Guy            color.start();
9913656743cc21bac43676568314366497346713eeRomain Guy        }
10013656743cc21bac43676568314366497346713eeRomain Guy
10113656743cc21bac43676568314366497346713eeRomain Guy        public int getPorterDuffColor() {
10213656743cc21bac43676568314366497346713eeRomain Guy            return mPorterDuffColor;
10313656743cc21bac43676568314366497346713eeRomain Guy        }
10413656743cc21bac43676568314366497346713eeRomain Guy
10513656743cc21bac43676568314366497346713eeRomain Guy        public void setPorterDuffColor(int porterDuffColor) {
10613656743cc21bac43676568314366497346713eeRomain Guy            mPorterDuffColor = porterDuffColor;
10713656743cc21bac43676568314366497346713eeRomain Guy            final PorterDuffColorFilter filter =
10813656743cc21bac43676568314366497346713eeRomain Guy                    (PorterDuffColorFilter) mBlendPaint.getColorFilter();
10913656743cc21bac43676568314366497346713eeRomain Guy            filter.setColor(mPorterDuffColor);
11013656743cc21bac43676568314366497346713eeRomain Guy            invalidate();
11113656743cc21bac43676568314366497346713eeRomain Guy        }
11213656743cc21bac43676568314366497346713eeRomain Guy
11313656743cc21bac43676568314366497346713eeRomain Guy        public int getLightAdd() {
11413656743cc21bac43676568314366497346713eeRomain Guy            return mLightAdd;
11513656743cc21bac43676568314366497346713eeRomain Guy        }
11613656743cc21bac43676568314366497346713eeRomain Guy
11713656743cc21bac43676568314366497346713eeRomain Guy        public void setLightAdd(int lightAdd) {
11813656743cc21bac43676568314366497346713eeRomain Guy            mLightAdd = lightAdd;
11913656743cc21bac43676568314366497346713eeRomain Guy            final LightingColorFilter filter =
12013656743cc21bac43676568314366497346713eeRomain Guy                    (LightingColorFilter) mLightingPaint.getColorFilter();
12113656743cc21bac43676568314366497346713eeRomain Guy            filter.setColorAdd(lightAdd);
12213656743cc21bac43676568314366497346713eeRomain Guy            invalidate();
12313656743cc21bac43676568314366497346713eeRomain Guy        }
12413656743cc21bac43676568314366497346713eeRomain Guy
12513656743cc21bac43676568314366497346713eeRomain Guy        public int getLightMul() {
12613656743cc21bac43676568314366497346713eeRomain Guy            return mLightAdd;
12713656743cc21bac43676568314366497346713eeRomain Guy        }
12813656743cc21bac43676568314366497346713eeRomain Guy
12913656743cc21bac43676568314366497346713eeRomain Guy        public void setLightMul(int lightMul) {
13013656743cc21bac43676568314366497346713eeRomain Guy            mLightMul = lightMul;
13113656743cc21bac43676568314366497346713eeRomain Guy            final LightingColorFilter filter =
13213656743cc21bac43676568314366497346713eeRomain Guy                    (LightingColorFilter) mLightingPaint.getColorFilter();
13313656743cc21bac43676568314366497346713eeRomain Guy            filter.setColorMultiply(lightMul);
13413656743cc21bac43676568314366497346713eeRomain Guy            invalidate();
13513656743cc21bac43676568314366497346713eeRomain Guy        }
13613656743cc21bac43676568314366497346713eeRomain Guy
13713656743cc21bac43676568314366497346713eeRomain Guy        public void setSaturation(float saturation) {
13813656743cc21bac43676568314366497346713eeRomain Guy            mSaturation = saturation;
13913656743cc21bac43676568314366497346713eeRomain Guy            final ColorMatrixColorFilter filter =
14013656743cc21bac43676568314366497346713eeRomain Guy                    (ColorMatrixColorFilter) mColorMatrixPaint.getColorFilter();
14113656743cc21bac43676568314366497346713eeRomain Guy            final ColorMatrix m = filter.getColorMatrix();
14213656743cc21bac43676568314366497346713eeRomain Guy            m.setSaturation(saturation);
14313656743cc21bac43676568314366497346713eeRomain Guy            filter.setColorMatrix(m);
14413656743cc21bac43676568314366497346713eeRomain Guy            invalidate();
14513656743cc21bac43676568314366497346713eeRomain Guy        }
14613656743cc21bac43676568314366497346713eeRomain Guy
14713656743cc21bac43676568314366497346713eeRomain Guy        public float getSaturation() {
14813656743cc21bac43676568314366497346713eeRomain Guy            return mSaturation;
14913656743cc21bac43676568314366497346713eeRomain Guy        }
15013656743cc21bac43676568314366497346713eeRomain Guy
15113656743cc21bac43676568314366497346713eeRomain Guy        @Override
15213656743cc21bac43676568314366497346713eeRomain Guy        protected void onDraw(Canvas canvas) {
15313656743cc21bac43676568314366497346713eeRomain Guy            super.onDraw(canvas);
15413656743cc21bac43676568314366497346713eeRomain Guy
15513656743cc21bac43676568314366497346713eeRomain Guy            canvas.drawARGB(255, 255, 255, 255);
15613656743cc21bac43676568314366497346713eeRomain Guy
15713656743cc21bac43676568314366497346713eeRomain Guy            canvas.save();
15813656743cc21bac43676568314366497346713eeRomain Guy            canvas.translate(120.0f, 50.0f);
15913656743cc21bac43676568314366497346713eeRomain Guy            canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mColorMatrixPaint);
16013656743cc21bac43676568314366497346713eeRomain Guy
16113656743cc21bac43676568314366497346713eeRomain Guy            canvas.translate(0.0f, 50.0f + mBitmap1.getHeight());
16213656743cc21bac43676568314366497346713eeRomain Guy            canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mLightingPaint);
16313656743cc21bac43676568314366497346713eeRomain Guy
16413656743cc21bac43676568314366497346713eeRomain Guy            canvas.translate(0.0f, 50.0f + mBitmap1.getHeight());
16513656743cc21bac43676568314366497346713eeRomain Guy            canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBlendPaint);
16613656743cc21bac43676568314366497346713eeRomain Guy            canvas.restore();
16713656743cc21bac43676568314366497346713eeRomain Guy
16813656743cc21bac43676568314366497346713eeRomain Guy            canvas.save();
16913656743cc21bac43676568314366497346713eeRomain Guy            canvas.translate(120.0f + mBitmap1.getWidth() + 120.0f, 50.0f);
17013656743cc21bac43676568314366497346713eeRomain Guy            canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mColorMatrixPaint);
17113656743cc21bac43676568314366497346713eeRomain Guy
17213656743cc21bac43676568314366497346713eeRomain Guy            canvas.translate(0.0f, 50.0f + mBitmap2.getHeight());
17313656743cc21bac43676568314366497346713eeRomain Guy            canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mLightingPaint);
17413656743cc21bac43676568314366497346713eeRomain Guy
17513656743cc21bac43676568314366497346713eeRomain Guy            canvas.translate(0.0f, 50.0f + mBitmap2.getHeight());
17613656743cc21bac43676568314366497346713eeRomain Guy            canvas.drawBitmap(mBitmap2, 0.0f, 0.0f, mBlendPaint);
17713656743cc21bac43676568314366497346713eeRomain Guy            canvas.restore();
17813656743cc21bac43676568314366497346713eeRomain Guy        }
17913656743cc21bac43676568314366497346713eeRomain Guy    }
18013656743cc21bac43676568314366497346713eeRomain Guy}
181