• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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.example.android.apis.graphics;
18 
19 import com.example.android.apis.R;
20 
21 import android.content.Context;
22 import android.graphics.*;
23 import android.os.Bundle;
24 import android.view.*;
25 
26 public class Vertices extends GraphicsActivity {
27 
28     @Override
onCreate(Bundle savedInstanceState)29     protected void onCreate(Bundle savedInstanceState) {
30         super.onCreate(savedInstanceState);
31         setContentView(new SampleView(this));
32     }
33 
34     private static class SampleView extends View {
35         private final Paint mPaint = new Paint();
36         private final float[] mVerts = new float[10];
37         private final float[] mTexs = new float[10];
38         private final short[] mIndices = { 0, 1, 2, 3, 4, 1 };
39 
40         private final Matrix mMatrix = new Matrix();
41         private final Matrix mInverse = new Matrix();
42 
setXY(float[] array, int index, float x, float y)43         private static void setXY(float[] array, int index, float x, float y) {
44             array[index*2 + 0] = x;
45             array[index*2 + 1] = y;
46         }
47 
SampleView(Context context)48         public SampleView(Context context) {
49             super(context);
50             setFocusable(true);
51 
52             Bitmap bm = BitmapFactory.decodeResource(getResources(),
53                                                      R.drawable.beach);
54             Shader s = new BitmapShader(bm, Shader.TileMode.CLAMP,
55                                         Shader.TileMode.CLAMP);
56             mPaint.setShader(s);
57 
58             float w = bm.getWidth();
59             float h = bm.getHeight();
60             // construct our mesh
61             setXY(mTexs, 0, w/2, h/2);
62             setXY(mTexs, 1, 0, 0);
63             setXY(mTexs, 2, w, 0);
64             setXY(mTexs, 3, w, h);
65             setXY(mTexs, 4, 0, h);
66 
67             setXY(mVerts, 0, w/2, h/2);
68             setXY(mVerts, 1, 0, 0);
69             setXY(mVerts, 2, w, 0);
70             setXY(mVerts, 3, w, h);
71             setXY(mVerts, 4, 0, h);
72 
73             mMatrix.setScale(0.8f, 0.8f);
74             mMatrix.preTranslate(20, 20);
75             mMatrix.invert(mInverse);
76         }
77 
onDraw(Canvas canvas)78         @Override protected void onDraw(Canvas canvas) {
79             canvas.drawColor(0xFFCCCCCC);
80             canvas.save();
81             canvas.concat(mMatrix);
82 
83             canvas.drawVertices(Canvas.VertexMode.TRIANGLE_FAN, 10, mVerts, 0,
84                                 mTexs, 0, null, 0, null, 0, 0, mPaint);
85 
86             canvas.translate(0, 240);
87             canvas.drawVertices(Canvas.VertexMode.TRIANGLE_FAN, 10, mVerts, 0,
88                                 mTexs, 0, null, 0, mIndices, 0, 6, mPaint);
89 
90             canvas.restore();
91         }
92 
onTouchEvent(MotionEvent event)93         @Override public boolean onTouchEvent(MotionEvent event) {
94             float[] pt = { event.getX(), event.getY() };
95             mInverse.mapPoints(pt);
96             setXY(mVerts, 0, pt[0], pt[1]);
97             invalidate();
98             return true;
99         }
100 
101     }
102 }
103 
104