0

I am implementing an ebook reader for android. I have done the pagination part successfully. But it takes a long time to load the book. what I did was, get the raw html and break into chapters and store them in a array. Then get the entire string and using Html.fromHtml I removed the html tags (because I am using view pager and need to get the no of pages, for this reason we need to remove the unnecessary strings first). Then according to this answer I am breaking into pages. (The logic was to get a sub string and check the string height is greater than the screen height, I am checking this condition at every space in the string).

I have used epub-lib library and jsoup for this project. I am using dynamic textviews and Imageviews for this.

Now I need to optimize this process. How to do this? My idea is to load and show the initial pages to users and while the user going through them the rest of the content should be paginated and loaded using a background process.

Is this possible? I am happy to get any other suggestions as well.

Thanks

Community
  • 1
  • 1
chathura
  • 3,362
  • 6
  • 41
  • 68

1 Answers1

1

Yes that should be possible. I had a similar situation where I had a ListView that potentially listed thousands of items from a database. Instead of loading everything, I simply read the first 9 items, displayed them and then had a listener for when the scrolling had reached the bottom of the list and then updated my listview to include the next 9 items.

Also, from a performance perspective, if your book is very large, I'd suggest doing that sort of parsing work immediately after you've downloaded the file in an AsyncTask. I would break down the pages of the book into different objects (i.e. page 1, 2, 3 in a "page" object) and then when you need to display that page, you simply read and display that object. This would also be beneficial if the user opens the ebook multiple times, as you won't have to parse the entire html file again each time that ebook is opened.

The logic would be as follows:

  1. Download the text html
  2. Remove the formatting and get raw text
  3. Break the text into pages
  4. Save each page (or a few pages together) in a "Page" object
  5. Display the first "Page" object
  6. Listener for scrolling to the end of the page
  7. Display the next "Page" object
  8. Repeat Steps 6, 7
erad
  • 1,766
  • 2
  • 17
  • 26
  • currently I used an object to represent a single page which contains textviews and imageviews. So to represent the entire book I have arrayList of that single page object – chathura Sep 14 '14 at 03:50
  • Yes that's what I would do. But to improve performance, I wouldn't add to that ArrayList until the current page has scrolled to the bottom. (or 1 page before the bottom if you want to make the transition between Page objects appear more seamless) Also, I would save these objects in a separate file or a SQLite Database so you don't need to do the parsing work multiple times. – erad Sep 14 '14 at 03:54
  • another things is that, when the user rotates the device the same process has to be done from the beginning (to fit the text into that screen) and this also takes time. what is your suggestion for this? – chathura Sep 14 '14 at 04:06
  • I also agree with your answer, but if a user wants to jump into a particular page in the latter part of the book, it is not possible. right? – chathura Sep 14 '14 at 04:09
  • @chathura2020 I would define the page size so that it's screen size independent. If the page size exceeds the screen size, yes you would need to do a little formatting to ensure that the text fits on the changed screen size. (You may want to let the user scroll if the page is larger than the screen size) – erad Sep 14 '14 at 04:18
  • As far as your page jumping, I would implement a SQLite Database that stores all my page objects in one column and the page number in a different column. If a user jumps to a page, I'd simply populate the ArrayList with that specific page by using a query for that page number. Scrolling would be handled similarly by modifying the ArrayList to include the pages before and after that page. In my page object, I would also include a variable called `int pageNumber` so that I can know what page it is regardless of the ArrayList size. – erad Sep 14 '14 at 04:22