Upgrade to Pro — share decks privately, control downloads, hide ads and more …

20160415-yabook-study

 20160415-yabook-study

2016年4月15日の「電子書籍アプリ開発者勉強会」の発表資料です。

ARIYAMA Keiji

April 15, 2016
Tweet

More Decks by ARIYAMA Keiji

Other Decks in Technology

Transcript

  1. C-LIS CO., LTD. "MJHO  public enum Align {
 Left,


    Right,
 Center;
 
 public static Align flip(Align align) {
 switch (align) {
 case Left:
 return Right;
 case Right:
 return Left;
 default:
 return Center;
 }
 }
 }
 ϖʔδͷࠨӈ൓స
  2. C-LIS CO., LTD. #PPL  public class Book {
 


    private final Align startPageAlign;
 
 public Align getStartPageAlign() {
 return startPageAlign;
 }
 
 private final List<Page> pageList;
 
 public List<Page> getPageList() {
 return pageList;
 }
 
 public Book(Align startPageAlign, List<Page> pageList) {
 this.startPageAlign = startPageAlign;
 this.pageList = pageList;
 }
 }
 ࠷ॳͷϖʔδͷ഑ஔ
  3. C-LIS CO., LTD. 1BHFʢൈਮʣ  public class Page implements Parcelable

    {
 
 private final int number;
 
 public int getNumber() {
 return number;
 }
 
 private final boolean isSpread;
 
 public boolean isSpread() {
 return isSpread;
 }
 
 private final String fileName;
 
 public String getFileName() {
 return fileName;
 }
 
 public Page(int number, boolean isSpread, String fileName) {
 this.number = number;
 this.isSpread = isSpread;
 this.fileName = fileName;
 }
 } ݟ։͖ϖʔδϑϥά
  4. C-LIS CO., LTD. SFTMBZPVUGSBHNFOU@QBHFYNM  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


    android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">
 
 <io.keiji.mincomisample.paging.PageView
 android:id="@+id/centerPage"
 android:layout_width="match_parent"
 android:layout_height="match_parent"/>
 </LinearLayout>
  5. C-LIS CO., LTD. SFTMBZPVUMBOEGSBHNFOU@QBHFYNM  <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"


    android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="horizontal">
 
 <io.keiji.mincomisample.paging.PageView
 android:id="@+id/leftPage"
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="1"/>
 
 <io.keiji.mincomisample.paging.PageView
 android:id="@+id/rightPage"
 android:layout_width="0dp"
 android:layout_height="match_parent"
 android:layout_weight="1"/>
 </LinearLayout> ϖʔδ഑ஔ͸
 LinearLayoutʹ೚ͤΔ
  6. C-LIS CO., LTD. ϖʔδͷׂΓ෇͚Λॲཧ͢Δ "CT1BHF"EBQUFS  public abstract class AbsPageAdapter

    {
 
 final Book book;
 
 protected AbsPageAdapter(@NonNull Book book) {
 this.book = book;
 }
 
 public final PageContainer getPage(int position) {
 // ϖʔδͷฒͼΛ൓స
 return getPageInternal(getCount() - position - 1);
 }
 
 public abstract int getCount();
 public abstract PageContainer getPageInternal(int position);
 
 }

  7. C-LIS CO., LTD. 1BHF$POUBJOFSʢൈਮʣ  public class PageContainer implements Parcelable

    {
 public final Page center;
 public final Page left;
 public final Page right;
 
 public PageContainer(@NonNull Page center) {
 this.center = center;
 this.left = null;
 this.right = null;
 }
 
 public PageContainer(@Nullable Page left, @Nullable Page right) {
 this.center = null;
 this.left = left;
 this.right = right;
 }
 }
  8. C-LIS CO., LTD. ୯ϖʔδදࣔ 4JOHMF1BHF"EBQUFS  public class SinglePageAdapter extends

    AbsPageAdapter {
 
 public SinglePageAdapter(Book book) {
 super(book);
 }
 
 @Override
 public int getCount() {
 return book.getPageList().size();
 }
 @Override
 public PageContainer getPageInternal(int position) {
 
 List<Page> pageList = book.getPageList();
 
 Page basePage = pageList.get(position);
 
 // ݟ։͖Ͱͳ͚Ε͹தԝ഑ஔ
 if (!basePage.isSpread()) {
 return new PageContainer(basePage);
 }
 
 Align basePageAlign = book.getStartPageAlign();
 if (position % 2 == 1) {
 basePageAlign = Align.flip(basePageAlign);
 }
 
 Page leftPage = null;
 Page rightPage = null;
 
 if (basePageAlign == Align.Left) {
 leftPage = basePage;
 } else {
 rightPage = basePage;
 }
 
 return new PageContainer(leftPage, rightPage);
 }
 }

  9. C-LIS CO., LTD. ̎ϖʔδදࣔ %PVCMF1BHF"EBQUFS  public class DoublePageAdapter extends

    AbsPageAdapter {
 
 public DoublePageAdapter(Book book) {
 super(book);
 }
 
 @Override
 public PageContainer getPageInternal(int position) {
 
 Page rightPage = null;
 Page leftPage = null;
 
 if (position == 0) {
 int pageIndex = 0;
 if (book.getStartPageAlign() == Align.Right) {
 rightPage = getPageByIndex(pageIndex);
 leftPage = getPageByIndex(pageIndex + 1);
 } else {
 leftPage = getPageByIndex(pageIndex);
 }
 } else {
 int pageIndex = position * 2 - 1;
 if (book.getStartPageAlign() == Align.Right) {
 pageIndex++;
 }
 rightPage = getPageByIndex(pageIndex);
 leftPage = getPageByIndex(pageIndex + 1);
 }
 
 return new PageContainer(leftPage, rightPage);
 } @Override
 public int getCount() {
 int basePageCount = book.getPageList().size();
 if (book.getStartPageAlign() == Align.Left) {
 basePageCount++;
 }
 return (basePageCount / 2) + (basePageCount % 2);
 }
 
 private Page getPageByIndex(int index) {
 if (index < 0) {
 return null;
 } else if (index > book.getPageList().size() - 1) {
 return null;
 }
 return book.getPageList().get(index);
 }
 }
  10. C-LIS CO., LTD. 7JFX1BHFSͷ"EBQUFS  private static class Adapter extends

    FragmentPagerAdapter {
 
 private final AbsPageAdapter adapter;
 
 public Adapter(@NonNull FragmentManager fm, AbsPageAdapter adapter) {
 super(fm);
 this.adapter = adapter;
 }
 
 @Override
 public Fragment getItem(int position) {
 PageContainer container = adapter.getPage(position);
 return PageFragment.newInstance(container);
 }
 
 @Override
 public int getCount() {
 return adapter.getCount();
 }
 
 @Override
 public int getItemPosition(Object object) {
 return POSITION_NONE;
 }
 }

  11. C-LIS CO., LTD. .BJO"DUJWJUZKBWBʢൈਮʣ  @NonNull
 private AbsPageAdapter getPageAdapter() {


    List<Page> pageList = new ArrayList<>();
 // লུ 
 Book book = new Book(Align.Left, pageList);
 
 AbsPageAdapter pageAdapter;
 if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
 pageAdapter = new DoublePageAdapter(book);
 } else {
 pageAdapter = new SinglePageAdapter(book);
 }
 
 return pageAdapter;
 }

  12. C-LIS CO., LTD. 1BHF'SBHNFOUʢൈਮʣ  public class PageFragment extends Fragment

    {
 private static final String TAG = PageFragment.class.getSimpleName();
 
 private static final String KEY_PAGE = "key_page";
 
 @Nullable
 private PageContainer pageContainer;
 
 @Nullable
 private PageView centerPage;
 
 @Nullable
 private PageView leftPage;
 
 @Nullable
 private PageView rightPage;
 
 @NonNull
 public static PageFragment newInstance(@NonNull PageContainer container) {
 PageFragment fragment = new PageFragment();
 
 Bundle args = new Bundle();
 args.putParcelable(KEY_PAGE, container);
 fragment.setArguments(args);
 
 return fragment;
 }
 
 @Override
 public void onCreate(@Nullable Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 
 pageContainer = getArguments().getParcelable(KEY_PAGE);
 
 if (pageContainer != null) {
 Log.d(TAG, pageContainer.toString());
 }
 }
 @Nullable
 @Override
 public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
 
 View view = inflater.inflate(R.layout.fragment_page, container, false);
 if (pageContainer == null) {
 return view;
 }
 
 // ̍ϖʔδ
 centerPage = (PageView) view.findViewById(R.id.centerPage);
 
 if (centerPage != null) {
 if (pageContainer.center != null) {
 centerPage.setPage(pageContainer.center, Align.Center);
 } else if (pageContainer.left != null) {
 centerPage.setPage(pageContainer.left, Align.Left);
 } else if (pageContainer.right != null) {
 centerPage.setPage(pageContainer.right, Align.Right);
 }
 
 return view;
 }
 
 // ̎ϖʔδ
 leftPage = (PageView) view.findViewById(R.id.leftPage);
 rightPage = (PageView) view.findViewById(R.id.rightPage);
 
 if (pageContainer.left != null || pageContainer.right != null) {
 if (leftPage != null) {
 leftPage.setPage(pageContainer.left, Align.Left);
 }
 if (rightPage != null) {
 rightPage.setPage(pageContainer.right, Align.Right);
 }
 }
 
 return view;
 }
 }
  13. C-LIS CO., LTD. public class Viewport {
 // লུ public

    void onSizeChanged(int width, int height, int oldWidth, int oldHeight) {
 if (viewWidth == width && viewHeight == height) {
 return;
 }
 
 viewWidth = width;
 viewHeight = height;
 
 calculateScale();
 }
 
 public void setAlign(@NonNull Align align) {
 this.align = align;
 
 calculateScale();
 }
 
 public void setContentSize(int width, int height) {
 contentWidth = width;
 contentHeight = height;
 
 calculateScale();
 }
 
 private void calculateScale() {
 scale = minScale = Math.min(viewWidth / contentWidth, viewHeight / contentHeight);
 maxScale = minScale * 5.0f;
 
 marginTop = (viewHeight - contentHeight * scale) / 2;
 float left = viewWidth - contentWidth * scale;
 
 switch (align) {
 case Left:
 marginLeft = left;
 break;
 case Right:
 marginLeft = 0;
 break;
 case Center:
 marginLeft = left / 2;
 break;
 }
 
 isInitialized = true;
 }
 // লུ } 7JFXQPSUʢൈਮʣ 
  14. C-LIS CO., LTD. public class PageView extends View {
 private

    static final String TAG = PageView.class.getSimpleName();
 
 private final Paint paint = new Paint();
 
 @Nullable
 private Page page;
 
 @Nullable
 private Align align;
 
 @NonNull
 private final Viewport viewport;
 // লུ
 
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
 super.onSizeChanged(w, h, oldw, oldh);
 
 viewport.onSizeChanged(w, h, oldw, oldh);
 }
 
 public void setPage(@Nullable Page page, @NonNull Align align) {
 this.page = page;
 this.align = align;
 
 viewport.setAlign(align);
 
 if (page == null) {
 return;
 }
 
 if (bitmap == null) {
 new LoadThread(page.getFileName()).start();
 }
 }
 // লུ private Bitmap bitmap;
 @Override
 protected void onDraw(Canvas canvas) {
 super.onDraw(canvas);
 
 if (page == null || bitmap == null) {
 return;
 }
 
 if (!viewport.isInitialized()) {
 return;
 }
 
 if (align == Align.Left) {
 canvas.drawColor(Color.DKGRAY);
 } else {
 canvas.drawColor(Color.LTGRAY);
 }
 
 int sc = canvas.save();
 canvas.translate(viewport.getMarginLeft(), viewport.getMarginTop());
 canvas.scale(viewport.getScale(), viewport.getScale());
 
 canvas.drawBitmap(bitmap, 0, 0, paint);
 
 canvas.restoreToCount(sc);
 
 canvas.drawText(align.name() + ": " + String.valueOf(page.getNumber()), 50, 150, paint);
 }
 // লུ } 1BHF7JFXʢൈਮʣ