Skip to content

Instantly share code, notes, and snippets.

@pietrocaselani
Created March 19, 2015 17:55
Show Gist options
  • Save pietrocaselani/bae1549e43c53eab897c to your computer and use it in GitHub Desktop.
Save pietrocaselani/bae1549e43c53eab897c to your computer and use it in GitHub Desktop.
Adapter to work with MuPDF
public final class PDFPageAdapter extends BaseAdapter {
//region Fields
private final SparseArray<PointF> mPageSizes;
private final Context mContext;
private final MuPDFCore mCore;
private final int mPages;
//endregion
//region Constructors
public PDFPageAdapter(Context context, MuPDFCore core) {
mPageSizes = new SparseArray<PointF>();
mContext = context;
mPages = core.countPages();
mCore = core;
}
//endregion
//region Base Adapter
@Override public int getCount() {
return mPages;
}
@Override public Object getItem(int position) {
return null;
}
@Override public long getItemId(int position) {
return position;
}
@Override public View getView(final int position, View convertView, ViewGroup parent) {
final MuPDFPageView pageView;
if (convertView == null) {
Bitmap b = Bitmap.createBitmap(parent.getMeasuredWidth(), parent.getMeasuredHeight(), ARGB_8888);
pageView = new MuPDFPageView(mContext, null, mCore, new Point(parent.getMeasuredWidth(), parent.getMeasuredHeight()), b);
} else {
pageView = (MuPDFPageView) convertView;
}
PointF pageSize = mPageSizes.get(position);
if (pageSize != null) {
pageView.setPage(position, pageSize);
} else {
pageView.blank(position);
AsyncTask<Void,Void,PointF> sizingTask = new AsyncTask<Void,Void,PointF>() {
@Override
protected PointF doInBackground(Void... arg0) {
return mCore.getPageSize(position);
}
@Override
protected void onPostExecute(PointF result) {
super.onPostExecute(result);
mPageSizes.put(position, result);
if (pageView.getPage() == position)
pageView.setPage(position, result);
}
};
sizingTask.execute((Void) null);
}
return pageView;
}
//endregion
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment