1886b275e529e44a59c54b933453d9bc902973178Romain Guy/*
2886b275e529e44a59c54b933453d9bc902973178Romain Guy * Copyright (C) 2010 The Android Open Source Project
3886b275e529e44a59c54b933453d9bc902973178Romain Guy *
4886b275e529e44a59c54b933453d9bc902973178Romain Guy * Licensed under the Apache License, Version 2.0 (the "License");
5886b275e529e44a59c54b933453d9bc902973178Romain Guy * you may not use this file except in compliance with the License.
6886b275e529e44a59c54b933453d9bc902973178Romain Guy * You may obtain a copy of the License at
7886b275e529e44a59c54b933453d9bc902973178Romain Guy *
8886b275e529e44a59c54b933453d9bc902973178Romain Guy *      http://www.apache.org/licenses/LICENSE-2.0
9886b275e529e44a59c54b933453d9bc902973178Romain Guy *
10886b275e529e44a59c54b933453d9bc902973178Romain Guy * Unless required by applicable law or agreed to in writing, software
11886b275e529e44a59c54b933453d9bc902973178Romain Guy * distributed under the License is distributed on an "AS IS" BASIS,
12886b275e529e44a59c54b933453d9bc902973178Romain Guy * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13886b275e529e44a59c54b933453d9bc902973178Romain Guy * See the License for the specific language governing permissions and
14886b275e529e44a59c54b933453d9bc902973178Romain Guy * limitations under the License.
15886b275e529e44a59c54b933453d9bc902973178Romain Guy */
16886b275e529e44a59c54b933453d9bc902973178Romain Guy
17886b275e529e44a59c54b933453d9bc902973178Romain Guypackage com.android.test.hwui;
18886b275e529e44a59c54b933453d9bc902973178Romain Guy
19886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.app.Activity;
20886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.content.Context;
21886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.Bitmap;
22886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.BitmapFactory;
23886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.BitmapShader;
24886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.Canvas;
25886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.Matrix;
26886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.Paint;
27886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.Rect;
28886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.RectF;
29886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.graphics.Shader;
30886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.os.Bundle;
31886b275e529e44a59c54b933453d9bc902973178Romain Guyimport android.view.View;
32886b275e529e44a59c54b933453d9bc902973178Romain Guy
33886b275e529e44a59c54b933453d9bc902973178Romain Guy@SuppressWarnings({"UnusedDeclaration"})
34886b275e529e44a59c54b933453d9bc902973178Romain Guypublic class Alpha8BitmapActivity extends Activity {
35886b275e529e44a59c54b933453d9bc902973178Romain Guy    @Override
36886b275e529e44a59c54b933453d9bc902973178Romain Guy    protected void onCreate(Bundle savedInstanceState) {
37886b275e529e44a59c54b933453d9bc902973178Romain Guy        super.onCreate(savedInstanceState);
38886b275e529e44a59c54b933453d9bc902973178Romain Guy        setContentView(new BitmapsView(this));
39886b275e529e44a59c54b933453d9bc902973178Romain Guy    }
40886b275e529e44a59c54b933453d9bc902973178Romain Guy
41886b275e529e44a59c54b933453d9bc902973178Romain Guy    static class BitmapsView extends View {
42886b275e529e44a59c54b933453d9bc902973178Romain Guy        private Paint mBitmapPaint;
43886b275e529e44a59c54b933453d9bc902973178Romain Guy        private final Bitmap mBitmap1;
44886b275e529e44a59c54b933453d9bc902973178Romain Guy        private final float[] mVertices;
45886b275e529e44a59c54b933453d9bc902973178Romain Guy
46886b275e529e44a59c54b933453d9bc902973178Romain Guy        BitmapsView(Context c) {
47886b275e529e44a59c54b933453d9bc902973178Romain Guy            super(c);
48886b275e529e44a59c54b933453d9bc902973178Romain Guy
49886b275e529e44a59c54b933453d9bc902973178Romain Guy            Bitmap texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.spot_mask);
50886b275e529e44a59c54b933453d9bc902973178Romain Guy            mBitmap1 = Bitmap.createBitmap(texture.getWidth(), texture.getHeight(),
51886b275e529e44a59c54b933453d9bc902973178Romain Guy                    Bitmap.Config.ALPHA_8);
52886b275e529e44a59c54b933453d9bc902973178Romain Guy            Canvas canvas = new Canvas(mBitmap1);
53886b275e529e44a59c54b933453d9bc902973178Romain Guy            canvas.drawBitmap(texture, 0.0f, 0.0f, null);
54886b275e529e44a59c54b933453d9bc902973178Romain Guy
55886b275e529e44a59c54b933453d9bc902973178Romain Guy            texture = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
56886b275e529e44a59c54b933453d9bc902973178Romain Guy            BitmapShader shader = new BitmapShader(texture,
57886b275e529e44a59c54b933453d9bc902973178Romain Guy                    Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
58886b275e529e44a59c54b933453d9bc902973178Romain Guy
59886b275e529e44a59c54b933453d9bc902973178Romain Guy            final float width = texture.getWidth() / 3.0f;
60886b275e529e44a59c54b933453d9bc902973178Romain Guy            final float height = texture.getHeight() / 3.0f;
61886b275e529e44a59c54b933453d9bc902973178Romain Guy
62886b275e529e44a59c54b933453d9bc902973178Romain Guy            mVertices = new float[] {
63886b275e529e44a59c54b933453d9bc902973178Romain Guy                    0.0f, 0.0f, width, 0.0f, width * 2, 0.0f, width * 3, 0.0f,
64886b275e529e44a59c54b933453d9bc902973178Romain Guy                    0.0f, height, width, height, width * 2, height, width * 4, height,
65886b275e529e44a59c54b933453d9bc902973178Romain Guy                    0.0f, height * 2, width, height * 2, width * 2, height * 2, width * 3, height * 2,
66886b275e529e44a59c54b933453d9bc902973178Romain Guy                    0.0f, height * 4, width, height * 4, width * 2, height * 4, width * 4, height * 4,
67886b275e529e44a59c54b933453d9bc902973178Romain Guy            };
68886b275e529e44a59c54b933453d9bc902973178Romain Guy
69886b275e529e44a59c54b933453d9bc902973178Romain Guy            mBitmapPaint = new Paint();
70886b275e529e44a59c54b933453d9bc902973178Romain Guy            mBitmapPaint.setFilterBitmap(true);
71886b275e529e44a59c54b933453d9bc902973178Romain Guy            mBitmapPaint.setShader(shader);
72886b275e529e44a59c54b933453d9bc902973178Romain Guy        }
73886b275e529e44a59c54b933453d9bc902973178Romain Guy
74886b275e529e44a59c54b933453d9bc902973178Romain Guy        @Override
75886b275e529e44a59c54b933453d9bc902973178Romain Guy        protected void onDraw(Canvas canvas) {
76886b275e529e44a59c54b933453d9bc902973178Romain Guy            super.onDraw(canvas);
77886b275e529e44a59c54b933453d9bc902973178Romain Guy
78886b275e529e44a59c54b933453d9bc902973178Romain Guy            canvas.drawColor(0xffffffff);
79886b275e529e44a59c54b933453d9bc902973178Romain Guy            canvas.drawBitmap(mBitmap1, 0.0f, 0.0f, mBitmapPaint);
80886b275e529e44a59c54b933453d9bc902973178Romain Guy
81886b275e529e44a59c54b933453d9bc902973178Romain Guy            Matrix matrix = new Matrix();
82886b275e529e44a59c54b933453d9bc902973178Romain Guy            matrix.setScale(2.0f, 2.0f);
83886b275e529e44a59c54b933453d9bc902973178Romain Guy            matrix.postTranslate(0.0f, mBitmap1.getHeight());
84886b275e529e44a59c54b933453d9bc902973178Romain Guy            canvas.drawBitmap(mBitmap1, matrix, mBitmapPaint);
85886b275e529e44a59c54b933453d9bc902973178Romain Guy
86886b275e529e44a59c54b933453d9bc902973178Romain Guy            Rect src = new Rect(0, 0, mBitmap1.getWidth() / 2, mBitmap1.getHeight() / 2);
87886b275e529e44a59c54b933453d9bc902973178Romain Guy            Rect dst = new Rect(0, mBitmap1.getHeight() * 3, mBitmap1.getWidth(),
88886b275e529e44a59c54b933453d9bc902973178Romain Guy                    mBitmap1.getHeight() * 4);
89886b275e529e44a59c54b933453d9bc902973178Romain Guy            canvas.drawBitmap(mBitmap1, src, dst, mBitmapPaint);
90886b275e529e44a59c54b933453d9bc902973178Romain Guy
91886b275e529e44a59c54b933453d9bc902973178Romain Guy            canvas.translate(0.0f, mBitmap1.getHeight() * 4);
92886b275e529e44a59c54b933453d9bc902973178Romain Guy            canvas.drawBitmapMesh(mBitmap1, 3, 3, mVertices, 0, null, 0, mBitmapPaint);
93886b275e529e44a59c54b933453d9bc902973178Romain Guy
94886b275e529e44a59c54b933453d9bc902973178Romain Guy            invalidate();
95886b275e529e44a59c54b933453d9bc902973178Romain Guy        }
96886b275e529e44a59c54b933453d9bc902973178Romain Guy    }
97886b275e529e44a59c54b933453d9bc902973178Romain Guy}
98