1

我在互联网上没有得到这个问题的答案,我正在尝试用用户可以选择的不同颜色进行绘制,问题是这个 drawview 没有绘制任何东西,我不知道如何使用触摸橡皮擦只擦除触摸点是 customview :

public class DrawView extends android.support.v7.widget.AppCompatImageView {
      private ArrayList<ColouredPaths> mTouches;
        // Current used colour
        private int mCurrColour;
        private Paint mPaint;
        private boolean erase=false;
        ColouredPaths mPath;
        Canvas mCanvas;
        public void setErase(boolean isErase){
    //set erase true or false
            erase=isErase;
        }
        public void setColor(int colour) {
            mCurrColour = colour;
        }

        public DrawView(Context context) {
            super(context);
            init();
        }

        // XML constructor
        public DrawView(Context context, AttributeSet attrs) {
            super(context, attrs);
            init(); }
        private void init() {
            mTouches = new ArrayList<>();
            mPaint = new Paint();
            mPaint.setColor(mCurrColour);
            mPaint.setAntiAlias(true);
            mPaint.setStrokeWidth(5);
            mPaint.setStyle(Paint.Style.FILL);
            mPaint.setStrokeJoin(Paint.Join.ROUND);
            mPaint.setStrokeCap(Paint.Cap.ROUND);
        }
        private float mX, mY;
        private static final float TOUCH_TOLERANCE = 4;

        private void touch_start(float x, float y) {
            mPath.reset();
            mPath.moveTo(x, y);
            mX = x;
            mY = y;
        }

        private void touch_move(float x, float y) {
            float dx = Math.abs(x - mX);
            float dy = Math.abs(y - mY);
            if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
                mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
                mX = x;
                mY = y;
            }
        }
        private void touch_up() {
            mPath.lineTo(mX, mY);
            mPath.reset();
            // commit the path to our offscreen
            mTouches.add(mPath);
            // kill this so we don't double draw
            mPath.reset();
        }

        @Override
        public boolean onTouchEvent(MotionEvent event) {
            float x = event.getX();
            float y = event.getY();
            mPath=new ColouredPaths(mCurrColour);
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    touch_start(x, y);

                    break;
                case MotionEvent.ACTION_MOVE:
                    touch_move(x, y);

                    break;
                case MotionEvent.ACTION_UP:
                    touch_up();

                    break;
            }
            return super.onTouchEvent(event);
        }

        @Override
        protected void onDraw(Canvas c) {
            // Let the image be drawn first
            super.onDraw(c);
            // Draw your custom points here
                    for (ColouredPaths p : mTouches) {

                mPaint.setColor(p.colour);
                c.drawPath(p,mPaint);}}


        /**
         * Class to store the coordinate and the colour of the point.
         */
        private class ColouredPaths extends Path{

            int colour;

            public ColouredPaths( int colour) {

                this.colour = colour;
            }
        }
    }

如果有人能提供答案,将不胜感激,这是我的日志:

06-14 21:26:03.926 907-1410/? E/hwintelligencewifi: updataApInfo cellid =4190217533
06-14 21:26:03.926 907-1410/? E/hwintelligencewifi: addCurrentApInfo cellid =4190217533
06-14 21:26:03.928 907-1410/? E/hwintelligencewifi: addCurrentApInfo info is already there
06-14 21:26:03.932 907-1410/? E/hwintelligencewifi: inlineAddCurrentApInfo mInfos.size()=31
4

1 回答 1

0

您似乎从未真正在 Drawview 上设置 OnTouchListener。

所以,给你:在 Imageview 的每个触摸坐标上绘制图像

于 2017-06-14T18:07:20.017 回答