宽度和高度必须>创建位图

时间:2016-11-08 15:31:48

标签: android google-maps

我正在开发一个Android应用程序。谷歌地图和标记群集在我的应用程序中受到了限制。应用程序在4.4和棒棒糖设备上正常运行。我正在使用以下代码创建自定义标记并收到以下错误 java.lang.IllegalArgumentException:width和height必须是> 0

我正在使用此代码将自定义位图创建为标记

private Bitmap getMarkerBitmapFromView(@DrawableRes int resId,String typeProfile) {

    View customMarkerView = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_view, null);
    RelativeLayout _markerType = (RelativeLayout) customMarkerView.findViewById(R.id.markertype);
    ImageView markerImageView = (ImageView) customMarkerView.findViewById(R.id.image);
    //ImageView markerStatus = (ImageView) customMarkerView.findViewById(R.id.onlieStatus);

    if(typeProfile.equals("blue")) {
        _markerType.setBackgroundResource(R.drawable.markr_general);


    } else if(typeProfile.equals("green")){
        _markerType.setBackgroundResource(R.drawable.markr_interest);


    } else if(typeProfile.equals("red")) {
        _markerType.setBackgroundResource(R.drawable.markr_profession);


    }

    markerImageView.setImageResource(resId);
    customMarkerView.measure(View.MeasureSpec.EXACTLY, View.MeasureSpec.EXACTLY);
    customMarkerView.layout(0, 0, customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight());
    customMarkerView.buildDrawingCache();
    Bitmap returnedBitmap = Bitmap.createBitmap(customMarkerView.getMeasuredWidth(), customMarkerView.getMeasuredHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(returnedBitmap);
    canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_IN);
    Drawable drawable = customMarkerView.getBackground();
    if (drawable != null)
        drawable.draw(canvas);
    customMarkerView.draw(canvas);
    return returnedBitmap;

}

这是我的代码标记rendring类

 private class PersonRenderer extends DefaultClusterRenderer<Person> {
    private final IconGenerator mIconGenerator = new IconGenerator(getApplicationContext());
    private final IconGenerator mClusterIconGenerator = new IconGenerator(getApplicationContext());
    private final CircleImageView mImageView;
    private final CircleImageView mClusterImageView;
    private final int mDimension;


    public PersonRenderer() {
        super(getApplicationContext(), mMap, mClusterManager);

        View multiProfile = getLayoutInflater().inflate(R.layout.custom_marker_view, null);
        mClusterIconGenerator.setContentView(multiProfile);
        mClusterImageView = (CircleImageView) multiProfile.findViewById(R.id.image);

        mImageView = new CircleImageView(getApplicationContext());
        mDimension = (int) getResources().getDimension(R.dimen.custom_profile_image);
        mImageView.setLayoutParams(new ViewGroup.LayoutParams(mDimension, mDimension));
        int padding = (int) getResources().getDimension(R.dimen.custom_profile_padding);
        mImageView.setPadding(padding, padding, padding, padding);
        mIconGenerator.setContentView(mImageView);

    }

    @Override
    protected void onBeforeClusterItemRendered(Person item, MarkerOptions markerOptions) {
       mImageView.setImageResource(item.profilePhoto);


        markerOptions.icon(BitmapDescriptorFactory.fromBitmap(getMarkerBitmapFromView(item.profilePhoto,item.profileType))).title("asfas");
         //Bitmap icon = mIconGenerator.makeIcon();
       // markerOptions.icon(getMarkerBitmapFromView(item.profilePhoto)).title(item.name);
    }

    @Override
    protected void onBeforeClusterRendered(Cluster<Person> cluster, MarkerOptions markerOptions) {
      /*  List<Drawable> profilePhotos = new ArrayList<Drawable>(Math.min(4, cluster.getSize()));
        int width = mDimension;
        int height = mDimension;


        MultiDrawable multiDrawable = new MultiDrawable(profilePhotos);
        multiDrawable.setBounds(0, 0, width, height);

        mClusterImageView.setImageDrawable(multiDrawable);
        Bitmap icon = mClusterIconGenerator.makeIcon(String.valueOf(cluster.getSize()));*/




        if(cluster.getSize() == 2) {

            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(getClusteredMarkerBitmapFromView(R.drawable.for_two,2,cluster)));

        } else if(cluster.getSize() == 3) {

            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(getClusteredMarkerBitmapFromView(R.drawable.for_three,3,cluster)));

        } else if(cluster.getSize()  == 4) {

            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(getClusteredMarkerBitmapFromView(R.drawable.images_for,4,cluster)));

        } else if(cluster.getSize() > 4) {

            markerOptions.icon(BitmapDescriptorFactory.fromBitmap(getClusteredMarkerBitmapFromView(R.drawable.images_for,cluster.getSize(),cluster)));

        }


    }

    @Override
    protected boolean shouldRenderAsCluster(Cluster cluster) {
        // Always render clusters.
        return cluster.getSize() > 1;
    }
}

1 个答案:

答案 0 :(得分:0)

我认为如果您在控制台上实际打印宽度和高度值会有所帮助,这样您就可以更好地了解您的程序是否真的能够获取某些实际值。当尚未为它们分配任何值时,您可能正在调用宽度和高度。检查此SO线程以获得更多信息。