Search code examples
androidviewdrawandroid-4.0-ice-cream-sandwichhardware-acceleration

Android view/canvas drawCircle issue with ICS


I have an android app which was running good on HoneyComb but on ICS it's not working properly. I have google map and on top of that I am creating radar effect using the circles (circles' radius keep on increasing giving kind of water wave effects.) The code is below:

class ShowRadar extends AsyncTask<Void, Void, RadarView>
        {

            private Context ctx;
            private int radius;
            private GeoPoint gp;
            private MapView mv;
            ShowRadar(Context ctxt, MapView mv, int radius, GeoPoint gp)
            {
                this.ctx = ctxt;
                this.radius = radius;
                this.gp = gp;
                this.mv = mv;
            }

            @Override
            protected RadarView doInBackground(Void... params) 
            {
                RadarView rdView = new RadarView(ctx, mv, gp, radius);
                LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                rdView.setLayoutParams(lp);

                return rdView;
            }

            @Override
            protected void onPostExecute(RadarView result) {
                if(this.radius <= 10000)
                {
                    if(this.radius < 50)
                    {
                        this.radius += 50;
                    }
                    else if(this.radius < 100)
                    {
                        this.radius += 100;
                    }
                    else if(this.radius < 500)
                    {
                        this.radius += 500;
                    }
                    else
                    {
                        this.radius += 1000;
                    }
                }
                else
                    this.radius = 10;

                if(radarUpdated || radarCount > 1000)
                {
                    if(radarCount > 1000 && !radarUpdated)
                    {
                        Toast.makeText(mContext, "Delay in response, wait a while", Toast.LENGTH_SHORT).show();
                    }
                    radarCount = 0;
                    if (oldrd != null) {
                        rel.removeView(oldrd);
                        oldrd = null;
                    }

                }
                else
                {
                    radarCount++;

                    if(oldrd != null)
                    {
                        rel.removeView(oldrd);
                    }
                    oldrd = result;
                    rel.addView(result);

                    new ShowRadar(ctx, mv, this.radius, gp).execute();

                }
            }
        }
        new ShowRadar(mContext, mapView, 10, center).execute();

And below is the code for RadarView

  public class RadarView extends View {

private int circleRadius = 500;
private GeoPoint p;
public RadarView(Context context) {
 super(context);
 init();
}

public RadarView(Context context, MapView mv, GeoPoint pt, Integer radius)
{
    super(context);
    this.mapView = mv;
    this.p = pt;
    this.circleRadius = radius;
    init();
}

private void init(){
 paint = new Paint();
 paint.setColor(Color.GREEN);
 paint.setStrokeWidth(10);
 paint.setStyle(Paint.Style.STROKE);

}

@Override
protected void onDraw(Canvas canvas) {
 Projection projection = mapView.getProjection();
 Point pt = new Point();
 projection.toPixels(p, pt);

 float circleRadius = metersToRadius(this.circleRadius, mapView, (double)p.getLatitudeE6()/1E6);
 Paint circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
 circlePaint.setARGB(150, 12, 232, 91);
 circlePaint.setStyle(Style.STROKE);
 circlePaint.setStrokeWidth(10);
 if(canvas.isHardwareAccelerated())
 {
     mapView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
 }
 canvas.drawCircle((float)pt.x, (float)pt.y, circleRadius, circlePaint);

 super.onDraw(canvas);
}

 public static int metersToRadius(float meters, MapView map, double latitude) {
        return (int) (map.getProjection().metersToEquatorPixels(meters) * (1/ FloatMath.cos((float) Math.toRadians(latitude))));         
    }

}

I have gone through various forums and found that ICS by default activates HardwareAcceleration so I have added following tag in the manifest but no help. Could anybody suggest me what's going wrong here.

 <application
    android:hardwareAccelerated="false"
    ....
</application>

Solution

  • I could resolve the issue simply by changing the android:targetVersion to 10 in android manifest file. The reason why it isn't working with API>=11 is the change in the behaviour of the AsyncTask method. have a look at Android AsyncTask not being called for minutes on ICS