Creating Thread from dispatch Draw

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

Creating Thread from dispatch Draw

Postby Kamal » Fri Jun 12, 2009 6:18 am

Hi....

Well, I will brief you a small scenario..

Class A extends MapActivity
{
Class B extends ViewGroup
{
protected void dispatchDraw(Canvas canvas)
{
//my drawings...
}
}
}



Whenever i click a button, approx 10000 points are drawn on the map. I have setted the map clickable to false and also not allowing the map to zoom. By doing so i assume that no one will invalidate and dispatch draw will not be called.

But even i do so, it takes lot time to draw(that is OK) but the system becomes too slow... I mean if i click on menu, menu items opens after a minute.

Why is this happening??? what could be the solution to overcome this.

OK.....
I thought i will create a thread which will start executing once dispatch draw is called...

protected void dispatchDraw(Canvas canvas)
{
Paint paint = new Paint();
paint.setARGB(200, 255, 0, 0);
super.dispatchDraw(canvas);
ObjDrawPoints = new DrawPoints(canvas, mMapView, paint); //create obj for thread to draw....
}

The Draw thread uses the canvas, mMapView, paint and draws the points
e.g.
.....
.....
canvas.drawLine(screenPts_Pre.x, screenPts_Pre.y, screenPts_Curr.x, screenPts_Curr.y, paint);

But the problem is, I CAN'T SEE THE DRAWING....

What am i doing wrong......
Kamal
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed May 27, 2009 11:15 am

Top

Postby WarrenFaith » Fri Jun 12, 2009 8:12 am

hm... i wouldn't draw with the thread, but i would trigger the drawing with the thread.
My game example:
My thread:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2. public void run() {
  3.     Canvas c;
  4.     while (_run) {
  5.         c = null;
  6.         try {
  7.             c = _surfaceHolder.lockCanvas(null);
  8.             synchronized (_surfaceHolder) {
  9.                 _panel.updatePhysics();
  10.                 _panel.checkForWinners();
  11.                 _panel.onDraw(c);
  12.             }
  13.         } finally {
  14.             // do this in a finally so that if an exception is thrown
  15.             // during the above, we don't leave the Surface in an
  16.             // inconsistent state
  17.             if (c != null) {
  18.                 _surfaceHolder.unlockCanvasAndPost(c);
  19.             }
  20.         }
  21.     }
  22. }
Parsed in 0.042 seconds, using GeSHi 1.0.8.4

The variable _panel is my SurfaceView I use...
More information on my Graphic tutorials: Playing with graphics in Android
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby Kamal » Fri Jun 12, 2009 9:58 am

Thanks for the reply...

well, i didn't actually get it....

can you tell me how i can do it in my project....? I have just started android programming.....
Kamal
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed May 27, 2009 11:15 am

Postby WarrenFaith » Fri Jun 12, 2009 10:07 am

Can you provide a bit more of your code?
I wouldn't use dispatchDraw() but onDraw() instead...

can you check your LogCat for errors and post it here too?
and please: use the java-buttons if you post code....
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Postby Kamal » Fri Jun 12, 2009 10:29 am

OK....

so here i go....

