on Draw problem

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

on Draw problem

Postby Steph » Tue Oct 27, 2009 10:39 am

Hi everyBody!
I have a problem with my basic application. I try to make a simple paint, but i can't draw more one thing. When i draw a new thing the old desapear. In fact, i'd like to draw twice (to begin)!
Does anbybody know how to do?

thanks

(sorry to my english)
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

Postby azraeal » Tue Oct 27, 2009 5:27 pm

Post your current onDraw() code so we can understand what you are doing despite the language barrier.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby Steph » Tue Oct 27, 2009 7:18 pm

Thanks to help me. I really want to progress in android.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. @Override
  3.  
  4.         public boolean onTouchEvent(MotionEvent evt){
  5.  
  6.         try{                   
  7.  
  8.                 switch (evt.getAction())
  9.  
  10.                 {
  11.  
  12.                 case MotionEvent.ACTION_DOWN: Log.i("Info ","On touch event : ACTION_DOWN x="+evt.getX()+" y="+evt.getY());
  13.  
  14.                         _myView._x0=evt.getX();
  15.  
  16.                         _myView._y0=evt.getY();
  17.  
  18.                         break;
  19.  
  20.                        
  21.  
  22.                 case MotionEvent.ACTION_UP: Log.i("Info ","On touch event : ACTION_UP x="+evt.getX()+" y="+evt.getY());
  23.  
  24.                         _myView._x1=evt.getX();
  25.  
  26.                         _myView._y1=evt.getY();        
  27.  
  28.                     _layout.addView(_myView,_nbView);
  29.  
  30.                         break;          
  31.  
  32.                 }  
  33.  
  34.                 return true;                   
  35.  
  36.         }
  37.  
  38.         catch (Exception e) {
  39.  
  40.                         Log.e("Error", "onTouchEvent : "+ e.getMessage());
  41.  
  42.                         return false;
  43.  
  44.                 }
  45.  
  46.        
  47.  
  48.         }
  49.  
  50.  
  51.  
  52. public class MyView extends View {
  53.  
  54.        
  55.  
  56.         public float _x1=0, _y1=0, _x0=0, _y0=0;
  57.  
  58.         private int _taille;
  59.  
  60.         Paint _paint;
  61.  
  62.         Canvas _myCanvas = new Canvas();
  63.  
  64.         //private Paint _paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  65.  
  66.         public MyView(Context context){
  67.  
  68.                 super(context);
  69.  
  70.                 Log.i("Info", "_paintview Constructor 1");
  71.  
  72.                 _taille = 10;
  73.  
  74.                 _paint = new Paint(Paint.ANTI_ALIAS_FLAG);
  75.  
  76.                 _paint.setColor(Color.RED);
  77.  
  78.                 //this.setBackgroundColor(Color.TRANSPARENT);
  79.  
  80.         }
  81.  
  82.  
  83.  
  84.         @Override
  85.  
  86.         protected void onDraw(Canvas canvas) {
  87.  
  88.                 Log.i("Info ","onDraw start");
  89.  
  90.                 try{
  91.  
  92.                         // make the entire canvas white
  93.  
  94.                         //canvas.drawColor(Color.WHITE);
  95.  
  96.                         if(_x0!=_x1 || _y0!=_y1)
  97.  
  98.                         {
  99.  
  100.                                 canvas.drawLine(_x0, _y0, _x1, _y1, _paint);
  101.  
  102.                                 _x0=0;_y0=0; _x1=0; _y1=0;
  103.  
  104.                         }
  105.  
  106.                         else
  107.  
  108.                                 canvas.drawCircle(_x1, _y1, _taille, _paint);  
  109.  
  110.                        
  111.  
  112.                         //canvas.save();
  113.  
  114.                         _myCanvas=canvas;
  115.  
  116.                         //invalidate();
  117.  
  118.  
  119.  
  120.                 }
  121.  
  122.                 catch(Exception e)
  123.  
  124.                 {
  125.  
  126.                         Log.i("ERROR", "on draw : " + e.getMessage());
  127.  
  128.                 }
  129.  
  130.         }
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138.  
Parsed in 0.039 seconds, using GeSHi 1.0.8.4
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Postby azraeal » Tue Oct 27, 2009 7:58 pm

Let me get this strait, when you run your app, it only draws the first case?

Whenever you want it to be redrawn, just call invalidate() on the view. In your case, it looks like you can call MyView.invalidate() right before returning true in your touchEvent. You can also uncomment the invalidate in your onDraw() for continuous drawing but that's wasteful.
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Postby Steph » Tue Oct 27, 2009 8:17 pm

No, that draw all i want. When i touch, that draw good. But that clear the other draw. I'd like to have all the drawing line. But i have only one at a time :(
I think that the onDraw function which clear screen at every call.
how can i keep all my draw on the screen?
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Postby azraeal » Tue Oct 27, 2009 8:35 pm

Then your logic is off.
Notice:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(_x0!=_x1 || _y0!=_y1)
  2.  
  3. {
  4.  
  5. canvas.drawLine(_x0, _y0, _x1, _y1, _paint);
  6.  
  7. _x0=0;_y0=0; _x1=0; _y1=0;
  8.  
  9. }
  10.  
  11. else
  12.  
  13. canvas.drawCircle(_x1, _y1, _taille, _paint);  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

is exclusive.
If you want both to be drawn, adjust your logic as follows:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(_x0!=_x1 || _y0!=_y1)
  2.  
  3. {
  4.  
  5. canvas.drawLine(_x0, _y0, _x1, _y1, _paint);
  6.  
  7. _x0=0;_y0=0; _x1=0; _y1=0;
  8.  
  9. }
  10.  
  11. if(put new conditions here)
  12.  
  13. {
  14.  
  15. canvas.drawCircle(_x1, _y1, _taille, _paint);
  16.  
  17. }  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
azraeal
Experienced Developer
Experienced Developer
 
Posts: 71
Joined: Fri Sep 04, 2009 6:34 pm
Location: California

Top

Postby Steph » Tue Oct 27, 2009 10:53 pm

I understand what you say, but that's not the point :(. I certainly didn't be clear in my explanation. Your point is to differentiate circle an line.
In fact, the onDraw() clear the caneva and draw my new form. I'd like it doesn't clear my caneva.
I search in a lot of function in View. I've tryed to add view by view in my layout. Nothing work.


really thanks for your time. I'll continu to try tomorrow.
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Postby I_Artist » Wed Oct 28, 2009 4:44 pm

Hi All, first post on AndDev!

Tell me if I'm wrong Steph, but what you really want to do is leave all the previously drawn lines on the screen, correct?

If that's the case well, YOU are responsible for drawing them, not the system. Your calls to Invalidate() clear the screen, you then need to redraw the previously drawn lines yourself. The way to do that is by remembering all the previous points, most probably in an array.

I think you should be able to resolve your problem with this new information,

cheers!

I_Artist
The I_Artist

follow me on Twitter: @I_Artist
follow Android Development on Twitter: #androiddev

Show the world that you are an #androiddev, wear the badge http://www.twibbon.com/join/androiddev
User avatar
I_Artist
Developer
Developer
 
Posts: 33
Joined: Fri Oct 16, 2009 3:49 am
Location: St-Eustache, Quebec

Postby Steph » Wed Oct 28, 2009 4:47 pm

Thats what i want thanks. i'm going to try what you purpose. I hope i'll find the way.
I post the answer when i solve my problem.

Thanks all!
Steph
Experienced Developer
Experienced Developer
 
Posts: 90
Joined: Mon Oct 20, 2008 1:48 pm
Location: France

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Exabot [Bot] and 5 guests