是否可以加载在android中的InfoWindow中重绘的画布?

时间:2016-05-04 08:55:15

标签: android android-canvas infowindow

我有一个探测器。我想在Infowindow画一个温度计,温度从json加载。这种温度计必须以最大和最小的温度加载。如果我在一个单独的proyect中做它可以正常工作,但是当我尝试加载infowindow时画布不起作用。我留下一些图片。

分开的项目。 1

在infoWindow proyect中 2

我加载customMarkerAdapter。

gMap.setInfoWindowAdapter(new CustomMarkerAdapter(getContext(),city,maxT,minT,weatherObject));

CustomMarkerAdapter的代码

public class CustomMarkerAdapter implements GoogleMap.InfoWindowAdapter {

    private double tempMax,tempMin;
    private Context context;
    private Geoname city;
    private WeatherObject weatherObject;

    public CustomMarkerAdapter(Context contextC, Geoname geocity, double maxT, double minT, WeatherObject weatherObjectP) {
        tempMax=maxT;
        tempMin=minT;
        context=contextC;
        city=geocity;
        weatherObject=weatherObjectP;
    }

    @Override
    public View getInfoWindow(Marker marker) {
        return null;
    }

    @Override
    public View getInfoContents(Marker marker) {
        LayoutInflater inflater= (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View rowView=inflater.inflate(R.layout.custom_marker,null,true);

        RelativeLayout rlTer= (RelativeLayout) rowView.findViewById(R.id.rlTer);

        //here i load my customMarker
        rlTer.addView(new com.example.lenovo_talentomobile.jsongson.CustomView.CustomMarker(context,tempMax,tempMin));

        TextView tvCiudad= (TextView) rowView.findViewById(R.id.tvPrueba);
        TextView tvHumedad= (TextView) rowView.findViewById(R.id.tvValueH);

        tvCiudad.setText(city.asciiName);
        tvHumedad.setText(String.valueOf(weatherObject.weatherObservations.get(0).humidity)+"%");

        return  rowView;
    }

}

customMarker的代码

public class CustomMarker extends View {

private double tempMax,tempMin;
private Paint paintNegro,paintBlanco,paintAzul,paintRojo;
private int barMax=0,barMin=0;
private int increasingM,increasingMin;

public CustomMarker(Context context, double tempMaxC, double tempMinC) {
    super(context);
    tempMax=tempMaxC;
    tempMin=tempMinC;
    init();
}

public CustomMarker(Context context, AttributeSet attrs) {
    super(context, attrs);
    init();
}

public CustomMarker(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomMarker(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super(context, attrs, defStyleAttr, defStyleRes);
    init();
}

public void init(){

    paintNegro =new Paint();
    paintNegro.setColor(getResources().getColor(R.color.negro));
    paintNegro.setStyle(Paint.Style.STROKE);
    paintNegro.setStrokeWidth(4);

    paintBlanco=new Paint();
    paintBlanco.setColor(getResources().getColor(R.color.blanco));
    paintBlanco.setStyle(Paint.Style.STROKE);
    paintBlanco.setStrokeWidth(4);

    paintAzul=new Paint();
    paintAzul.setColor(getResources().getColor(R.color.azul));
    paintAzul.setStyle(Paint.Style.FILL);
    paintAzul.setStrokeWidth(13);

    paintRojo=new Paint();
    paintRojo.setColor(getResources().getColor(R.color.rojo));
    paintRojo.setStyle(Paint.Style.FILL);
    paintRojo.setStrokeWidth(13);
}

@Override
protected void onDraw(Canvas canvas) {
    canvas.drawColor(getResources().getColor(R.color.blanco));
    pintaFigura(canvas);

    barMax=barMax+increasingM;
    moverYMax(barMax,tempMax);

    if(barMin<tempMin) {
        barMin = barMin + increasingMin;
        moverYMin(barMin, tempMin);
    }

    canvas.drawLine(18,90,18,90-(70*barMax/60),paintRojo);
    canvas.drawLine(31,90,31,90-(70*barMin/60),paintAzul);

    if(barMax>=tempMax){
        paintRojo.setStrokeWidth(0);
        paintRojo.setTextSize(20);
        canvas.drawText(""+tempMax,50, (float) (90-tempMax),paintRojo);
        paintAzul.setStrokeWidth(0);
        paintAzul.setTextSize(20);
        canvas.drawText(""+tempMin,50, (float) (90-tempMin),paintAzul);
    }

}

public void moverYMax(float y, double max){
    if(y<max){
        increasingM=1;
        invalidate();
    }
}


public void moverYMin(float y, double max){
    if(y<max){
        increasingMin=1;
        invalidate();
    }
}

public void pintaFigura(Canvas canvas){

    RectF rectF = new RectF();
    rectF.set(25 - 20, 100 - 20, 25 + 20, 100 + 20);
    //1-rectangulo
    canvas.drawRect(10,20,40,90,paintNegro);
    //2-linea blanco arriba
    canvas.drawLine(12,20,37,20,paintBlanco);
    //3-linea abajo blanco
    canvas.drawLine(12,90,37,90,paintBlanco);

    //pinto los circulos de abajo
    canvas.drawArc(rectF, 270, 180, true, paintAzul);
    canvas.drawArc(rectF, 90, 180, true, paintRojo);

    //4-contorno-negro circulo abajo
    canvas.drawArc(rectF,310,280,false,paintNegro);

    //5-arco arriba*/
    RectF oval=new RectF();
    // float left = center_x - radius;
    // float top = center_y - radius;
    // float right = center_x + radius;
    // float bottom = center_y + radius;
    float left=25-15;
    float top=25-15;
    float right=25+15;
    float bottom=25+15;
    oval.set(left, top, right, bottom);
    canvas.drawArc(oval,180,180,false,paintNegro);
}

}

0 个答案:

没有答案