drawLine clearing canvas?

Tutorials concerning the OpenGL® ES cross-platform API for full-function 2D and 3D graphics on the Google-Android platform.

drawLine clearing canvas?

Postby squizz0r » Wed Mar 31, 2010 10:10 pm

Hi everyone!

I'm currently making my first graphics application to see what I can achieve.

At the moment I can touch the screen and drag my finger to somewhere else to create a line.
When I touch again to draw a new line the previous line is removed.

I'm novice to graphics programming so I bet its something simple but I have no idea!

Help please!
Thanks :)



Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Main extends Activity implements OnTouchListener
  3.  
  4. {
  5.  
  6.     float x1 = 0;
  7.  
  8.     float y1 = 0;
  9.  
  10.     float x2 = 0;
  11.  
  12.     float y2 = 0;
  13.  
  14.    
  15.  
  16.     @Override
  17.  
  18.     public void onCreate(Bundle savedInstanceState)
  19.  
  20.     {
  21.  
  22.         super.onCreate(savedInstanceState);
  23.  
  24.         setContentView(R.layout.main);
  25.  
  26.        
  27.  
  28.         LinearLayout layout = (LinearLayout)this.findViewById(R.id.layout_id);
  29.  
  30.         layout.setOnTouchListener((OnTouchListener)this);
  31.  
  32.         layout.addView(new Drawer(this));
  33.  
  34.     }
  35.  
  36.    
  37.  
  38.     @Override
  39.  
  40.     public boolean onTouch(View v, MotionEvent event)
  41.  
  42.     {
  43.  
  44.         switch (event.getAction())
  45.  
  46.         {
  47.  
  48.                 case MotionEvent.ACTION_DOWN:
  49.  
  50.                         x1 = event.getX();
  51.  
  52.                         y1 = event.getY();
  53.  
  54.                         return true;
  55.  
  56.                 case MotionEvent.ACTION_MOVE:
  57.  
  58.                         x2 = event.getX();
  59.  
  60.                         y2 = event.getY();
  61.  
  62.                         v.invalidate();
  63.  
  64.                         return true;
  65.  
  66.                 case MotionEvent.ACTION_UP:
  67.  
  68.                         x2 = event.getX();
  69.  
  70.                         y2 = event.getY();
  71.  
  72.                         v.invalidate();
  73.  
  74.                         return true;
  75.  
  76.         }
  77.  
  78.         return false;
  79.  
  80.     }
  81.  
  82.  
  83.  
  84.     public class Drawer extends View
  85.  
  86.     {
  87.  
  88.          public Drawer(Context context)
  89.  
  90.          {
  91.  
  92.               super(context);
  93.  
  94.          }
  95.  
  96.  
  97.  
  98.          protected void onDraw(Canvas canvas)
  99.  
  100.          {
  101.  
  102.                  Paint p = new Paint();
  103.  
  104.              p.setColor(Color.parseColor("#7CFC00"));
  105.  
  106.  
  107.  
  108.              canvas.drawLine(x1, y1, x2 , y2, p);
  109.  
  110.              invalidate();
  111.  
  112.          }
  113.  
  114.      }
  115.  
  116. }
  117.  
  118.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4
squizz0r
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Mar 31, 2010 10:06 pm

Top

Postby eyespyus » Sat Apr 17, 2010 3:50 am

Class Overview

The Canvas class holds the "draw" calls. To draw something, you need 4 basic components: A Bitmap to hold the pixels, a Canvas to host the draw calls (writing into the bitmap), a drawing primitive (e.g. Rect, Path, text, Bitmap), and a paint (to describe the colors and styles for the drawing).

You will need to draw into something by creating a bitmap to hold the lines drawn. This class will show more on what to do (although right now it draws the intermediate lines also):

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.    public class Drawer extends View
  3.  
  4.     {
  5.  
  6.         private Bitmap  mBitmap;
  7.  
  8.         private Canvas  mCanvas;
  9.  
  10.  
  11.  
  12.         private Paint   mBitmapPaint;
  13.  
  14.        
  15.  
  16.          public Drawer(Context context)
  17.  
  18.          {
  19.  
  20.               super(context);
  21.  
  22.               mBitmap = Bitmap.createBitmap(400,800,Bitmap.Config.ARGB_8888);
  23.  
  24.               mCanvas = new Canvas(mBitmap);
  25.  
  26.               mBitmapPaint = new Paint(Paint.DITHER_FLAG);
  27.  
  28.               mBitmapPaint.setColor(Color.MAGENTA);
  29.  
  30.          }
  31.  
  32.  
  33.  
  34.          protected void onDraw(Canvas canvas)
  35.  
  36.          {
  37.  
  38.            Paint p = new Paint();
  39.  
  40.              p.setColor(Color.parseColor("#7CFC00"));
  41.  
  42.              canvas.drawBitmap(mBitmap, 0, 0, p);
  43.  
  44.              
  45.  
  46.              canvas.drawLine(x1, y1, x2 , y2, p);
  47.  
  48.              mCanvas.drawLine(x1, y1, x2 , y2, mBitmapPaint);
  49.  
  50.              
  51.  
  52.              
  53.  
  54.              invalidate();
  55.  
  56.          }
  57.  
  58.      }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
User avatar
eyespyus
Freshman
Freshman
 
Posts: 7
Joined: Fri Mar 05, 2010 9:31 pm

Top

Return to Android 2D/3D Graphics - OpenGL Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests