【问题标题】:How to draw faster? SurfaceView, SurfaceHolder dirty rect怎么画得更快? SurfaceView、SurfaceHolder 脏矩形
【发布时间】:2012-12-04 10:01:48
【问题描述】:

我对 SurfaceView 有疑问。

函数DrawWave是由一个定时器间隔5ms调用,但实际上两次调用之间需要超过30ms,(我试过删除drawColor,drawPath)。

我尝试了“模式1”和“模式2”,通过使用Dirty rect,希望它可以运行得更快。 Interval一样,都是30ms以上,(这个宽度是800px,eScrX-sScrX

问题 1:使用 SurfaceView/SurfaceHolder 是不是我做错了什么?

问题2:如何提高绘图速度?我希望它能在 10 毫秒内完成绘制。

我的代码:

public class VS_VChannel extends SurfaceView
{//Wave Show Class
    //-------------------------------------------------------------------------------
    private Paint paint = new Paint();  
    private SurfaceHolder sHolder = null;
    private Canvas cvs = null;
    private Path P = new Path();
    //Data source of the wave point Y
    protected VSChannel Channel = null;
    private float drawY;
    //-------------------------------------------------------------------------------
    public VS_VChannel(Context context) 
    {
        super(context);
    }
    //------------------------------------------------------------------------------
    public VS_VChannel(Context context, AttributeSet attrs) 
    {
        super(context, attrs);
        //Bind the wave data source whith AttributeSet name:vs_wave
        String vsName = attrs.getAttributeValue(null, "vs_wave");
        Channel = (VSChannel)VS.GetItem(vsName);//Find the wave data source from collection with name
        //
        paint.setAntiAlias(true);         
        paint.setDither(true);  
        paint.setColor(Channel.getColor());
        paint.setStyle(Paint.Style.STROKE);     
        paint.setStrokeWidth(2);                
        //
        sHolder = this.getHolder(); 
        this.setZOrderOnTop(true);  
        sHolder.setFormat(PixelFormat.TRANSPARENT);
    }
    //------------------------------------------------------------------------------
    /** DrawWave is Call by a timer Interval 5ms, 
     *  BUT actually it need more than 30ms between twice call,(tried delete drawColor, drawPath)
     *  I try as "Mode 1" and "Mode 2", by using Dirty rect, hope it can run faster
     *  The Interval is the same, both more than 30ms,
     *  (The width of this is 800px, and eScrX-sScrX<20px)
     */
    protected void DrawWave(int sScrX, int eScrX)
    {   
        Rect rect = new Rect(sScrX, 0, eScrX+8, getHeight());   //Mode 1    
        //Rect rect = new Rect(0, 0, getWidth(), getHeight());  //Mode 2
        //
        cvs = sHolder.lockCanvas(rect);     
        cvs.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
        cvs.drawPath(P, paint);
        sHolder.unlockCanvasAndPost(cvs);   
    }
    //-------------------------------------------------------------------------------
    protected void GetWaveY()
    {
        drawY = Channel.GetWaveY(getHeight());
    }
    //-------------------------------------------------------------------------------
    protected void MoveTo(float x)
    {
        P.moveTo(x, drawY);
    }
    //-------------------------------------------------------------------------------
    protected void LineTo(float x)
    {
        P.lineTo(x, drawY);
    }
    //-------------------------------------------------------------------------------
    protected void DrawReturn()
    {
        P.reset();//Clear
        P.moveTo(0, drawY);
    }
    //-------------------------------------------------------------------------------
}
//-------------------------------------------------------------------------------

【问题讨论】:

    标签: surfaceview rect surfaceholder


    【解决方案1】:

    如果您在显示线程之上运行,您只能以该线程允许的速度进行处理和绘制。我可能会建议在另一个线程中绘制位图,然后使用表面支架解锁、绘制位图并发布。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-09
      • 1970-01-01
      • 2021-06-03
      • 2012-05-24
      • 2011-07-21
      • 1970-01-01
      相关资源
      最近更新 更多