ogiso雪菜 2018-05-04 05:45 采纳率: 37.5%
浏览 1334
已结题

请问我如何把edittext的文字通过onDraw放在画板上

我想做一个画板,现在想实现插入文字功能,目前点击屏幕时能弹出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);
        }
    }
  • 写回答

4条回答 默认 最新

  • dabocaiqq 2018-05-04 10:49
    关注

    用drawText()可以实现
    canvas.drawText()

    https://blog.csdn.net/harvic880925/article/details/50423762

    评论

报告相同问题?

悬赏问题

  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决