0

I have a problem with SurfaceView.

Function DrawWave is call by a timer Interval 5ms, BUT actually it needs more than 30ms between two calls, (I tried delete drawColor, drawPath).

I tried 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)

Question 1: Is it something I do wrong, using SurfaceView/SurfaceHolder?

Question 2: What can I do to improve the drawing speed? I hope that it can finish drawing in 10 ms.

My code:

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);
    }
    //-------------------------------------------------------------------------------
}
//-------------------------------------------------------------------------------
tshepang
  • 12,111
  • 21
  • 91
  • 136

1 Answers1

0

If you are running on top of the display thread, you can only process and draw as fast as that thread allows. I might suggest drawing to a bitmap in another thread and then using the surface holder to unlock, draw the bitmap, and post.

freeman_irl
  • 158
  • 1
  • 10