I have to trace the path and show it on map...
Map also must rotate based on heading value....
I have "View Full Path" in Menu which shows me the complete path travelled.....

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MapsActivity extends MapActivity implements MyDialogListener
  2.  
  3. {
  4.  
  5.     private SensorManager mSensorManager;
  6.  
  7.     private RotateView mRotateView;
  8.  
  9.     public MapView mMapView;
  10.  
  11.     private MyLocationOverlay mMyLocationOverlay;
  12.  
  13.  
  14.  
  15.  
  16.  
  17.     public void onCreate(Bundle savedInstanceState)
  18.  
  19.     {
  20.  
  21.         super.onCreate(savedInstanceState);
  22.  
  23.         mRotateView = new RotateView(this,this);
  24.  
  25.         mMapView = new MapView(this, "API_Key");
  26.  
  27.         mRotateView.addView(mMapView);
  28.  
  29.         setContentView(mRotateView);
  30.  
  31.     }
  32.  
  33.  
  34.  
  35.  
  36.  
  37.     private class RotateView extends ViewGroup implements SensorListener
  38.  
  39.     {
  40.  
  41.         private static final float SQ2 = 1.414213562373095f;
  42.  
  43.         private final SmoothCanvas mCanvas = new SmoothCanvas();
  44.  
  45.         private float mHeading = 0;
  46.  
  47.        
  48.  
  49.         public RotateView(Context context, MapActivity mapAct)
  50.  
  51.         {
  52.  
  53.             super(context);
  54.  
  55.         }
  56.  
  57.  
  58.  
  59.         @Override
  60.  
  61.         protected void dispatchDraw(Canvas canvas)
  62.  
  63.         {
  64.  
  65.             Paint paint = new Paint();
  66.  
  67.             paint.setARGB(200, 255, 0, 0);
  68.  
  69.             //canvas.save(Canvas.MATRIX_SAVE_FLAG);
  70.  
  71.             canvas.rotate(-mHeading, 320 * 0.5f, 430 * 0.5f);
  72.  
  73.             mCanvas.delegate = canvas;            
  74.  
  75.             super.dispatchDraw(mCanvas);
  76.  
  77.            
  78.  
  79.             int i;
  80.  
  81.             Point screenPts_Pre = new Point();
  82.  
  83.             Point screenPts_Curr = new Point();            
  84.  
  85.            
  86.  
  87.             /*Starting Point */
  88.  
  89.             mMapView.getProjection().toPixels(Point[0], screenPts_Pre);
  90.  
  91.             RectF oval = new RectF(screenPts_Pre.x - 2, screenPts_Pre.y + 2, screenPts_Pre.x + 2,screenPts_Pre.y - 2);
  92.  
  93.             canvas.drawOval(oval, paint);
  94.  
  95.  
  96.  
  97.             /* index changes dynamicaly as new point is added*/
  98.  
  99.             for(i= 1 ; i <= index; i++)
  100.  
  101.             {
  102.  
  103.         mMapView.getProjection().toPixels(Point[i-1], screenPts_Pre);
  104.  
  105.         mMapView.getProjection().toPixels(Point[i], screenPts_Curr);
  106.  
  107.                        
  108.  
  109.         RectF oval_cur = new RectF(screenPts_Curr.x - 2, screenPts_Curr.y + 2, screenPts_Curr.x + 2, screenPts_Curr.y - 2);                
  110.  
  111.         canvas.drawOval(oval_cur, paint);
  112.  
  113.                    
  114.  
  115.         paint.setStrokeWidth(2);
  116.  
  117.         canvas.drawLine(screenPts_Pre.x, screenPts_Pre.y, screenPts_Curr.x, screenPts_Curr.y, paint);
  118.  
  119.             }
  120.  
  121.         }
  122.  
  123.     }
  124.  
  125. }
Parsed in 0.050 seconds, using GeSHi 1.0.8.4


Now doing so, if the points are less, there seems to be no problem.... But if points on Map are more, It takes long time to paint the points and also responds very slow for any menu inputs.
This is because most of the time it is busy in plotting the points...

How do we handle it in a better way......
Kamal
Junior Developer
Junior Developer
 
Posts: 23
Joined: Wed May 27, 2009 11:15 am

Postby WarrenFaith » Fri Jun 12, 2009 12:02 pm

I am interested in you Point class.... because what does Point[i] returns?!

You could add some Log in your dispatchDraw().
I would add one in the first and last line and in the first line of your loop. Also log the time between the log entries in ms or ns to see if you have a performance problem from the very beginning or if it take more time for the last point than for the first point.
Second reason: you could find out how often this method is called...
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 6 guests