質問

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>
役に立ちましたか?

解決

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

他のヒント

AsycTask in Api Level above 10 has changed the Execution mechanism , when ever you mention targetedSdk :10 the apk get compiled for API level 10 and hence the dalvik machine treat it as .and if you want faster response you use use the following procedure in API level >10

http://developer.android.com/reference/android/os/AsyncTask.html#executeOnExecutor(java.util.concurrent.Executor,Params...)

I had the same problem with targetSDKVersion 13 or lower. The circle did not appear.

The solution to disable hardware acceleration for the view. Appearantly the drawCircle function does not work with accel on pre ICS. I added the following line to an init function of my view:

setLayerType(View.LAYER_TYPE_SOFTWARE,null);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top