Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Mobile Development
  3. Android
  4. Custom drawing view with tools

Custom drawing view with tools

Scheduled Pinned Locked Moved Android
graphicsquestioncomdesigndata-structures
4 Posts 3 Posters 13 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cristo1985
    wrote on last edited by
    #1

    Hi, i need a little help. I'm creating my custom Drawing View. The functionality I am trying to achieve is: Arrow icon => touch one point and drag until other point draws line-arrow Text icon => touch in one point of screen and enter a text (open keyboard and write and close it) Eraser icon => erase canvas drawn These is the design of the menu I've made: TOOLS MENU And all interface is here: INTERFACE How can I edit my custom drawing view for make tools specified above? Whick portion of code will I put inside CustomDrawingView class? Here my DrawingView class:

    public class DrawingView extends View
    {
    private Path mDrawPath;
    private Paint mBackgroundPaint;
    private Paint mDrawPaint;
    private Canvas mDrawCanvas;
    private Bitmap mCanvasBitmap;

    private ArrayList mPaths = new ArrayList<>();
    private ArrayList mPaints = new ArrayList<>();
    private ArrayList mUndonePaths = new ArrayList<>();
    private ArrayList mUndonePaints = new ArrayList<>();
    
    // Set default values
    private int mBackgroundColor = 0x00FFFFFF;
    private int mPaintColor = 0x00660000;
    private int mStrokeWidth = 10;
    
    public DrawingView(Context context, AttributeSet attrs)
    {
        super(context, attrs);
        init();
    }
    
    private void init()
    {
        mDrawPath = new Path();
        mBackgroundPaint = new Paint();
        initPaint();
    }
    
    private void initPaint()
    {
        mDrawPaint = new Paint();
        mDrawPaint.setColor(mPaintColor);
        mDrawPaint.setAntiAlias(true);
        mDrawPaint.setStrokeWidth(mStrokeWidth);
        mDrawPaint.setStyle(Paint.Style.STROKE);
        mDrawPaint.setStrokeJoin(Paint.Join.ROUND);
        mDrawPaint.setStrokeCap(Paint.Cap.ROUND);
    }
    
    private void drawBackground(Canvas canvas)
    {
        mBackgroundPaint.setColor(Color.TRANSPARENT);
        mBackgroundPaint.setAntiAlias(true);
        mBackgroundPaint.setAlpha(0);
        canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), mBackgroundPaint);
    
    }
    
    private void drawPaths(Canvas canvas)
    {
        int i = 0;
        for (Path p : mPaths)
        {
            canvas.drawPath(p, mPaints.get(i));
            i++;
        }
    }
    
    @Override
    protected void onDraw(Canvas canvas)
    {
        drawBackground(canvas);
    
    D 1 Reply Last reply
    0
    • C Cristo1985

      Hi, i need a little help. I'm creating my custom Drawing View. The functionality I am trying to achieve is: Arrow icon => touch one point and drag until other point draws line-arrow Text icon => touch in one point of screen and enter a text (open keyboard and write and close it) Eraser icon => erase canvas drawn These is the design of the menu I've made: TOOLS MENU And all interface is here: INTERFACE How can I edit my custom drawing view for make tools specified above? Whick portion of code will I put inside CustomDrawingView class? Here my DrawingView class:

      public class DrawingView extends View
      {
      private Path mDrawPath;
      private Paint mBackgroundPaint;
      private Paint mDrawPaint;
      private Canvas mDrawCanvas;
      private Bitmap mCanvasBitmap;

      private ArrayList mPaths = new ArrayList<>();
      private ArrayList mPaints = new ArrayList<>();
      private ArrayList mUndonePaths = new ArrayList<>();
      private ArrayList mUndonePaints = new ArrayList<>();
      
      // Set default values
      private int mBackgroundColor = 0x00FFFFFF;
      private int mPaintColor = 0x00660000;
      private int mStrokeWidth = 10;
      
      public DrawingView(Context context, AttributeSet attrs)
      {
          super(context, attrs);
          init();
      }
      
      private void init()
      {
          mDrawPath = new Path();
          mBackgroundPaint = new Paint();
          initPaint();
      }
      
      private void initPaint()
      {
          mDrawPaint = new Paint();
          mDrawPaint.setColor(mPaintColor);
          mDrawPaint.setAntiAlias(true);
          mDrawPaint.setStrokeWidth(mStrokeWidth);
          mDrawPaint.setStyle(Paint.Style.STROKE);
          mDrawPaint.setStrokeJoin(Paint.Join.ROUND);
          mDrawPaint.setStrokeCap(Paint.Cap.ROUND);
      }
      
      private void drawBackground(Canvas canvas)
      {
          mBackgroundPaint.setColor(Color.TRANSPARENT);
          mBackgroundPaint.setAntiAlias(true);
          mBackgroundPaint.setAlpha(0);
          canvas.drawRect(0, 0, this.getWidth(), this.getHeight(), mBackgroundPaint);
      
      }
      
      private void drawPaths(Canvas canvas)
      {
          int i = 0;
          for (Path p : mPaths)
          {
              canvas.drawPath(p, mPaints.get(i));
              i++;
          }
      }
      
      @Override
      protected void onDraw(Canvas canvas)
      {
          drawBackground(canvas);
      
      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      My first suggestion would be to trim your code down to just the absolute minimum needed to reproduce the problem. Everything else is just extra noise getting in the way. People here don't mind helping at all, but they can't be expected to wade through a bunch of code that only you understand. Have you tried using different colors for text and background? Have you tried drawing on the DrawingView canvas itself instead of the canvas that is created from within the onSizeChanged() method? Have you tried stepping through the code using the debugger?

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      C 1 Reply Last reply
      0
      • D David Crow

        My first suggestion would be to trim your code down to just the absolute minimum needed to reproduce the problem. Everything else is just extra noise getting in the way. People here don't mind helping at all, but they can't be expected to wade through a bunch of code that only you understand. Have you tried using different colors for text and background? Have you tried drawing on the DrawingView canvas itself instead of the canvas that is created from within the onSizeChanged() method? Have you tried stepping through the code using the debugger?

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        C Offline
        C Offline
        Cristo1985
        wrote on last edited by
        #3

        The usual answer. Thanks. Cris

        M 1 Reply Last reply
        0
        • C Cristo1985

          The usual answer. Thanks. Cris

          M Offline
          M Offline
          Mycroft Holmes
          wrote on last edited by
          #4

          I do wonder what your expectation is when posting such a request, from volunteers! You seem to expect someone to go through your entire program and basically mentor you to get what you want to achieve. You don't post any error, there is no indication where the problem lies, just the entire code with a set of design requirements. So you should not expect anything but the usual answer. This implies that you have posted such requests before, it seems you do not learn from the usual answer!

          Never underestimate the power of human stupidity - RAH I'm old. I know stuff - JSOP

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups