use of com.github.barteksc.pdfviewer.model.PagePart in project AndroidPdfViewer by barteksc.
the class RenderingHandler method handleMessage.
@Override
public void handleMessage(Message message) {
RenderingTask task = (RenderingTask) message.obj;
final PagePart part = proceed(task);
if (part != null) {
pdfView.post(new Runnable() {
@Override
public void run() {
pdfView.onBitmapRendered(part);
}
});
}
}
use of com.github.barteksc.pdfviewer.model.PagePart in project AndroidPdfViewer by barteksc.
the class RenderingHandler method proceed.
private PagePart proceed(RenderingTask renderingTask) {
if (!openedPages.contains(renderingTask.page)) {
openedPages.add(renderingTask.page);
pdfiumCore.openPage(pdfDocument, renderingTask.page);
}
int w = Math.round(renderingTask.width);
int h = Math.round(renderingTask.height);
Bitmap render = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
calculateBounds(w, h, renderingTask.bounds);
pdfiumCore.renderPageBitmap(pdfDocument, render, renderingTask.page, roundedRenderBounds.left, roundedRenderBounds.top, roundedRenderBounds.width(), roundedRenderBounds.height(), renderingTask.annotationRendering);
if (!renderingTask.bestQuality) {
Bitmap cpy = render.copy(Bitmap.Config.RGB_565, false);
render.recycle();
render = cpy;
}
return new //
PagePart(//
renderingTask.userPage, //
renderingTask.page, //
render, //
renderingTask.width, //
renderingTask.height, //
renderingTask.bounds, //
renderingTask.thumbnail, renderingTask.cacheOrder);
}
use of com.github.barteksc.pdfviewer.model.PagePart in project AndroidPdfViewer by barteksc.
the class CacheManager method recycle.
public void recycle() {
synchronized (passiveActiveLock) {
for (PagePart part : passiveCache) {
part.getRenderedBitmap().recycle();
}
passiveCache.clear();
for (PagePart part : activeCache) {
part.getRenderedBitmap().recycle();
}
activeCache.clear();
}
synchronized (thumbnails) {
for (PagePart part : thumbnails) {
part.getRenderedBitmap().recycle();
}
thumbnails.clear();
}
}
use of com.github.barteksc.pdfviewer.model.PagePart in project AndroidPdfViewer by barteksc.
the class PDFView method onDraw.
@Override
protected void onDraw(Canvas canvas) {
if (isInEditMode()) {
return;
}
// As I said in this class javadoc, we can think of this canvas as a huge
// strip on which we draw all the images. We actually only draw the rendered
// parts, of course, but we render them in the place they belong in this huge
// strip.
// That's where Canvas.translate(x, y) becomes very helpful.
// This is the situation :
// _______________________________________________
// | | |
// | the actual | The big strip |
// | canvas | |
// |_____________| |
// |_______________________________________________|
//
// If the rendered part is on the bottom right corner of the strip
// we can draw it but we won't see it because the canvas is not big enough.
// But if we call translate(-X, -Y) on the canvas just before drawing the object :
// _______________________________________________
// | _____________|
// | The big strip | |
// | | the actual |
// | | canvas |
// |_________________________________|_____________|
//
// The object will be on the canvas.
// This technique is massively used in this method, and allows
// abstraction of the screen position when rendering the parts.
// Draws background
Drawable bg = getBackground();
if (bg == null) {
canvas.drawColor(Color.WHITE);
} else {
bg.draw(canvas);
}
if (recycled) {
return;
}
if (state != State.SHOWN) {
return;
}
// Moves the canvas before drawing any element
float currentXOffset = this.currentXOffset;
float currentYOffset = this.currentYOffset;
canvas.translate(currentXOffset, currentYOffset);
// Draws thumbnails
for (PagePart part : cacheManager.getThumbnails()) {
drawPart(canvas, part);
}
// Draws parts
for (PagePart part : cacheManager.getPageParts()) {
drawPart(canvas, part);
}
// Draws the user layer
if (onDrawListener != null) {
canvas.translate(toCurrentScale(currentFilteredPage * optimalPageWidth), 0);
//
onDrawListener.onLayerDrawn(//
canvas, //
toCurrentScale(optimalPageWidth), toCurrentScale(optimalPageHeight), currentPage);
canvas.translate(-toCurrentScale(currentFilteredPage * optimalPageWidth), 0);
}
// Restores the canvas position
canvas.translate(-currentXOffset, -currentYOffset);
}
use of com.github.barteksc.pdfviewer.model.PagePart in project AndroidPdfViewer by barteksc.
the class CacheManager method makeAFreeSpace.
private void makeAFreeSpace() {
synchronized (passiveActiveLock) {
while ((activeCache.size() + passiveCache.size()) >= CACHE_SIZE && !passiveCache.isEmpty()) {
PagePart part = passiveCache.poll();
part.getRenderedBitmap().recycle();
}
while ((activeCache.size() + passiveCache.size()) >= CACHE_SIZE && !activeCache.isEmpty()) {
activeCache.poll().getRenderedBitmap().recycle();
}
}
}