Pro Yearly is on sale from $80 to $50! »

Book Reader Implementation Notes with Android ViewPager 2016

9278c3a06b8d8752fb913dea93f959c1?s=47 FUJI Goro
April 16, 2016

Book Reader Implementation Notes with Android ViewPager 2016

9278c3a06b8d8752fb913dea93f959c1?s=128

FUJI Goro

April 16, 2016
Tweet

Transcript

  1. ಡॻը໘࣮૷ͷࣄ݅฽ ~ Android ViewPagerฤ ~ 2016/4/15 ిࢠॻ੶ษڧձ FUJI Goro (gfx)

  2. ࣗݾ঺հ

  3. ࢀর࣮૷ • ͜ͷࢿྉͷࢀর࣮૷͸ҎԼʹ͋Γ·͢ • https://github.com/gfx/TinyPdfReader • ίΞϩδοΫ͸200ߦ͘Β͍ • DeploygateͰAPKΛ഑৴͍ͯ͠·͢ •

    https://dply.me/xntdx6
  4. ViewPagerجૅ஌ࣝ • AOSPͷҰ෦ (support library) • εϫΠϓͰࠨӈʹҠಈ͢ΔViewGroup • λϒҰൠɺಡॻը໘ͳͲʹ࢖͏ •

    ϖʔδͷਐΉํ޲͸LTR(left-to-right)
  5. ϑΝΠϧ#1 ϖʔδૹΓ͕΋ͬ͞Γ͢Δ

  6. ϖʔδૹΓ͕஗͍ • ʮBookʯʢࠓճ͸PDFʣ͔Β1ϖʔδऔΓग़ ͯ͠bitmapΛੜ੒͢Δͷʹ100 ~ 300sec͔͔ Δ • ͢΂ͯUIεϨουͰॲཧ͢ΔͱɺϖʔδૹΓ Ͱʮࢦ͕Ҿ͔͔ͬΔʯײ͕͢͡Δ

  7. ViewPagerͷ࣮૷ • ViewPager͸ɺʮݱࡏͷϖʔδʯͱʮલޙͷ ϖʔδʯͷ3ϖʔδ෼ͷviewΛߏங͢Δ • ʮ࣍ͷϖʔδʯʹҠಈ͢Δલʹͦͷ࣍ͷϖʔ δͷviewΛߏங͢ΔͷͰɺͦ͜Ͱ਺ඦϛϦ͔ ͔ΔͱʮϖʔδૹΓ͕٧·ΔʯΑ͏ʹײ͡Δ

  8. #1 ͷղܾ • ViewPagerʹҰ୴ۭͷviewΛ౉͠ɺbitmapੜ ੒͸όοΫάϥ΢ϯυεϨουͰߦ͏ • bitmap͕ੜ੒Ͱ͖ͨΒviewʹ౉ͯ͠ඳը͢ Δɺ͜ͷͱ͖alpha஋ʢಁ໌౓ʣͰΞχϝʔ γϣϯͯ͠ʮ;Θͬͱʯදࣔ͞ΕΔΑ͏ʹ͢ Δͱࢹ֮తʹ༏ͯ͘͠Α͍

  9. PdfPagerAdapter: 1ท෼ͷॲཧ ϓϨʔεϗϧμ7JFXΛͭ͘Δ ඇಉظͰCJUNBQΛϩʔυ͢Δ ݟ։͖࣌͸ϖʔδ෼ϩʔυ

  10. RenderBitmapTask: bitmapੜ੒ ݟ։͖ରԠͷͨΊͪΐͬͱෳࡶ جຊ͸1EG3FOEFSFSSFOEFS ΛݺͿ͚ͩ

  11. RenderBitmapTask ϑϨʔϜ͔ͭͬͯ;Θͬͱग़ݱͤ͞Δ

  12. Ͳ͜·Ͱ࠷దԽ͢΂͖͔ • 3ϖʔδ෼ (දࣔϖʔδ+લޙϖʔδʣͷʮ४උʯ͕1ϑ ϨʔϜ(16ms)ҎԼͳΒे෼ߴ଎ • 1ϑϨʔϜҎԼͰͷ࠷దԽ͸અిޮՌ͘Β͍ • bitmapੜ੒͸30msͱ300msͰUXʹͦΕ΄Ͳҧ͍͸ͳ͍ •

    500msΛ௒͑ΔΑ͏ͳΒ࠷దԽ͢΂͖͚ͩͲɺͦΕҎ Լʹ͢Δ౒ྗ͸͋·Γҙຯ͕ͳ͍
  13. File #2 طಡ؅ཧ͍ͨ͠

  14. طಡ؅ཧ • ΞϓϦ಺ͷॻ੶͝ͱʹɺͲ͜·ͰಡΜ͔ͩه࿥ ͍ͨ͠ • ViewPager#getCurrentItem() ͰҐஔΛऔಘ • 1ը໘1ϖʔδͷ࣌͸؆୯ɺ໰୊͸1ը໘2ϖʔ δʢݟ։͖ʣͷͱ͖

    • ʮॻ੶σʔλͰͷҐஔʯʹม׵͢Δඞཁ͕͋Δ
  15. ʮҐஔʯʹؔ͢Δ໋໊نଇ • ໋໊نଇΛͭ͘Βͳ͍ͱࢮ͵ • ViewPagerͷݱࡏ஋: position • ॻ੶σʔλͰͷҐஔ: pageIndex •

    Ϧϑϩʔͷ͜ͱ͸ߟ͑ͳ͍΋ͷͱ͢Δ
  16. QPSUSBJUͷͱ͖͸ͦͷ·· JT1PSUSBJU ͡Όͳ͍ͱ͍͚ͳ͍ͷ͸ 7JFX1BHFSͷόάͱࢥΘΕΔ MBOETDBQF ݟ։͖ ͷͱ͖͸ ഒͯ͠දࢴ෼ͷΛҾ͘ ਖ਼֬ʹٯͷม׵Λߦ͏ 7JFX1BHFSͷҐஔ͔Βॻ੶Ґஔʹม׵

    ॻ੶Ґஔ͔Β7JFX1BHFSͷҐஔʹม׵
  17. File #3 ViewPager͕ࢥ͍௨Γʹಈ͍ ͯ͘Εͳ͍ΜͰ͢…

  18. VPΛӈ͔ΒࠨʹҠಈͤ͞Δ • ViewPager͸left-to-rightͳͷͰɺright-to-left ͷॻ੶Λදࣔ͢Δͱ͖ʹͦͷ··࢖͏ͱࢮ͵ • …ͷͰRVPͱ͍͏ϥΠϒϥϦΛ࡞ͬͨ • https://github.com/gfx/ReversibleViewPager • rvp.setReversed(true)

    ͢Δ͚ͩʂ
  19. ViewPagerͰϐϯνΠϯɾΞ΢τ͢ ΔͱIllegalArgumentException • ViewPagerͷόάͬΆ͍ • https://code.google.com/p/android/issues/detail?id=64553 • ViewPager Λܧঝͯ͠ super.onInterceptTouchEvent()

    ͷྫ֎ΛѲΓ௵ ͤʂ • RVP͸͜ͷରԠ͕ೖ͍ͬͯΔ
  20. VP#onSavedInstanceState() • ը໘ճస࣌ʹঢ়ଶΛอଘ͢Δ࣌ʹݺ͹ΕΔ • configuration͔ΒͱΕΔorientation͕ʮݱࡏͷ σόΠεͷํ޲ʯͰ͸ͳ͘ʮ͜Ε͔Β޲͔͏ํ޲ʯ ʹͳΔ • ͦ͜Ͱલड़ͷ !isPortrait()

    ʹͳͬͨ • ͜Ε͸ਖ਼͍͠ͷ͔…ಈ͍ͯΔ͚Ͳ…
  21. VP#onCreateView()ͱঢ়ଶ෮ؼ • onSavedInstanceState()Ͱอଘͨ͠ঢ়ଶΛ෮ؼͤ͞Δ • ͔͠͠ɺonCreateView()Ͱ vp.setCurrentItem() ͯ͠΋ແ ࢹ͞ΕΔʂʂʂ • onCreateView()ͷ͋ͱʹFragment͕view

    (VPؚΉ) ͷঢ় ଶΛ෮ؼ͢Δ͔ΒɺΒ͍͠ • onCreateView()Ͱঢ়ଶΛಡΈͩ͠ɺonResume()Ͱ vp.setCurrentItem()͢Δ͜ͱʹͨ͠
  22. RVPͷ՝୊ • RVPΛ׬ᘳʹ࣮૷͢Δͷ͕೉͍͠ • v1.0.2࣌఺Ͱ΋ PagerTitleStrip ͱͷڠௐ͕͏ ·͍͔͘ͳ͍όά͕͋Δ • RTL͸AOSPଆͰରԠͯ͘͠ΕΔͱخ͍͠

    • ໘౗ͳ͜ͱ͸GoogleͷΤϯδχΞʹ೚͍ͤͨ
  23. That's all.

  24. https://github.com/gfx/ TinyPdfReader