我正在使用cordova-plugin-sketch
插件,而我在Android上遇到问题,每次渲染/保存时图像的质量都会下降。其中一位维护者已经足够好提供一些链接来帮助解决问题,但到目前为止我还没有成功。我对Android开发相当新,这是问题的一部分。
插件: https://github.com/blinkmobile/cordova-plugin-sketch
问题: https://github.com/blinkmobile/cordova-plugin-sketch/issues/3
我已尝试在inDensity
上设置inTargetDensity
和BitmapFactory.Options
的不同选项,但未找到任何可提高质量的设置。此外,我想这些设置的有效性会根据使用的设备而改变。
正在修改的文件:TouchDrawActivity.java
我尝试过改变:
mBitmap = createScaledBitmap(mBitmap, mTdView.mCanvas.getWidth(),
mTdView.mCanvas.getHeight(),ScalingLogic.FIT);
//mBitmap = Bitmap.createScaledBitmap(mTdView.mCanvas.getWidth(),mTdView.mCanvas.getHeight(),false);
现在使用此代码:
public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight, scalingLogic);
Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(scaledBitmap);
canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
return scaledBitmap;
}
public static Rect calculateSrcRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
if (scalingLogic == ScalingLogic.CROP)
{
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect)
{
final int srcRectWidth = (int) (srcHeight * dstAspect);
final int srcRectLeft = (srcWidth - srcRectWidth) / 2;
return new Rect(srcRectLeft, 0, srcRectLeft + srcRectWidth, srcHeight);
}
else
{
final int srcRectHeight = (int) (srcWidth / dstAspect);
final int scrRectTop = (int) (srcHeight - srcRectHeight) / 2;
return new Rect(0, scrRectTop, srcWidth, scrRectTop + srcRectHeight);
}
}
else
{
return new Rect(0, 0, srcWidth, srcHeight);
}
}
public static Rect calculateDstRect(int srcWidth, int srcHeight, int dstWidth, int dstHeight, ScalingLogic scalingLogic)
{
if (scalingLogic == ScalingLogic.FIT)
{
final float srcAspect = (float) srcWidth / (float) srcHeight;
final float dstAspect = (float) dstWidth / (float) dstHeight;
if (srcAspect > dstAspect)
{
return new Rect(0, 0, dstWidth, (int) (dstWidth / srcAspect));
}
else
{
return new Rect(0, 0, (int) (dstHeight * srcAspect), dstHeight);
}
}
else
{
return new Rect(0, 0, dstWidth, dstHeight);
}
}
不幸的是,图像质量下降似乎几乎不相同。