创建位图时,宽度和高度必须 > 0
Posted
技术标签:
【中文标题】创建位图时,宽度和高度必须 > 0【英文标题】:width and height must be > 0 while creating bitmap 【发布时间】:2016-11-08 15:31:48 【问题描述】:我正在开发一个安卓应用程序。 Google Map and Marker clustering 在我的应用程序中实现。应用程序在 4.4 和棒棒糖设备上运行良好。我正在使用以下代码创建自定义标记并收到以下错误 java.lang.IllegalArgumentException: 宽度和高度必须 > 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;
这是我的代码标记渲染类
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;
【问题讨论】:
我认为如果您在控制台上实际打印宽度和高度的值会有所帮助,这样您就可以更好地了解您的程序是否真的能够获取一些实际值。当尚未为它们分配任何值时,您可能会调用宽度和高度。检查此SO thread 以获得更多信息。 解决了吗? 【参考方案1】:我认为如果您在控制台上实际打印宽度和高度的值会有所帮助,这样您就可以更好地了解您的程序是否真的能够获取一些实际值。当尚未为它们分配任何值时,您可能会调用宽度和高度。查看此 SO 线程以获取更多信息。
【讨论】:
以上是关于创建位图时,宽度和高度必须 > 0的主要内容,如果未能解决你的问题,请参考以下文章