Android的,最快的方法绘制位图以帆布 [英] Android, fastest way to draw a bitmap to canvas

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

问题描述

只是想知道最快的方法是绘制位图以帆布?

目前我有一个位图(和帆布绘画),我使用双缓冲绘图电话,然后当我画到画布上有应用1px的帆布翻译一个滚动效应。仅此一项将减少帧率从60+ FPS至〜40,相当一炮打响。我不是用surfaceView(或GLSurfaceView)的时刻,但只是想知道,如果即时通讯丢失任何会提高速度。以下

的OnDraw()code

  @覆盖
    公共无效的OnDraw(帆布油画)
    {
        //更新FPS文本
        mFpsTracker.frameTouch();

        如果(mBufferedBitmap == NULL)
        {
            mBufferedBitmap = Bitmap.createBitmap(的getWidth(),的getHeight(),Bitmap.Config.ARGB_4444);
            mBufferedCanvas =新的Canvas(mBufferedBitmap);
        }

        paint.setColor(Color.BLUE);
        mBufferedCanvas.drawLine(0,的getHeight(),的getWidth(),的getHeight(),漆);
        mBufferedCanvas.translate(0,-1);


    canvas.drawBitmap(mBufferedBitmap,0,0,NULL);

    //得出的FPS
    mTextPaint.setColor(Color.WHITE);
    canvas.drawText(mFpsTracker.getFPSString(),40,40,mTextPaint);


    无效();
}
 

解决方案

请参阅的这个通过罗曼盖伊博客文章。

一个视频版本可以这里

不要使用ARGB_4444了。这是pcated德$ P $。每个像素只分配每信道的4位(因此而得名)。 ARBG_8888提供16,777,216种颜色,代替ARBG_4444的4096,但使用4个字节每个像素,而不是2

在姜饼的Andr​​oid取得了ARGB_8888为表面的标准格式,每因为它的过程增加了内存分配。

这是更有效地设置您的窗口的和(假设你使用流线型 SurfaceView )SurfaceHolder的格式< A HREF =htt​​p://developer.android.com/reference/android/graphics/PixelFormat.html#RGBA_8888相对=nofollow> RGBA_8888 。这避免了格式的变化,这是明显慢。

其他的技巧包括:

  • 在限alpha组合,因为这需要从Skia的比较昂贵的混合。
  • 在请求位图选项是preFER的ARGB_8888 配置并禁用抖动。
  • 删除窗口背景如果可能的话
  • 启用硬件加速,但要注意不支持的操作

在2.1设备,我能够得出至少300位图在屏幕上以50 fps的。

Just wondering what the fastest way is to draw a bitmap to canvas?

Currently I have a bitmap (and canvas for drawing) which i use to double buffer drawing calls, and then when i draw to canvas have a scrolling effect by applying a 1px canvas translation. This alone will reduce the framerate from 60+ FPS to ~40, quite a hit. Im not using surfaceView (or GLSurfaceView) at the moment but just wondering if im missing anything that would improve the speed. onDraw() code below

@Override
    public void onDraw(Canvas canvas)
    {
        //update fps text
        mFpsTracker.frameTouch();

        if(mBufferedBitmap == null)
        {
            mBufferedBitmap = Bitmap.createBitmap(getWidth(), getHeight(), Bitmap.Config.ARGB_4444);
            mBufferedCanvas = new Canvas(mBufferedBitmap);
        }

        paint.setColor(Color.BLUE);
        mBufferedCanvas.drawLine(0, getHeight(), getWidth(), getHeight(), paint);
        mBufferedCanvas.translate(0, -1);


    canvas.drawBitmap(mBufferedBitmap, 0, 0, null); 

    //draw fps
    mTextPaint.setColor(Color.WHITE);
    canvas.drawText(mFpsTracker.getFPSString(), 40, 40, mTextPaint);


    invalidate();       
}

解决方案

please see this blog post by Romain Guy.

A video version is available here.

Don't use ARGB_4444 anymore. It is deprecated. Each pixel is only allocated 4 bits per channel (hence the name). ARBG_8888 offers 16,777,216 colors instead of ARBG_4444's 4,096, but uses 4 bytes per pixel instead of 2.

In Gingerbread, Android made ARGB_8888 the standard format for Surface and increased memory allotment per process because of it.

It is more efficient to set your Window's and (assuming you are using streamlined SurfaceView) SurfaceHolder's format to RGBA_8888. This avoids format changes which are noticeably slower.

Other tips include:

  • Limit alpha compositing, as this requires comparatively expensive blending from Skia.
  • Request Bitmap Options that prefer the ARGB_8888 Config and disable dithering.
  • Remove the Window background if possible.
  • Enable hardware acceleration, but beware of unsupported operations.

On a 2.1 device, I am able to draw at least 300 bitmaps on-screen at 50 fps.

这篇关于Android的,最快的方法绘制位图以帆布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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