B4A Library PageTurnView library providing a page turn animation

Discussion in 'Additional libraries, classes and official updates' started by agraham, Aug 12, 2011.

  1. agraham

    agraham Expert Licensed User

    This library displays bitmap images and provides a page turn animation to allow the user to "turn" the pages.

    This library wraps an Android view which written by Harri Smått and released as open source under the Apache 2.0 licence. It was located for Basic4android use by forum member Smee.

    There is no credit due to me for the functionality of this library. As Harri wrote it as a normal Android view I have merely wrapped Erel's view structure around it to make it accessible to Basic4android.

    EDIT :- Version 1.1 posted. See post #6 for details.

    EDIT :- Version 1.2 posted. See post #10 for details.

    EDIT :- Version 1.3 posted. See post #43 for details.

    EDIT :- Version 1.4 posted. See post #158 for details.

    Attached Files:

    Last edited: Aug 11, 2019
  2. Djembefola

    Djembefola Active Member Licensed User

    Thank you, Andrew, Harri and Smee.
  3. klaus

    klaus Expert Licensed User

    Hi Andrew,
    Excellent and impressive work :sign0098:.

    What is the ba parameter PageTurner.ba ?

    I would suggest to replace, in the autocomplete, the argx names by the names like in the other views.
    Initialize(arg1 As String) > Initialize(EventName As String)
    Invalidate3(arg0 As android.graphics.Rect) > Invalidate3(Rect As android.graphics.Rect)
    Invalidate3(arg0 As Int, arg1 As Int, arg2 As Int, arg3 As Int) > Invalidate3(Left As Int, Top As Int, Right As Int, Bottom As Int)

    Best regards.
  4. agraham

    agraham Expert Licensed User

    It's an internal field inherited from the Basic4android view wrapper that unfortunately is being exposed by the doclet that generates the xml.
    I can't because they are not defined in my library but inherited from the Basic4android view wrapper so the doclet that produces the xml does not have the source code to get the names of the parameters.

    Incidentally the present demo is stupidly broken under pause and resume again by me making a beginners mistake and re-initializing the view in Activity_Resume instead of Activity_Create. I will post a modified library and demo in a little while.
    Cableguy likes this.
  5. klaus

    klaus Expert Licensed User

    Thank's for the information.

    Best regards.
  6. agraham

    agraham Expert Licensed User

    A quick update to version 1.1, mainly because the demo failed under certain circumstances as I had stuff in Activity_Resume that should have been in Activity_Create. Stupid beginners mistake! :sign0161:

    After playing on my Xoom i have also added a smoothness parameter to Initialize to specify the number of line segments on the curl of a page as the default of 10 was a little bit obvious on a high resolution screen.

    If you get a force close the most likely reason is that a bitmap allocation within the OpenGL code caused an out of memory exception. It can't be trapped normally with a Basic4android Try - Catch as it is not occurring on the main thread but on the rendering. I will look at trapping it within the OpenGL as I do this in my own OpenGl libraries but so far I have modified the code as little as possible to make it easy to update if Harri produces a newer version. In the meantime I have added a RecoverMemory method that forces a garbage collection. If you get force closes you could play with using this in the GetBitmap event and see if it helps.
  7. Fox

    Fox Active Member Licensed User


    Very nice work the libary. I'm not on my home pc so i can't try it but a question is it possible to draw than pics on the page and if yes how. You make an tutorial for this ?

    But very nice and useful think :sign0188: sounds cool
  8. agraham

    agraham Expert Licensed User

    It displays bitmaps so you can display whatever you want. I don't think it needs a tutorial, the demo should tell you all you need to know.
  9. Fox

    Fox Active Member Licensed User

    ok then i will look into if i'm at home...
  10. agraham

    agraham Expert Licensed User

    Another quick update to version 1.2

    Thanks to Erel for sending me the source code for the view wrapper in Basic4android the xml defficiencies that Klaus pointed out above are fixed now and the xml has complete help for all methods.

    I have also poked around inside Harris' source code and implemented some explicit bitmap handling which seems to have eliminated the out of memory errors (so far!) I was experiencing. Android suffers from bitmap memory problems in the same way as Windows Mobile. Because bitmaps are large native objects you can't just leave large ones lying round until the garbage collector removes them as you can run out of native memory before managed memory.

    As an insurance I have added a RecycleBitmap method which does much the same as Dispose did for a bitmap in Basic4ppc. You might need to use this to manage the bitmaps you feed the PageTurnView with in a real application. I left RecoverMemory in just in case of need. I don't force a garbage collection in the new bitmap handling so I would expect that you will never need to either - but it's there.
  11. Smee

    Smee Well-Known Member Licensed User

    Many Thanks Andrew,

    I am about to download this and give it a whirl. Been away for a little while.

    I am trying to load a picture jpg on the page but without much success. Is it possible?

    Last edited: Aug 12, 2011
  12. hdtvirl

    hdtvirl Active Member Licensed User

    Great work.

    I was out walking the dog yesterday and wondering how I might do exactly what you guys have done with this Library, I had performed something similar using GDI+ in VB.net and I was trying to figure out how I could transfer the routines to B4A. So I was totally amazed to see it had been done.

    So tomorrow morning and I am taking the dog out again to sort out another problem !! I never knew B4A was this easy, just think about the problem and somebody will post the solution. I am only joking of course.

    I know you guys put a lot of thought and effort in to everything you post and I am very grateful as I am sure others are too .

    I have tried your example on my Acer Iconia and it works ‘FAB’ !!.

    Can I use this library in my application as part of V4B ?

    Amazing, what a community !!!!!!, I hope one day I can make similar contributions and help other developers like this community does..

  13. Smee

    Smee Well-Known Member Licensed User

  14. agraham

    agraham Expert Licensed User

    Should be. Whack it on a bitmap and give it to the PageTurnView.
  15. Smee

    Smee Well-Known Member Licensed User

    Sorry Andrew, not with you

    The code i changed was this
    bmp.Initialize (File.DirAssets,"26833.jpg")
    '   bmp.InitializeMutable(Width, Height)
    '   cnv.Initialize2(bmp)
    The DestRect was declared in Process_Globals coz the program crashed if it was declared elsewhere.
  16. agraham

    agraham Expert Licensed User

    I don't know what you mean by "V4B" but the licence conditions are in the readme .text file in the archive and are duplicated in the xml help.
  17. hdtvirl

    hdtvirl Active Member Licensed User

    Sorry B4A !!
  18. Smee

    Smee Well-Known Member Licensed User

    This compiles and runs but crashes with error at the last line
    Dim Canvas1 As Canvas
          bmp.Initialize (
    Dim Rect1 As Rect
    i know i am missing the obvious but what?
  19. Fox

    Fox Active Member Licensed User

    Yes an sample for an image on page one and a image on the second page would be good on 1 page i have an image but if i change the side there is no other text...

    But here is the code for image show...

    Sub PageTurner_GetBitmap(Width As Int, Height As Int, Page As Int) As Bitmap 'Called when the Bitmap for the given page number is required. Return the Bitmap
       Dim bmp As Bitmap
    Dim cnv As Canvas
    Dim c As Canvas
    Dim b As Bitmap
    Dim brect As Rect

       bmp.InitializeMutable(Width, Height)
    If page = 0 Then
    "Page 0 Text Center", Width/2180dipTypeface.DEFAULT, 24Colors.White, "CENTER")
    File.DirAssets, "picture.png")
    0,0,b.Width, b.Height)
    Null, brect)
    If page = 1 Then 
    "Page1 Text", Width/2260dipTypeface.DEFAULT, 24Colors.White, "CENTER")
    If Page = PageColors.Length - 1 Then
    "Letzte Seite", Width/2100dipTypeface.DEFAULT, 24Colors.White, "CENTER")
    End If
    End If
    End If
    Return bmp
    End Sub
    koaunglay likes this.
  20. Smee

    Smee Well-Known Member Licensed User

    Thanks Fox

    I tried that sample code but i still get a crash
    at this line
    b.Initialize(File.DirAssets, "26833.png")
    the error log says this

    android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.

    so i dont know whats happening. It is the same error line when i ran my code


    Can't believe i missed this, prolly cause i got the same error with my code. Anyway it works since i changed 26833.png to 26833.jpg
    Last edited: Aug 13, 2011
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice