相关疑难解决方法(0)

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

我正在尝试为我的地图集群设置视图.我正在从XML膨胀视图并根据群集大小设置文本,我想显示该视图.在下面的代码中,我得到一个null位图作为回报:

private Bitmap createClusterBitmap(int clusterSize) {
    View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster, null);        
    cluster.setText(String.valueOf(clusterSize));
    cluster.setDrawingCacheEnabled(true);
    cluster.buildDrawingCache(true);
    Bitmap bm = cluster.getDrawingCache();
    return bm;
}
Run Code Online (Sandbox Code Playgroud)

在下面的代码中我得到第四行的空指针(布局参数):

private Bitmap createClusterBitmap(int clusterSize) {
    View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster, null);
    TextView clusterSizeText = (TextView) cluster.findViewById(R.map.cluster);
    clusterSizeText.setText(String.valueOf(clusterSize));
    Bitmap clusterBitmap = Bitmap.createBitmap( cluster.getLayoutParams().width, cluster.getLayoutParams().height, Bitmap.Config.ARGB_8888);                
    Canvas clusterCanvas = new Canvas(clusterBitmap);
    cluster.layout(cluster.getLeft(), cluster.getTop(), cluster.getRight(), cluster.getBottom());
    cluster.draw(clusterCanvas);
    return clusterBitmap;
}
Run Code Online (Sandbox Code Playgroud)

当将其更改为以下代码时,我得到的不是错误,但没有绘制任何内容:

private Bitmap createClusterBitmap(int clusterSize) {
    View cluster = LayoutInflater.from(context).inflate(R.layout.map_cluster, null);
    TextView clusterSizeText = (TextView) cluster.findViewById(R.map.cluster);
    clusterSizeText.setText(String.valueOf(clusterSize));
    Bitmap clusterBitmap = Bitmap.createBitmap( 50,50 …
Run Code Online (Sandbox Code Playgroud)

xml android bitmap

8
推荐指数
1
解决办法
5195
查看次数

标签 统计

android ×1

bitmap ×1

xml ×1