我有一个在HoneyComb上运行良好的Android应用程序,但在ICS上运行不正常。我有谷歌地图,最重要的是我使用圆圈创建雷达效果(圆圈的半径不断增加给出一种水波效果。)代码如下:
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();
以下是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))));
}
}
我已经浏览了各种论坛,发现ICS默认激活了HardwareAcceleration,所以我在清单中添加了以下标记但没有帮助。任何人都可以 告诉我这里出了什么问题。
<application
android:hardwareAccelerated="false"
....
</application>
答案 0 :(得分:0)
我可以通过在android清单文件中将android:targetVersion更改为10来解决此问题。它不能使用API&gt; = 11的原因是AsyncTask方法的行为发生了变化。看看Android AsyncTask not being called for minutes on ICS
答案 1 :(得分:0)
Aspi Level中的AsycTask已经改变了执行机制,当你提到targetedSdk:10时,apk被编译为API级别10,因此dalvik机器将其视为。如果你想要更快的响应你使用以下API级别&gt; 10
的程序答案 2 :(得分:0)
我对targetSDKVersion 13或更低版本有同样的问题。圆圈没有出现。
禁用视图硬件加速的解决方案。显然,drawCircle函数不适用于pre ICS上的accel。 我将以下行添加到我的视图的init函数中:
setLayerType(View.LAYER_TYPE_SOFTWARE,null);