我想做一个画板,现在想实现插入文字功能,目前点击屏幕时能弹出popupwindow,然后可以编辑EditText,请问如果在点击其他地方的时候让文字留在画板上
这是showTextPopup方法部分
public void showTextPopup(float preX,float preY){
int x = (int) preX;
int y = (int) preY;
dp = new DrawPath();
dp.mOffX = preX;
dp.mOffY = preY;
dp.paint = mpaint;
EditText mEditText = new EditText(getContext());
mEditText.setText("输入文字");
PopupWindow mTextPopup = new PopupWindow(mEditText, WindowManager.LayoutParams.WRAP_CONTENT,WindowManager.LayoutParams.WRAP_CONTENT,true);
mEditText.setFocusable(true);
mEditText.setFocusableInTouchMode(true);
//让popup显示在软键盘上面
mTextPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
mEditText.requestFocus();
mTextPopup.setOutsideTouchable(true);
mTextPopup.setBackgroundDrawable(new BitmapDrawable());
//自动弹出软键盘,会导致布局变化,重测量、绘制
InputMethodManager imm = (InputMethodManager) getContext().getSystemService(Service.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(0,InputMethodManager.HIDE_NOT_ALWAYS);
mTextPopup.showAtLocation(this, Gravity.TOP | Gravity.LEFT, x, y);
invalidate();
}
这是DrawPath类
static class DrawPath{
public Path path;//路径
public Paint paint;//画笔
public Editable mText;//文本
//public int mWidth;//宽度
public float mOffX;//X轴偏移
public float mOffY;//Y轴偏移
}
点击事件
public boolean onTouchEvent(MotionEvent event){
//获取拖动事件的发生位置
float x = event.getX();
float y = event.getY();
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//从前一个点绘制到当前点之后,把当前点定义成下次绘制的一个点
if(currentStyle==3){
showTextPopup(x,y);
}
mpath = new Path();
//每一次记录的路径对象是不一样的
dp = new DrawPath();
dp.path = mpath;
dp.paint = mpaint;
mpath.moveTo(x,y);
preX = x;
preY = y;
invalidate();
break;
case MotionEvent.ACTION_MOVE:
mpath.quadTo(preX,preY,x,y);
preX = x;
preY = y;
break;
case MotionEvent.ACTION_UP:
mpath.lineTo(preX,preY);
cachecanvas.drawPath(mpath,mpaint);
//将一条完整的路径保存下来
savePath.add(dp);
mpath = null;//重新置空
break;
}
invalidate();
//返回true表明处理方法已经处理该事件
return true;
}
onDraw方法
public void onDraw(Canvas canvas){
//Paint bmpPaint = new Paint();
//将cacheBitmap绘制到该View组件上
canvas.drawBitmap(cacheBitmap,0,0,mBitmapPaint);//显示旧的画布
//沿着path绘制
if(mpath != null) {
canvas.drawPath(mpath, mpaint);
}
}