| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.android.test.hwui; |
| 18 |
|
| 19 |
import android.app.Activity; |
| 20 |
import android.content.Context; |
| 21 |
import android.graphics.Bitmap; |
| 22 |
import android.graphics.BitmapFactory; |
| 23 |
import android.graphics.Camera; |
| 24 |
import android.graphics.Canvas; |
| 25 |
import android.graphics.Matrix; |
| 26 |
import android.os.Bundle; |
| 27 |
import android.view.View; |
| 28 |
|
| 29 |
@SuppressWarnings({"UnusedDeclaration"}) |
| 30 |
public 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 |
} |