在Android中,为什么我的组合位图是空白的?

时间:2014-01-28 22:20:30

标签: android bitmap

我正在使用一组分层图像(想想堆叠),我需要将它们组合成一个元素。

我的解决方案是Combine multiple bitmap into one

//send a map to the method that has my stored image locations in order
private Bitmap combineImageIntoOne(NavigableMap<Integer, String> layerImages) {
        //size of my bitmaps
        int w = 400, h = 400;
        //bitmap placeholder
        Bitmap productIndex = null;

        //flattened layers
        Bitmap temp = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
        //canvas to write layers to
        Canvas canvas = new Canvas(temp);
        int top = 0;
        for (Map.Entry<Integer, String> e : layerImages.entrySet()) {

            //create the layer bitmap
            productIndex = decodeSampledBitmapFromResource(getResources(), e.getValue(), 400, 400);

            //add layer to canvas
            canvas.drawBitmap(productIndex, 0f, top, null);
        }

        //convert temp to a BitmapDrawable
        Drawable d = new BitmapDrawable(getResources(),temp);

        //set my image view to have the flattened image
        carBase.setImageDrawable(d);

        return temp;
    }

decodeSampledBitmapFromResource来自关于加载大型位图的Android文档:Loading Large Bitmaps Efficiently您可以查看该文档上的代码,看看我在做什么。我没有太多编辑Android代码。

我一直在使用Android代码很好地为FrameLayout添加图层,但是当图层开始变得非常高时,最终会耗尽内存。这种组合方法用于节省存储空间。

为什么最终位图没有任何内容?

1 个答案:

答案 0 :(得分:0)

Reference LINK <-------------------------

public Bitmap combineImages(Bitmap c, Bitmap s) { // can add a 3rd parameter 'String loc' if you want to save the new image - left some code to do that at the bottom 
    Bitmap cs = null; 

    int width, height = 0; 

    if(c.getWidth() > s.getWidth()) { 
      width = c.getWidth() + s.getWidth(); 
      height = c.getHeight(); 
    } else { 
      width = s.getWidth() + s.getWidth(); 
      height = c.getHeight(); 
    } 

    cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 

    Canvas comboImage = new Canvas(cs); 

    comboImage.drawBitmap(c, 0f, 0f, null); 
    comboImage.drawBitmap(s, c.getWidth(), 0f, null); 

    // this is an extra bit I added, just incase you want to save the new image somewhere and then return the location 
    /*String tmpImg = String.valueOf(System.currentTimeMillis()) + ".png"; 

    OutputStream os = null; 
    try { 
      os = new FileOutputStream(loc + tmpImg); 
      cs.compress(CompressFormat.PNG, 100, os); 
    } catch(IOException e) { 
      Log.e("combineImages", "problem combining images", e); 
    }*/ 

    return cs; 
  }