Custom View - invalidate and onDraw()

Problems with Canvas, OpenGL, etc...

Custom View - invalidate and onDraw()

Postby harmjanr » Tue Jun 07, 2011 9:33 am

Hi,

I made a custom control of a MapView:

Code: Select all
public class MyMapView extends MapView {

   public Polyline polyLine;
   
   public MyMapView(Context context, AttributeSet atts) {
      super(context, atts);
   }
   
   @Override
   public void onDraw(Canvas canvas){
      super.onDraw(canvas);
      
      if(polyLine != null){
             //do something
      }
      
   }


In a method of another class, I have this code:
Code: Select all
mapView.polyLine = polyLine;
mapView.invalidate();


I put a breakpoint inside the if(polyLine != null), but it is never hit. The polyLine in mapView.polyLine = polyLine; is not null, so that works fine.
What am I doing wrong?
harmjanr
Freshman
Freshman
 
Posts: 6
Joined: Thu Apr 14, 2011 8:55 am

Top

Re: Custom View - invalidate and onDraw()

Postby MichaelEGR » Tue Jun 07, 2011 12:54 pm

Use "postInvalidate()" from a non-UI thread. In general I also recommend using a little encapsulation and create a setter / getter for Polyline. IE setPolyline(Polyline line), getPolyline()
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: Custom View - invalidate and onDraw()

Postby harmjanr » Tue Jun 07, 2011 1:14 pm

Thanks for your reply. I tried postInvalidate, but it doesn't work either..

And yeah, I will make a getter and setter for polyLine. But this is easier for testing :)
harmjanr
Freshman
Freshman
 
Posts: 6
Joined: Thu Apr 14, 2011 8:55 am

Re: Custom View - invalidate and onDraw()

Postby MichaelEGR » Wed Jun 08, 2011 9:25 am

I haven't messed around with MapView or custom drawing on one... It appears there are different steps involved in drawing on top of a MapView. Reference the following:

http://developer.android.com/resources/ ... pview.html
http://stackoverflow.com/questions/1937 ... oordinates

Refer to the SO post above. I've not used MapView, so it appears there is a different process than other custom View creation. What I mentioned in 1st reply should work for normal custom views.
Founder & Principal Architect; EGR Software LLC
http://www.typhonrt.org
http://www.egrsoftware.com
User avatar
MichaelEGR
Senior Developer
Senior Developer
 
Posts: 147
Joined: Thu Jan 21, 2010 5:30 am
Location: San Francisco, CA

Re: Custom View - invalidate and onDraw()

Postby harmjanr » Thu Jun 09, 2011 8:27 am

I solved the problem! MyMapView wasn't the problem, neither was the invalidate/onDraw method. I set the view with setContentView(R.layout.main);, which created a new MyMapView instead of using the current one. This caused that the polyLine was null, and the MapView didn't invalidate.

Thanks for your help anyway!
harmjanr
Freshman
Freshman
 
Posts: 6
Joined: Thu Apr 14, 2011 8:55 am

Top

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

Who is online

Users browsing this forum: Google [Bot] and 2 guests