Path, lineTo, moveTo, Coordinates, managed

Quickly share your Android Code Snippets here...

Path, lineTo, moveTo, Coordinates, managed

Postby plusminus » Sun Oct 26, 2008 3:46 pm

I just came along this snippet, as I had a couple of problems, where I was filling a couple of Paths in a loop, and could just hardly ensure, that a moveTo was called before a lineTo was called. So this is my 'managed' solution that extends Path to handle that problem.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. import android.graphics.Path;
  2.  
  3. import android.graphics.Point;
  4.  
  5.  
  6.  
  7. /**
  8.  
  9.  * ManagedPath ensures that <code>moveTo</code> gets called before <code>lineTo</code> gets called for the first time.<br />
  10.  
  11.  * Its only 'managed' for lines!
  12.  
  13.  * <b>Info:</b> Where you had to decide whether to call moveTo or lineTo before, now just call lineTo.
  14.  
  15.  * @author Nicolas Gramlich
  16.  
  17.  */
  18.  
  19. public class ManagedLinePath extends Path {
  20.  
  21.         // ===========================================================
  22.  
  23.         // Fields
  24.  
  25.         // ===========================================================
  26.  
  27.        
  28.  
  29.         protected boolean mDidMoveTo = false;
  30.  
  31.  
  32.  
  33.         // ===========================================================
  34.  
  35.         // Methods from SuperClass/Interfaces
  36.  
  37.         // ===========================================================
  38.  
  39.        
  40.  
  41.         public void lineTo(final Point p) {
  42.  
  43.                 this.lineTo(p.x, p.y);
  44.  
  45.         }
  46.  
  47.        
  48.  
  49.         @Override
  50.  
  51.         public void lineTo(float x, float y) {
  52.  
  53.                 if(!this.mDidMoveTo)
  54.  
  55.                         this.moveTo(x, y);
  56.  
  57.                 else
  58.  
  59.                         super.lineTo(x, y);
  60.  
  61.         }
  62.  
  63.        
  64.  
  65.         @Deprecated
  66.  
  67.         public void moveTo(final Point p) {
  68.  
  69.                 this.moveTo(p.x, p.y);
  70.  
  71.         }
  72.  
  73.  
  74.  
  75.         @Deprecated
  76.  
  77.         @Override
  78.  
  79.         public void moveTo(float x, float y) {
  80.  
  81.                 this.mDidMoveTo = true;
  82.  
  83.                 super.moveTo(x, y);
  84.  
  85.         }
  86.  
  87. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests