gpt4 book ai didi

android - 在 Android 上不缩放绘制位图

转载 作者:太空宇宙 更新时间:2023-11-03 11:44:36 28 4
gpt4 key购买 nike

我试图在 Canvas 上绘制位图。在模拟器中,图像似乎很模糊。 android可能会自动缩放位图吗?我确实尝试禁用缩放,例如

< supports-screens android:smallScreens="false" android:normalScreens="true" 
android:largeScreens="false" android:xlargeScreens="false" android:anyDensity="true" />

blur 意味着如果我在位图中有一个像素,我看到的是 2x2 像素。有时会丢失一个像素。所以我假设 Android 会自动缩放它以适应不同的屏幕。我只是使用 WVGA,如何防止这种缩放?谢谢。

最佳答案

事实上,BitmapCanvas 都有密度属性,如果 Canvas 和位图的密度不同,绘制位图会自动缩放位图。

来自 Bitmap.setDensity()文档:

Specifies the density for this bitmap. When the bitmap is drawn to a Canvas that also has a density, it will be scaled appropriately.

您可以调用 bitmap.setDensity(Bitmap.DENSITY_NONE) 来完全禁用这种自动缩放行为。如果您从资源中加载位图,将其放在 drawable-nodpi 下就足够了。

出于好奇:此行为背后的逻辑在 Canvas.cpp(android.graphics.Canvas 的 native 部分)中实现,在 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();
}
}

关于android - 在 Android 上不缩放绘制位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13115006/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com