public static Bitmap fitBitmapToShare(Bitmap bitmap, int newWidth, int newHeight) {
Bitmap newBitmap = scaleBitmap(bitmap, newWidth, newHeight);
if (newBitmap == null || newBitmap.isRecycled()) {
return null;
}
if (newWidth == 0 || newHeight == 0) {
throw new IllegalArgumentException("illegal argument");
}
int width = newBitmap.getWidth();
int height = newBitmap.getHeight();
int startX = (width - newWidth) / 2;
int startY = (height - newHeight) / 2;
int endX = width - (width - newWidth) / 2;
int endY = height - (height - newHeight) / 2;
Bitmap finalBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.RGB_565);
Canvas canvas = new Canvas(finalBitmap);
Paint paint = new Paint();
paint.setFilterBitmap(true);
canvas.save();//记录原来的canvas状态
canvas.drawBitmap(newBitmap, new Rect(startX, startY, endX, endY), new Rect(0, 0, newWidth, newHeight), paint); //将阉割过的图片画到(0,0)位置
canvas.restore();//恢复canvas状态
return finalBitmap;
}