位图不绘制抗锯齿 [英] Bitmap not drawn anti-aliased

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

问题描述

我有一个自定义的查看总是绘制位图在一定的旋转。我覆盖的OnDraw 法,旋转画布并绘制位图的消除锯齿涂料

I have a custom View that always draws a Bitmap at a certain rotation. I overwrite the onDraw method, rotate the Canvas and draw the bitmap with an anti-aliased Paint.

public RotatedImageView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    someBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.placeholder);
}

@Override
protected void onDraw(Canvas canvas) {

    // Save and rotate canvas
    canvas.save();
    canvas.rotate(3F, getWidth() / 2, getHeight());

    // Draw the icon
    Paint p = new Paint(Paint.ANTI_ALIAS_FLAG);
    canvas.drawBitmap(someBitmap, 0, 0, p);
    canvas.drawRoundRect(new RectF(75, 50, 225, 200), 10, 10, p);

    // All done; restore canvas
    canvas.restore();
}

不过,我总是对位图锯齿边缘。需要注意的是roudned矩形得到很好的消除锯齿边缘。此外,当我申请 p.setFilterBitmap(真); 这工作(位图表面被过滤/平滑)正确。我失去了一些东西?

However, I always get jagged edges on the Bitmap. Note that the roudned rectangle gets nicely anti-aliased edges. Also, when I apply p.setFilterBitmap(true); this works (the bitmap surface gets filtered/smoothed) correctly. Am I missing something?

下面是一个最小的Andr​​oid项目与一个屏幕的孤立的例子,显示了绘制非反锯齿位图,从资源加载查看:<一href="https://bitbucket.org/erickok/antialiastest">https://bitbucket.org/erickok/antialiastest

Here's a minimal Android project with isolated example of one screen that shows the View that draws the non-anti-aliased Bitmap, loaded from a resource: https://bitbucket.org/erickok/antialiastest

更新:我也试过如下:

    Paint p = new Paint();
    p.setAntiAlias(true);
    p.setFilterBitmap(true);
    p.setDither(true);
    canvas.drawBitmap(someBitmap, 0, 0, p);

但是,这并不利于为 setFilterBitmap 过滤表面;它没有消除锯齿边缘。此外, setAntiAlias​​ 不一样直接设置标志在油漆的构造。如有疑问,请尝尝我最小的测试项目。非常感谢您的帮助!

But this doesn't help as setFilterBitmap filters the surface; it does not anti-alias the edges. Also, setAntiAlias does the same as directly setting the flag in the Paint constructor. If in doubt, please try my minimal test project. Thanks so much for any help!

推荐答案

我发现,为什么我没有得到任何抗锯齿:硬件加速。当我(通过纯属意外)测试,在我的Nexus One突然的工作,而我的Nexus 7没有。原来, ANTI_ALIAS_FLAG 不执行任何操作时,画布使用硬件加速绘制。有了这种洞察力,我发现<一href="http://stackoverflow.com/questions/9730249/canvas-antialiasing-with-hardware-acceleration-android-api-11-and-later"标题=关于计算器罗曼盖伊的回答>在计算器罗曼盖伊的答案,回答我的问题。

I have found why I did not get any anti-aliasing: hardware acceleration. After I (by pure accident) tested on my Nexus One it suddenly worked, while my Nexus 7 didn't. Turns out that the ANTI_ALIAS_FLAG does nothing when the Canvas is drawn using the hardware acceleration. With that insight I found Romain Guy's answer on StackOverflow which answers my question.

解决方案似乎是:增加了透明边框的每一个位图(手动或飞)或做一些与着色器。他们都不是很吸引我。相反,我禁用硬件加速在我的自定义视图使用 setLayerType 方法。请注意,这是可用的,因为API层11,但(没有意外),你将不必处理之前,任何形式的硬件加速。所以,我已经加入,在视图中的构造函数:

Solutions seem to be: adding a transparent border to every bitmap (manually or on the fly) or 'do something with shaders'. Neither are very appealing to me. Instead I disabled hardware acceleration on my custom view using the setLayerType method. Note that this is available since API level 11, but (not by accident) you won't have to deal with hardware acceleration before any way. So I have added, in the view constructor:

    if (android.os.Build.VERSION.SDK_INT >= 11) {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
    }

我已经更新了上面提到的开源项目在BitBucket上为那些有兴趣。

I've updated the above referenced open-source project on BitBucket for those interested.

这篇关于位图不绘制抗锯齿的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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