位图绘制无缩放在Android [英] Drawing bitmap without scaling on Android

查看:174
本文介绍了位图绘制无缩放在Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想drawbitmap在画布上。在模拟器中,似乎图像模糊。它是可能的机器人自动缩放的位图?我曾尝试禁用缩放比例,如

I was trying to drawbitmap on a canvas. In the emulator, it seems that the image is blurred. Is it likely that the android automatically scaled the bitmap? I did try to disable the scaling such as

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

模糊意味着,如果我有位图像素,我看到喜欢的2x2像素。 soemtimes的像素缺失。所以我认为Android的自动缩放,以适合不同的屏幕。我只是用WVGA以及如何prevent这个比例?谢谢你。

blur means if I have a pixel in the bitmap, I am seeing like 2x2 pixel. soemtimes a pixel is missing. so I assumed that Android automatically scaled it to fit different screens. I am just use WVGA and how do I prevent this scaling? Thanks.

推荐答案

事实上,无论是位图画布有密度特性,绘制一个位图自动缩放的位图,如果在画布和位图的密度不同。

Indeed, both Bitmap and Canvas have a density property, drawing a bitmap automatically scales the bitmap if the densities of the canvas and bitmap differ.

从<一个href="http://developer.android.com/reference/android/graphics/Bitmap.html#setDensity%28int%29">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)来完全禁用这个自动缩放行为。如果加载从资源的位图,将其置于绘制-nodpi 就足够了。

You can call bitmap.setDensity(Bitmap.DENSITY_NONE) to disable this automatic scaling behavior altogether. If you load the bitmap from resources, placing it under drawable-nodpi should be enough.

有关的好奇:这种行为背后的逻辑是 Canvas.cpp (原生部分实施 android.graphics.Canvas ),在 drawBitmap__BitmapFFPaint()方法:

For the curious: the logic behind this behavior is implemented in Canvas.cpp (native part of the android.graphics.Canvas), in the drawBitmap__BitmapFFPaint() method:

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