Yasir Ameen Yasir Ameen - 22 days ago 10
Android Question

width and height must be > 0 while creating bitmap

I am working on an android app. Google Map and Marker clustering is impemented in my app. App is running fine on 4.4 and lollipop devices. I am creating custom marker using the following code and recieving following error
java.lang.IllegalArgumentException: width and height must be > 0

I am using this code for creating custom bitmap as marker

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;

}


here is my code marker rendring class

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;
}
}

Answer

I think it would help if you to actually print the values of width and height on the console so you would better understand if your program was really able to fetch some actual values. You might be calling width and height when no values haven't been assigned to them yet. Check this SO thread for additional insight.