简体   繁体   English

Android 缩放画布不起作用

[英]Android scale canvas does not working

i'm trying to make a photo crop app and i want to load a large size image and scale it to to 480 Width and 800 Height (keep aspect ratio) but the problem is if i scale bitmap it shrinks image and when i save it the result get low quality so i decided to scale its canvas to keep bitmap original size.我正在尝试制作一个照片裁剪应用程序,我想加载一个大尺寸的图像并将其缩放到 480 宽和 800 高(保持纵横比)但问题是如果我缩放位图它会缩小图像,当我保存它时结果质量很低,所以我决定缩放它的画布以保持位图的原始大小。 i need to keep original size but scale the image to fit in screen?我需要保持原始大小但缩放图像以适应屏幕?

here is my code:这是我的代码:

public void setBitmap(Bitmap bitmap) {

finalBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
myCanvas = new Canvas(finalBitmap);
myCanvas.drawBitmap(bitmap, 0, 0, null);
myCanvas.scale(scaleFactor,scaleFactor);
isLoaded = true;
}

draw method:绘制方法:

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    if (isLoaded)
        canvas.drawBitmap(finalBitmap, 0, 0, null);

}

i changed scalefactor with 0.9f ,480,... but non of them had effect on bitmap我用 0.9f ,480 更改了比例因子,...但没有一个对位图有影响

Your code should look like this你的代码应该是这样的

canvas.save();
canvas.scale(mScaleFactor, mScaleFactor);
canvas.drawBitmap(bitmap, 0, 0, null);
canvas.restore();

Another option is to use the drawBitmap that does the scaling for you with the following call canvas.drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)另一种选择是使用 drawBitmap 为您进行缩放,通过以下调用canvas.drawBitmap (Bitmap bitmap, Rect src, Rect dst, Paint paint)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM