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