1

I have got no answer for this question on the internet , I'm trying to paint with different colors which the user can choose from , the problem is this drawview is not drawing anything and i don't know how o ad an on touch eraser to erase only the touched point here's the 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;
            }
        }
    }

it will be appreciated if anyone could provide an answer this is my log :

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
rgl
  • 45
  • 8
  • 1
    When I did something similar I liked [this file](https://github.com/SueSmith/android-drawing-app/blob/master/src/com/example/drawingfun/DrawingView.java) in [this repo](https://github.com/SueSmith/android-drawing-app). It has drawing/erasing just like you're trying to do. It's also part of a tutorial (links in the README) if that helps. – Brett Beatty Jun 14 '17 at 18:19
  • @BrettBeatty if i want on an imageview over a bitmap image this can be used ?? – rgl Jun 14 '17 at 18:21
  • Sorry, I don't understand the question. You can then get the bitmap from the view (see the canvasBitmap property) when the user is done drawing if that's what you're asking. – Brett Beatty Jun 14 '17 at 18:27
  • no , my question is i have image on the imageview already , will this allow me to paint over this image ? – rgl Jun 14 '17 at 18:29
  • Yeah, I would imagine instead of creating a new bitmap you could use the existing one (or clone it) [when you create the canvas](https://github.com/SueSmith/android-drawing-app/blob/master/src/com/example/drawingfun/DrawingView.java#L72) – Brett Beatty Jun 14 '17 at 18:35
  • thank you i will try this @BrettBeatty – rgl Jun 14 '17 at 18:52
  • i used this example but th image is remove and when the touch event is up the paint is erased – rgl Jun 14 '17 at 19:51

1 Answers1

0

You never seem to actually set the OnTouchListener on your Drawview.

So, here you go : Draw images on evey touch co-ordinats Of Imageview

S-L-R
  • 46
  • 4
  • I have added on touch listener on the main activity , and i don't want to draw point i want to draw paths on touch and erase also on touch – rgl Jun 14 '17 at 18:09
  • Could you add Logs to the events and tell us if your onTouchEvent is actually being called? – S-L-R Jun 14 '17 at 18:23
  • Yes. But please add some logs to the code so that you can see if the "onTouchEvent" is ever called. You could very well be in a situation where the action is never actually caught by the right object. – S-L-R Jun 14 '17 at 18:31
  • can you only tell how to erase on touch ?? for this class https://stackoverflow.com/questions/44415757/only-one-color-is-displayed-in-time – rgl Jun 14 '17 at 20:45
  • It all depends on how you handle the drawing logic and drawn shapes, but you basically want to know if you either have to draw a color (if there's no color present on the touch event) or if you want to draw the background color (if there's already a color present on the touch event position) – S-L-R Jun 15 '17 at 12:34
  • I want to delete the painted points not to change their colors – rgl Jun 16 '17 at 13:00