我对代码的逻辑感到有些困惑。我正在做的是使用showDialog(我知道折旧)向用户显示一个对话框,然后根据他们选择的内容,在他们触摸的地方绘制一个图标。
我的问题是:“当用户选择他们的选择时,如何让程序”暂停“,以便下面的代码不会立即被调用,并且在他们按下确定后没有完成绘图。< / p> 编辑:我忘了提 - 这只是在主UI线程上,我没有很多处理线程的经验但在程序中有几个AsyncTasks。是否有必要制作一个新线程来处理触摸和绘图功能?
private void touch_Start(float x, float y) { // on finger touchdown
// check touch mode
if (addObjectMode == true) {
//drawingAreaView method to add EditText box at position x,y
iX = (int) (Math.round(x));
iY = (int) (Math.round(y));
mX = (int) (Math.round(x));
mY = (int) (Math.round(y));
myRect.set(iX, iY, mX, mY);
} else if (addApplianceMode == true) {
// code to draw an appliance icon at mX, mY (with offset so icon is centered)
makeToast("Start drawing appliance at x, y");
}
}
private void touch_Move(float x, float y) { // on finger movement
float dX = Math.abs(x - mX); // get difference between x and my X
float dY = Math.abs(y - mY);
if (dX >= TOUCH_TOLERANCE || dY >= TOUCH_TOLERANCE) { // if coordinates are outside screen? if touching hard enough?
mX = (int) (Math.round(x));
mY = (int) (Math.round(y));
if (addObjectMode == true) {
myRect.set(iX, iY, mX, mY);
} else if (addApplianceMode == true) { // whatever you do here gets repeated LOADS if they move (e.g. per pixel)
makeToast("User moved appliance");// just dont set myRect
}
}
}
@SuppressWarnings("deprecation")
private void touch_Up() { // on finger release
if (addObjectMode == true) {
myRect.set(iX, iY, mX, mY);
myCanvas.drawRect(iX, iY, mX, mY, myPaint);
makeToast("touchUp, so call the dialogs!");
dialogStarter();
} else if (addApplianceMode == true) {
// call add appliance dialog
makeToast("Call Appliance Dialog");
showDialog(DIALOG_APPLIANCE_CLASSIFICATION);
//myCanvas.drawBitmap(bmp, mX, mY, myBitmapPaint);
}
}
它绘制的位图失败了,因为我有代码根据用户选择的内容选择bmp来绘制但是所有内容都以错误的顺序调用。
这是我的对话框onClickListener代码:
.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
makeToast("User Clicked Ok");
if (isLamp == true) {
Resources res = getResources();
bmp = BitmapFactory.decodeResource(res,
R.drawable.lamp);
// code to draw lamp to canvas here
HomerView.drawApplianceIcon();
makeToast("Lamp Drawn To Canvas");
}
}
})
drawApplianceIcon()出错,因为即使我尝试使用HomerView.drawApplianceIcon(),也无法调用自定义View类中包含的方法。
它返回错误:“无法从DrawNewPlans.HomerView类型中对静态方法drawApplianceIcon()进行静态引用”
继承层次结构:
public class DrawNewPlans extends Activity implements ColorPickerDialog.OnColorChangedListener {
protected void onCreate(Bundle savedInstanceState) { // on create method - initialises the activity
protected Dialog onCreateDialog(int id) { // dialog creator code
public void colorChanged(int color) { // implemented method for when color is changed
public class HomerView extends View { // the custom View for drawing on
protected void onDraw(Canvas canvas) { // method used when we want to draw something to our canvas
protected void onSizeChanged(int w, int h, int oldw, int oldh) { // if screen size changes, alter the bitmap size
private void touch_Start(float x, float y) { // on finger touchdown
private void touch_Move(float x, float y) { // on finger movement
private void touch_Up() { // on finger release
public boolean onTouchEvent(MotionEvent event) { // on any touch event
public void drawApplianceIcon() { // the code im working on
public boolean onCreateOptionsMenu(Menu menu) { // on menu creation
public void dialogStarter() { // starts the dialogs for gathering user submitted information
public void checkStoredObjects() { // checks what is stored in the object List
public void resetValues() { // resets values of object to null, for reuse
public boolean onPrepareOptionsMenu(Menu menu) { // method which is needed to inflate menu
public boolean onOptionsItemSelected(MenuItem item) { // method containing code for what to do when a menu item is selected
public void makeToast(String message) { // method to display a pop up for a short duration
public void makeLongToast(String message) { // method to display a pop up for a long duration
public void redrawMode() { // reenables drawing mode by resetting myPaint
public class getLocations extends AsyncTask<Void, Integer, JSONArray> { // aSyncTask class for getting Homer Locations
编辑:不幸的是,我接受的答案是没有正确调用我想要的代码片段! :(
以下是修订后的代码:
DrawNewPlans.this.myHomer.drawApplianceIcon(200, 200); // should call a method contained in my custom view which draws a bmp to the coordinates provided.
方法代码:
public void drawApplianceIcon(float x, float y) {
myCanvas.drawBitmap(bmp, x-50, y-50, myBitmapPaint);
}
当在自定义视图类中调用touch_start()时调用此方法,但在通过onCreateDialog方法调用时调用则不行。很抱歉是一个痛苦,并有一个长期的问题 - 有没有更好的方式来讨论这个?只是觉得开始一个新问题意味着我将不得不重复重复该程序的上下文。