Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/200.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
将位图纵横比设置为centerFit Android_Android_Canvas_Bitmap_Drawing_Aspect Ratio - Fatal编程技术网

将位图纵横比设置为centerFit Android

将位图纵横比设置为centerFit Android,android,canvas,bitmap,drawing,aspect-ratio,Android,Canvas,Bitmap,Drawing,Aspect Ratio,我已经寻找了很多,但没有得到适当的解决办法! 我的android应用程序中有一个绘图区域,我想从相机/画廊中拍摄图像,并将其设置为画布的背景。 我在从照相机/画廊拍照后得到位图,并将位图传递给此方法,使其纵横比与drawView相匹配,并保持其纵横比。 这里newHeight/newWidth是我的drawView的高度和宽度 public Bitmap scaleCenterCrop(Bitmap source, int newHeight, int newWidth) { int s

我已经寻找了很多,但没有得到适当的解决办法! 我的android应用程序中有一个绘图区域,我想从相机/画廊中拍摄图像,并将其设置为画布的背景。 我在从照相机/画廊拍照后得到位图,并将位图传递给此方法,使其纵横比与drawView相匹配,并保持其纵横比。 这里newHeight/newWidth是我的drawView的高度和宽度

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.min(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.drawColor(Color.WHITE);
    canvas.drawBitmap(source, null, targetRect, null);

    return dest;
}
它在mt Htc m7上运行良好,但在我的galaxy tab4上,它使图像拉伸如此光滑,如下图所示:

有人知道我可以通过什么方法将位图设置为我的drawView吗

Bitmap backgroundBitMap= c.scaleCenterCrop(bmp, drawView.getHeight(), drawView.getWidth());



 String tempPath = getPath(selectedImageUri, MainActivity.this);
  backgroundBitMap=  c.rotateImage(backgroundBitMap,tempPath);

 drawView.setBackgroundDrawable(new BitmapDrawable(backgroundBitMap));
但是保持它的外观不变。实际上,我想要位图的CenterFit功能。
任何帮助都将不胜感激!多亏了它的复杂性,使用Canvas.drawBitmap并使用setRectRect设置矩阵参数method@pskink谢谢你的回答。我也试过,但没用。你能提供一个有效的代码片段吗?是的,它工作得很好。我用了好几次,只需创建适当的矩阵并使用it@pskink“矩阵”只有“中心”选项,可使位图从中心裁剪。但我想要的是中心拟合的功能,其中位图保持其纵横比,并根据视图中的拟合度进行缩放。你知道有什么方法可以做到这一点吗?中心填充放大/缩小图像保留纵横比,它不会裁剪图像