smooth scrolling in WP7: is it possible ?

Sep 12, 2012 at 10:09 AM
Edited Sep 12, 2012 at 10:20 AM

Hi, I'm trying to achieve smooth scrolling through a number of simple filled rectangles in WP7. The number of rects is principally unlimted (in the model), but the
number displayed in one "frame"/redraw is only about 30. 

while I'm experienceing a fast'n' fluid  scrolling in silverlight , the same procedure executed in WP7 results in a very poor performance, even in the emulator:
The updated bitmap gets not displayed with each update in the Image control.  It's all choppy and painfully slow when scrolling.

Maybe way I'm using it is not optimal:

=> Layout (the canvas inside the scrollviewer is only used to enable/detect the panning/scrolling):

<Grid Width=400 Height=700>
<Scrollviewer>
    <Canvas Width=10000 Height=10000/>
</Scrollviewer>
<Image IsHitTestVisible=false  />
</Grid>




=> Setup:

writeableBmp = BitmapFactory.New(400, 700);
image.Source = writeableBmp;



=> Redraw:

Then, on every scroll change (which is approx every 100 mesc) I'm drawing the rects that a currently visible in the viewport:

writeableBmp.Clear();

foreach (rect in rects){

if (IsVisible(rect)){

  writeableBmp.FillRectangle(left,top,50,50);

}

}

writeableBmp.Invalidate(); //btw: using (writeableBmp.GetBitmapContext(ReadWriteMode.ReadWrite)) does NOT update the bitmap in the image control



Any ideas how to optimize that ?

Thanks.

Coordinator
Sep 13, 2012 at 9:18 AM

You should wrap your calls in a BitmapContext. This will only invalidate all once.

 

using (writeableBmp.GetBitmapContext(ReadWriteMode.ReadWrite))
{
writeableBmp.Clear();

foreach (rect in rects)
{
if (IsVisible(rect))
{
  writeableBmp.FillRectangle(left,top,50,50);

}
}
}

 

 

Sep 13, 2012 at 1:13 PM
Edited Sep 13, 2012 at 1:27 PM

However, after the using block you have to add another " writeableBmp.Invalidate()" , otherwise the Image control will not display the updated bitmap during scrolling...

-->See the demo project  attached.

The second thing I've noticed is that there is a huge performance hit as soon as you do some text rendering:

 

textBlock.Text = "some text";
this.textBitmap = new WriteableBitmap(textBlock, null);

targetBitmap.Blit(
                new Rect(left, top, this.textBitmap.PixelWidth,  
                               this.textBitmap.PixelHeight),
                this.textBitmap,
                new Rect(0, 0, this.textBitmap.PixelWidth,      
                              this.textBitmap.PixelHeight));

 

 

Is there a faster way to render text with writeableBitmap?

Sep 15, 2012 at 4:28 PM

I switched over to XNA, as it is also supported in WP 8 and is much better suited for a scenario where you have to draw text frequently