实际上,两者Bitmap
都Canvas
具有密度属性,如果画布和位图的密度不同,则绘制位图会自动缩放位图。
来自Bitmap.setDensity()文档:
指定此位图的密度。当位图被绘制到也有密度的 Canvas 上时,它会被适当地缩放。
您可以调用bitmap.setDensity(Bitmap.DENSITY_NONE)
以完全禁用此自动缩放行为。如果您从资源中加载位图,将其放置在下面drawable-nodpi
就足够了。
对于好奇的:这种行为背后的逻辑是在Canvas.cpp
(的本机部分android.graphics.Canvas
)中实现的,在drawBitmap__BitmapFFPaint()
方法中:
static void drawBitmap__BitmapFFPaint(JNIEnv* env, jobject jcanvas,
SkCanvas* canvas, SkBitmap* bitmap,
jfloat left, jfloat top,
SkPaint* paint, jint canvasDensity,
jint screenDensity, jint bitmapDensity) {
SkScalar left_ = SkFloatToScalar(left);
SkScalar top_ = SkFloatToScalar(top);
if (canvasDensity == bitmapDensity || canvasDensity == 0
|| bitmapDensity == 0) {
if (screenDensity != 0 && screenDensity != bitmapDensity) {
SkPaint filteredPaint;
if (paint) {
filteredPaint = *paint;
}
filteredPaint.setFilterBitmap(true);
canvas->drawBitmap(*bitmap, left_, top_, &filteredPaint);
} else {
canvas->drawBitmap(*bitmap, left_, top_, paint);
}
} else {
canvas->save();
SkScalar scale = SkFloatToScalar(canvasDensity / (float)bitmapDensity);
canvas->translate(left_, top_);
canvas->scale(scale, scale);
SkPaint filteredPaint;
if (paint) {
filteredPaint = *paint;
}
filteredPaint.setFilterBitmap(true);
canvas->drawBitmap(*bitmap, 0, 0, &filteredPaint);
canvas->restore();
}
}