位图的安卓作物中心位图、作物、中心

2023-09-11 12:12:54 作者:嫌我你滚

我有位图这是正方形或长方形。我走最短边,做这样的事情:

I have bitmaps which are squares or rectangles. I take the shortest side and do something like this:

int value = 0;
if (bitmap.getHeight() <= bitmap.getWidth()) {
    value = bitmap.getHeight();
} else {
    value = bitmap.getWidth();
}

Bitmap finalBitmap = null;
finalBitmap = Bitmap.createBitmap(bitmap, 0, 0, value, value);

然后,我用这个缩放到144×144位图:

Then I scale it to a 144 x 144 Bitmap using this:

Bitmap lastBitmap = null;
lastBitmap = Bitmap.createScaledBitmap(finalBitmap, 144, 144, true);

问题是,它作物原始位图的左上角,任何人都具有code裁剪位图的中心?

Problem is that it crops the top left corner of the original bitmap, Anyone has the code to crop the center of the bitmap?

推荐答案

这可以实现用: Bitmap.createBitmap(源,X,Y,宽,高)

if (srcBmp.getWidth() >= srcBmp.getHeight()){

  dstBmp = Bitmap.createBitmap(
     srcBmp, 
     srcBmp.getWidth()/2 - srcBmp.getHeight()/2,
     0,
     srcBmp.getHeight(), 
     srcBmp.getHeight()
     );

}else{

  dstBmp = Bitmap.createBitmap(
     srcBmp,
     0, 
     srcBmp.getHeight()/2 - srcBmp.getWidth()/2,
     srcBmp.getWidth(),
     srcBmp.getWidth() 
     );
}