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 
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.Canvas;
24 import android.graphics.Paint;
25 import android.os.Bundle;
26 import android.view.View;
27 
28 @SuppressWarnings({"UnusedDeclaration"})
29 public class BitmapMeshActivity extends Activity {
30     @Override
onCreate(Bundle savedInstanceState)31     protected void onCreate(Bundle savedInstanceState) {
32         super.onCreate(savedInstanceState);
33         final BitmapMeshView view = new BitmapMeshView(this);
34         setContentView(view);
35     }
36 
37     static class BitmapMeshView extends View {
38         private Paint mBitmapPaint;
39         private final Bitmap mBitmap1;
40         private float[] mVertices;
41         private int[] mColors;
42 
BitmapMeshView(Context c)43         BitmapMeshView(Context c) {
44             super(c);
45 
46             mBitmap1 = BitmapFactory.decodeResource(c.getResources(), R.drawable.sunset1);
47 
48             final float width = mBitmap1.getWidth() / 3.0f;
49             final float height = mBitmap1.getHeight() / 3.0f;
50 
51             mVertices = new float[] {
52                 0.0f, 0.0f, width, 0.0f, width * 2, 0.0f, width * 3, 0.0f,
53                 0.0f, height, width, height, width * 2, height, width * 4, height,
54                 0.0f, height * 2, width, height * 2, width * 2, height * 2, width * 3, height * 2,
55                 0.0f, height * 4, width, height * 4, width * 2, height * 4, width * 4, height * 4,
56             };
57 
58             mColors = new int[] {
59                 0xffff0000, 0xff00ff00, 0xff0000ff, 0xffff0000,
60                 0xff0000ff, 0xffff0000, 0xff00ff00, 0xff00ff00,
61                 0xff00ff00, 0xff0000ff, 0xffff0000, 0xff00ff00,
62                 0x00ff0000, 0x0000ff00, 0x000000ff, 0x00ff0000,
63             };
64         }
65 
66         @Override
onDraw(Canvas canvas)67         protected void onDraw(Canvas canvas) {
68             super.onDraw(canvas);
69 
70             canvas.drawARGB(255, 255, 255, 255);
71 
72             canvas.translate(100, 100);
73             canvas.drawBitmapMesh(mBitmap1, 3, 3, mVertices, 0, null, 0, null);
74 
75             canvas.save();
76             canvas.translate(0, 400);
77             canvas.clipRect(0.0f, 0.0f, 80.0f, 80.0f);
78             canvas.drawBitmapMesh(mBitmap1, 3, 3, mVertices, 0, null, 0, null);
79             canvas.restore();
80 
81             canvas.translate(400, 0);
82             canvas.drawBitmapMesh(mBitmap1, 3, 3, mVertices, 0, mColors, 0, null);
83         }
84     }
85 }
86