代码之家  ›  专栏  ›  技术社区  ›  Steve Haley

如何将较小的位图复制到较大的位图?

  •  8
  • Steve Haley  · 技术社区  · 16 年前

    希望这应该是一个简单的问题。我试图将一系列小位图复制到一个较大的位图中,将它们并排排列,像素中没有任何间隙或重叠。例如,如果我有3个方形位图,我想将它们复制到一个细长的矩形中。我知道如何做相反的事情,即用一个较大的位图创建一个小位图,但不是这样。什么是正确的命令?

    (如果有人好奇的话,我想这样做是为了能够重用我编写的用于处理单个位图动画的代码。)

    1 回复  |  直到 16 年前
        1
  •  14
  •   Seth    16 年前

    Bitmap makeBigBitmap(Bitmap srcBmps[]) {
        Bitmap wideBmp;
        Canvas wideBmpCanvas;
        Rect src, dest;
    
        // assume all of the src bitmaps are the same height & width
        wideBmp = Bitmap.createBitmap(srcBmps[0].getWidth() * srcBmps.length, 
            srcBmps[0].getHeight(), srcBitmaps[0].getConfig());
    
        wideBmpCanvas = new Canvas(wideBmp); 
    
        for (int i = 0; i < srcBmps.length; i++) {
             src = new Rect(0, 0, srcBmps[i].getWidth(), srcBmps[i].getHeight());
             dest = new Rect(src); 
             dest.offset(i * srcBmps[i].getWidth(), 0); 
    
             wideBmpCanvas.drawBitmap(srcBmps[i], src, dest, null); 
        }
    
        return wideBmp;
    }