Cdacians

Cdacians
Cdacians

Tuesday 7 August 2012

Create Options Menu with SurfaceView


Create Options Menu with SurfaceView

To create Options Menu on SurfaceView, simple override onCreateOptionsMenu() to add menu options, and override onOptionsItemSelected() to handle the event.

Create Options Menu with SurfaceView

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package com.TestSurefaceView;
 
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
 
public class TestSurefaceView extends Activity {
  
 MySurfaceView mySurfaceView;
  
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mySurfaceView = new MySurfaceView(this);
        setContentView(mySurfaceView);
    }
     
    @Override
 protected void onResume() {
  // TODO Auto-generated method stub
  super.onResume();
  mySurfaceView.onResumeMySurfaceView();
 }
 
 @Override
 protected void onPause() {
  // TODO Auto-generated method stub
  super.onPause();
  mySurfaceView.onPauseMySurfaceView();
 }
 
 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // TODO Auto-generated method stub
  menu.add(0, 0, 0, "Do Something");
  menu.add(0, 1, 1, "Do Something Else");
  return true;
 }
 
 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  // TODO Auto-generated method stub
  switch(item.getItemId()){
  case 0:
   Toast.makeText(TestSurefaceView.this,
     "Do Something",
     Toast.LENGTH_LONG).show();
   break;
  case 1:
   Toast.makeText(TestSurefaceView.this,
     "Do Something Else",
     Toast.LENGTH_LONG).show();
   break;
  }
  return true;
 }
 
 class MySurfaceView extends SurfaceView implements Runnable{
   
  //In this test, handle maximum of 2 pointer
  final int MAX_POINT_CNT = 2;
   
  private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  float[] x = new float[MAX_POINT_CNT];
  float[] y = new float[MAX_POINT_CNT];
  boolean[] isTouch = new boolean[MAX_POINT_CNT];
   
  float[] x_last = new float[MAX_POINT_CNT];
  float[] y_last = new float[MAX_POINT_CNT];
  boolean[] isTouch_last = new boolean[MAX_POINT_CNT];
 
      
     Thread thread = null;
     SurfaceHolder surfaceHolder;
     volatile boolean running = false;
      
     volatile boolean touched = false;
     volatile float touched_x, touched_y;
 
  public MySurfaceView(Context context) {
   super(context);
   // TODO Auto-generated constructor stub
   surfaceHolder = getHolder();
 
  }
   
  public void onResumeMySurfaceView(){
   running = true;
   thread = new Thread(this);
   thread.start();
  }
   
  public void onPauseMySurfaceView(){
   boolean retry = true;
   running = false;
   while(retry){
    try {
     thread.join();
     retry = false;
    } catch (InterruptedException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
 
  @Override
  public void run() {
   // TODO Auto-generated method stub
   while(running){
    if(surfaceHolder.getSurface().isValid()){
     Canvas canvas = surfaceHolder.lockCanvas();
     //... actual drawing on canvas
 
     paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(1);
      
     if(isTouch[0]){
      if(isTouch_last[0]){
       paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeWidth(5);
          paint.setColor(Color.RED);
          canvas.drawLine(x_last[0], y_last[0], x[0], y[0], paint);
      }
     }
     if(isTouch[1]){
      if(isTouch_last[1]){
       paint.setStyle(Paint.Style.STROKE);
          paint.setStrokeWidth(5);
          paint.setColor(Color.BLUE);
          canvas.drawLine(x_last[1], y_last[1], x[1], y[1], paint);
      }
     }
      
     surfaceHolder.unlockCanvasAndPost(canvas);
    }
   }
  }
 
  @Override
  public boolean onTouchEvent(MotionEvent motionEvent) {
   int pointerIndex = ((motionEvent.getAction() & MotionEvent.ACTION_POINTER_ID_MASK)
     >> MotionEvent.ACTION_POINTER_ID_SHIFT);
   int pointerId = motionEvent.getPointerId(pointerIndex);
   int action = (motionEvent.getAction() & MotionEvent.ACTION_MASK);
   int pointCnt = motionEvent.getPointerCount();
    
   if (pointCnt <= MAX_POINT_CNT){
    if (pointerIndex <= MAX_POINT_CNT - 1){
 
     for (int i = 0; i < pointCnt; i++) {
      int id = motionEvent.getPointerId(i);
      x_last[id] = x[id];
      y_last[id] = y[id];
      isTouch_last[id] = isTouch[id];
      x[id] = motionEvent.getX(i);
      y[id] = motionEvent.getY(i);
      }
      
     switch (action){
     case MotionEvent.ACTION_DOWN:
      isTouch[pointerId] = true;
      break;
     case MotionEvent.ACTION_POINTER_DOWN:
      isTouch[pointerId] = true;
      break;
     case MotionEvent.ACTION_MOVE:
      isTouch[pointerId] = true;
      break;
     case MotionEvent.ACTION_UP:
      isTouch[pointerId] = false;
      isTouch_last[pointerId] = false;
      break;
     case MotionEvent.ACTION_POINTER_UP:
      isTouch[pointerId] = false;
      isTouch_last[pointerId] = false;
      break;
     case MotionEvent.ACTION_CANCEL:
      isTouch[pointerId] = false;
      isTouch_last[pointerId] = false;
      break;
     default:
      isTouch[pointerId] = false;
      isTouch_last[pointerId] = false;
     }
    }
   }
    
   return true;
  }
      
    }
}


akm
www.cdacians.com

No comments:

Post a Comment