Java Android按宽度拉伸位图/PNG,但保持高度

Java Android按宽度拉伸位图/PNG,但保持高度,java,android,bitmap,Java,Android,Bitmap,我有一个png图像资源,我正在将其制作成画布,然后在其上绘制另一个png资源(箭头)。我想知道我怎样才能把这个箭头的宽度和高度保持一致?我尝试过的所有方法都会导致箭头部分被截断或出现其他错误。下面我粘贴了当前的尝试,但是箭头3(缩放为比原稿更宽和更短)比原稿更高和更宽,部分箭头在顶部和底部被切断。谢谢你的帮助 Bitmap fieldBitmapResource = BitmapFactory.decodeResource(getResources(), R.dra

我有一个png图像资源,我正在将其制作成画布,然后在其上绘制另一个png资源(箭头)。我想知道我怎样才能把这个箭头的宽度和高度保持一致?我尝试过的所有方法都会导致箭头部分被截断或出现其他错误。下面我粘贴了当前的尝试,但是箭头3(缩放为比原稿更宽和更短)比原稿更高和更宽,部分箭头在顶部和底部被切断。谢谢你的帮助

   Bitmap fieldBitmapResource = BitmapFactory.decodeResource(getResources(),
            R.drawable.football_field);
    Bitmap fieldBitmap = fieldBitmapResource.copy(null, true);
    Canvas fieldCanvas = new Canvas(fieldBitmap);

    Bitmap arrowBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.back);
    fieldCanvas.drawBitmap(arrowBitmap, 0, 0, null); //draw arrow to field

    Bitmap arrow2 = scaleCenterCrop(arrowBitmap, 150, 155);
    fieldCanvas.drawBitmap(arrow2, 1025, 573, null);

    Bitmap arrow3 = scaleCenterCrop(arrowBitmap, 150, 400);
    fieldCanvas.drawBitmap(arrow3, 1200, 600, null);

    ...

    public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();

// Compute the scaling factors to fit the new height and width, respectively.
// To cover the final image, the final scaling will be the bigger
// of these two.
float xScale = (float) newWidth / sourceWidth;
float yScale = (float) newHeight / sourceHeight;
float scale = Math.max(xScale, yScale);

// Now get the size of the source bitmap when scaled
float scaledWidth = scale * sourceWidth;
float scaledHeight = scale * sourceHeight;

// Let's find out the upper left coordinates if the scaled bitmap
// should be centered in the new size give by the parameters
float left = (newWidth - scaledWidth) / 2;
float top = (newHeight - scaledHeight) / 2;

// The target rectangle for the new, scaled version of the source bitmap will now
// be
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

// Finally, we create a new bitmap of the specified size and draw our new,
// scaled bitmap onto it.
Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, source.getConfig());
Canvas canvas = new Canvas(dest);
canvas.drawBitmap(source, null, targetRect, null);

return dest;

}

我建议查看9张补丁图片: