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.Camera;
24import android.graphics.Canvas;
25import android.graphics.Matrix;
26import android.os.Bundle;
27import android.view.View;
28
29@SuppressWarnings({"UnusedDeclaration"})
30public class Transform3dActivity extends Activity {
31    @Override
32    protected void onCreate(Bundle savedInstanceState) {
33        super.onCreate(savedInstanceState);
34        final Transform3dView view = new Transform3dView(this);
35        setContentView(view);
36    }
37
38    static class Transform3dView extends View {
39        private final Bitmap mBitmap1;
40        private Camera mCamera;
41        private Matrix mMatrix;
42
43        Transform3dView(Context c) {
44            super(c);
45
46            mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
47            mCamera = new Camera();
48            mMatrix = new Matrix();
49        }
50
51        @Override
52        protected void onDraw(Canvas canvas) {
53            super.onDraw(canvas);
54
55            canvas.drawARGB(255, 255, 255, 255);
56
57            final float centerX = getWidth() / 2.0f - mBitmap1.getWidth() / 2.0f;
58            final float centerY = getHeight() / 2.0f - mBitmap1.getHeight() / 2.0f;
59            final Camera camera = mCamera;
60
61            final Matrix matrix = mMatrix;
62
63            rotate(centerX, centerY, camera, matrix, 32.0f);
64            drawBitmap(canvas, centerX, centerY, 0.0f, matrix);
65
66            rotate(centerX, centerY, camera, matrix, 12.0f);
67            drawBitmap(canvas, centerX, centerY, -mBitmap1.getWidth(), matrix);
68
69            rotate(centerX, centerY, camera, matrix, 52.0f);
70            drawBitmap(canvas, centerX, centerY, mBitmap1.getWidth(), matrix);
71
72            rotate(centerX, centerY, camera, matrix, 122.0f);
73            drawBitmap(canvas, centerX, centerY, mBitmap1.getWidth() * 2.0f, matrix);
74
75        }
76
77        private void drawBitmap(Canvas canvas, float centerX, float centerY, float offset,
78                Matrix matrix) {
79            canvas.save();
80            canvas.translate(offset, 0.0f);
81            canvas.concat(matrix);
82            canvas.drawBitmap(mBitmap1, centerX, centerY, null);
83            canvas.restore();
84        }
85
86        private void rotate(float centerX, float centerY, Camera camera,
87                Matrix matrix, float angle) {
88            camera.save();
89            camera.rotateY(angle);
90            camera.getMatrix(matrix);
91            camera.restore();
92
93            matrix.preTranslate(-centerX, -centerY);
94            matrix.postTranslate(centerX, centerY);
95        }
96    }
97}
98