且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

android将XML视图转换为位图而不显示它

更新时间:2023-11-11 21:47:28

您的cluster.getLayoutParams()可能是null.首先,您需要测量展开视图的宽度/高度,然后分配给它.请按照以下步骤进行操作:

Your cluster.getLayoutParams() is probably null. First, you need to measure the width/height of your inflated view and then assign to it. Do it as below:

private Bitmap createClusterBitmap(int clusterSize) {
    View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster,
            null);

    TextView clusterSizeText = (TextView) cluster.findViewById(R.id.map_cluster_text);
    clusterSizeText.setText(String.valueOf(clusterSize));

    cluster.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
            MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    cluster.layout(0, 0, cluster.getMeasuredWidth(),cluster.getMeasuredHeight());

    final Bitmap clusterBitmap = Bitmap.createBitmap(cluster.getMeasuredWidth(),
            cluster.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(clusterBitmap);
    cluster.draw(canvas);

    return clusterBitmap;
}