ディスプレイのどこにでもタッチしたときにダイアログが表示されるようにしたい。アクティビティ、ビュー、レンダラークラスがあります。私はViewクラスでコードを書きました:
public boolean onTouchEvent(final MotionEvent event) {
queueEvent(new Runnable() {....}}
コードを投稿しました。(アクティビティと表示のみ)
public class MapEditorActivity extends Activity {
private GLSurfaceView mGLSurfaceView;
Region dialogRegion;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Create our Preview view and set it as the content of our
// Activity
mGLSurfaceView = new MapEditorView(this);
setContentView(mGLSurfaceView);
}
@Override
protected void onPause() {
super.onPause();
mGLSurfaceView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
}
}
そして私のViewクラス:
public class MapEditorView extends GLSurfaceView {
private static final String LOG_TAG = "de.rwth.arlab.colofields.ui.MapEditorView";
MapEditorRenderer editMapRenderer;
// we have a maximum of 5 pointer
private int[] coordsX = new int[5];
private int[] coordsY = new int[5];
// we have to save first touch for motion
private int[] coordsXFirst = new int[5];
private int[] coordsYFirst = new int[5];
// offsets for view
private float offX;
private float offY;
private float offZ;
Context context;
boolean zooming;
public MapEditorView(Context context) {
super(context);
editMapRenderer = new MapEditorRenderer(context);
setRenderer(editMapRenderer);
// initialize as not present
Arrays.fill(coordsX, Integer.MIN_VALUE);
Arrays.fill(coordsY, Integer.MIN_VALUE);
zooming = false;
}
public boolean onTouchEvent(final MotionEvent event) {
queueEvent(new Runnable() {
// TODO: Speak about this thing, this functions need API8
@SuppressLint("NewApi")
public void run() {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
offX = editMapRenderer.getxView();
offY = editMapRenderer.getyView();
coordsXFirst[0] = (int) event.getX(0);
coordsYFirst[0] = (int) event.getY(0);
startDialog(null);
break;
case MotionEvent.ACTION_POINTER_DOWN:
offZ = editMapRenderer.getzView();
coordsXFirst[0] = (int) event.getX(0);
coordsYFirst[0] = (int) event.getY(0);
coordsXFirst[1] = (int) event.getX(1);
coordsYFirst[1] = (int) event.getY(1);
zooming=true;
break;
case MotionEvent.ACTION_UP:
zooming=false;
break;
default:
// track all touches
for (int i = 0; i < event.getPointerCount(); i++) {
int id = event.getPointerId(i);
// update all other pointer positions
coordsX[id] = (int) event.getX(i);
coordsY[id] = (int) event.getY(i);
}
// When only one finger
if (event.getPointerCount() == 1 && !zooming)
editMapRenderer.setPosition(offX
+ (coordsX[0] - coordsXFirst[0])*editMapRenderer.getzView()*-1 / 1000.0f, offY
+ (coordsYFirst[0] - coordsY[0])*editMapRenderer.getzView()*-1 / 1000.0f);
// When two fingers
float distanceOld = (float) FloatMath
.sqrt((coordsXFirst[1] - coordsXFirst[0])
* (coordsXFirst[1] - coordsXFirst[0])
+ (coordsYFirst[1] - coordsYFirst[0])
* (coordsYFirst[1] - coordsYFirst[0]));
float distanceNew = (float) FloatMath
.sqrt((coordsX[1] - coordsX[0])
* (coordsX[1] - coordsX[0])
+ (coordsY[1] - coordsY[0])
* (coordsY[1] - coordsY[0]));
float distanceMax = (float) FloatMath
.sqrt(getWidth()
* getWidth()
+ getHeight()
* getHeight());
if (event.getPointerCount() == 2) {
editMapRenderer.setZoom(offZ + (distanceNew
- distanceOld)/(distanceMax/10));
}
break;
}
}
public Dialog startDialog(Bundle savedInstanceState) {
final CharSequence[] items = new String[] { "#F5F6F6", "#FFC0CB", "#FF0000",
"#C00000","#62492F","#DB3C00", "#98E01C" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText((Context) getApplicationWindowToken(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
}
});
return true;
}
MapEditorRenderer mRenderer;
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater =((Activity)context).getMenuInflater();
inflater.inflate(R.layout.menu_save, menu);
return true;
}
}
私の問題はメソッドstartDialog()です。それは動作しません。エラーが発生します:
"The constructor AlertDialog.Builder(new Runnable(){}) is undefined"
どうすれば解決できますか?
わかりました、コードをもう一度投稿します:
>>
パブリッククラスMapEditorActivityはActivity{を拡張します
private GLSurfaceView mGLSurfaceView;
Region dialogRegion;
MapEditorRenderer editMapRenderer;
// we have a maximum of 5 pointer
private int[] coordsX = new int[5];
private int[] coordsY = new int[5];
// we have to save first touch for motion
private int[] coordsXFirst = new int[5];
private int[] coordsYFirst = new int[5];
// offsets for view
private float offX;
private float offY;
private float offZ;
Context context;
boolean zooming;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
// Create our Preview view and set it as the content of our
// Activity
mGLSurfaceView = new MapEditorView(this);
setContentView(mGLSurfaceView);
mGLSurfaceView.setOnTouchListener(MapEditorActivity.this);
}
@Override
protected void onPause() {
super.onPause();
mGLSurfaceView.onPause();
}
@Override
protected void onResume() {
super.onResume();
mGLSurfaceView.onResume();
}
public boolean onTouchEvent(final MotionEvent event) {
queueEvent(new Runnable() {
// TODO: Speak about this thing, this functions need API8
@SuppressLint("NewApi")
public void run() {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
offX = editMapRenderer.getxView();
offY = editMapRenderer.getyView();
coordsXFirst[0] = (int) event.getX(0);
coordsYFirst[0] = (int) event.getY(0);
startDialog(null);
break;
case MotionEvent.ACTION_POINTER_DOWN:
offZ = editMapRenderer.getzView();
coordsXFirst[0] = (int) event.getX(0);
coordsYFirst[0] = (int) event.getY(0);
coordsXFirst[1] = (int) event.getX(1);
coordsYFirst[1] = (int) event.getY(1);
zooming=true;
break;
case MotionEvent.ACTION_UP:
zooming=false;
break;
default:
// track all touches
for (int i = 0; i < event.getPointerCount(); i++) {
int id = event.getPointerId(i);
// update all other pointer positions
coordsX[id] = (int) event.getX(i);
coordsY[id] = (int) event.getY(i);
}
// When only one finger
if (event.getPointerCount() == 1 && !zooming)
editMapRenderer.setPosition(offX
+ (coordsX[0] - coordsXFirst[0])*editMapRenderer.getzView()*-1 / 1000.0f, offY
+ (coordsYFirst[0] - coordsY[0])*editMapRenderer.getzView()*-1 / 1000.0f);
// When two fingers
float distanceOld = (float) FloatMath
.sqrt((coordsXFirst[1] - coordsXFirst[0])
* (coordsXFirst[1] - coordsXFirst[0])
+ (coordsYFirst[1] - coordsYFirst[0])
* (coordsYFirst[1] - coordsYFirst[0]));
float distanceNew = (float) FloatMath
.sqrt((coordsX[1] - coordsX[0])
* (coordsX[1] - coordsX[0])
+ (coordsY[1] - coordsY[0])
* (coordsY[1] - coordsY[0]));
float distanceMax = (float) FloatMath
.sqrt(getWidth()
* getWidth()
+ getHeight()
* getHeight());
if (event.getPointerCount() == 2) {
editMapRenderer.setZoom(offZ + (distanceNew
- distanceOld)/(distanceMax/10));
}
break;
}
}
private int getHeight() {
// TODO Auto-generated method stub
return 0;
}
private int getWidth() {
// TODO Auto-generated method stub
return 0;
}
public Dialog startDialog(Bundle savedInstanceState) {
final CharSequence[] items = new String[] { "#F5F6F6", "#FFC0CB", "#FF0000",
"#C00000","#62492F","#DB3C00", "#98E01C" };
AlertDialog.Builder builder = new AlertDialog.Builder(MapEditorActivity.this);
builder.setTitle("Pick a color");
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
Toast.makeText((Context) getApplication(), items[item], Toast.LENGTH_SHORT).show();
}
});
AlertDialog alert = builder.create();
return alert;
}
});
return true;
}
private void queueEvent(Runnable runnable) {
// TODO Auto-generated method stub
}
MapEditorRenderer mRenderer;
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater =((Activity)context).getMenuInflater();
inflater.inflate(R.layout.menu_save, menu);
return true;
}
}