当我触摸画布上的坐标时,我正在尝试启动自定义弹出窗口。我正在绘制一个场地的图像,我需要弹出一个出现在我触摸场地的坐标周围。
我无法从DrawingView类(我在图像上绘制)中启动弹出窗口,因为我没有弹出功能的Activity。如果我尝试从必要的活动(FieldPage)启动弹出窗口,则DrawingView触摸事件将覆盖FieldPage触摸事件。在这种情况下,每当我触摸按钮后面的灰色背景时,我都可以创建弹出窗口,但不能在我正在使用的绿色场图像上创建弹出窗口。我会附上一张照片,但没有声誉。
以下是FieldPage的代码:
public class FieldPage extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.field_page);
}
// The method that displays the popup.
private void showPopup(final Activity context, int xPos, int yPos) { //Point p) { //
int popupWidth = 800;
int popupHeight = 800;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popout_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 0; //30;
int OFFSET_Y = 0; //30;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y); //xPos + OFFSET_X, yPos + OFFSET_Y);
// Getting a reference to Close button, and close the popup when clicked.
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int touchX = (int) event.getX();
int touchY = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
showPopup(FieldPage.this, touchX, touchY);
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
break;
default:
return false;
}
return true;
}
}
//Popup function
private void showPopup(final Activity context, int xPos, int yPos) {
int popupWidth = 800;
int popupHeight = 800;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popout_layout, viewGroup);
// Creating the PopupWindow
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x, p.y);
// Getting a reference to Close button, and close the popup when clicked.
Button close = (Button) layout.findViewById(R.id.close);
close.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
popup.dismiss();
}
});
}
DrawingView代码:
public class DrawingView extends View {
private Path drawPath;
private Paint drawPaint, canvasPaint;
private int paintColor = 0xFFFF0000;
private Canvas drawCanvas;
private Bitmap canvasBitmap, mutableBitmap;
public DrawingView (Context context, AttributeSet attrs) {
super(context, attrs);
setupDrawing();
}
private void setupDrawing() {
//Sets up various paths and my canvas
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
canvasBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.field);
mutableBitmap = canvasBitmap.copy(Bitmap.Config.ARGB_8888, true);
drawCanvas = new Canvas(mutableBitmap);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.drawBitmap(mutableBitmap, 0, 0, canvasPaint);
canvas.drawPath(drawPath, drawPaint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int touchX = (int) event.getX();
int touchY = (int) event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Draw Stuff
//This is where I tried launching the popup
break;
case MotionEvent.ACTION_MOVE:
break;
case MotionEvent.ACTION_UP:
//Stuff
break;
default:
return false;
}
invalidate();
return true;
}
}