private void renderImageSupportsScaling(Canvas canvas, AnimatedImageFrame frame) { double xScale = (double) mRenderedBounds.width() / (double) mAnimatedImage.getWidth(); double yScale = (double) mRenderedBounds.height() / (double) mAnimatedImage.getHeight(); int frameWidth = (int) Math.round(frame.getWidth() * xScale); int frameHeight = (int) Math.round(frame.getHeight() * yScale); int xOffset = (int) (frame.getXOffset() * xScale); int yOffset = (int) (frame.getYOffset() * yScale); synchronized (this) { prepareTempBitmapForThisSize(mRenderedBounds.width(), mRenderedBounds.height()); frame.renderFrame(frameWidth, frameHeight, mTempBitmap); // Temporary bitmap can be bigger than frame, so we should draw only rendered area of bitmap mRenderSrcRect.set(0, 0, mRenderedBounds.width(), mRenderedBounds.height()); mRenderDstRect.set(xOffset, yOffset, mRenderedBounds.width(), mRenderedBounds.height()); canvas.drawBitmap(mTempBitmap, mRenderSrcRect, mRenderDstRect, null); } }
private void renderImageDoesNotSupportScaling(Canvas canvas, AnimatedImageFrame frame) { int frameWidth = frame.getWidth(); int frameHeight = frame.getHeight(); int xOffset = frame.getXOffset(); int yOffset = frame.getYOffset(); synchronized (this) { prepareTempBitmapForThisSize(frameWidth, frameHeight); frame.renderFrame(frameWidth, frameHeight, mTempBitmap); // Temporary bitmap can be bigger than frame, so we should draw only rendered area of bitmap mRenderSrcRect.set(0, 0, frameWidth, frameHeight); mRenderDstRect.set(0, 0, frameWidth, frameHeight); float xScale = (float) mRenderedBounds.width() / (float) mAnimatedImage.getWidth(); float yScale = (float) mRenderedBounds.height() / (float) mAnimatedImage.getHeight(); canvas.save(); canvas.scale(xScale, yScale); canvas.translate(xOffset, yOffset); canvas.drawBitmap(mTempBitmap, mRenderSrcRect, mRenderDstRect, null); canvas.restore(); } }
private void renderImageSupportsScaling(Canvas canvas, AnimatedImageFrame frame) { double xScale = (double) mRenderedBounds.width() / (double) mAnimatedImage.getWidth(); double yScale = (double) mRenderedBounds.height() / (double) mAnimatedImage.getHeight(); int frameWidth = (int) Math.round(frame.getWidth() * xScale); int frameHeight = (int) Math.round(frame.getHeight() * yScale); int xOffset = (int) (frame.getXOffset() * xScale); int yOffset = (int) (frame.getYOffset() * yScale); synchronized (this) { int renderedWidth = mRenderedBounds.width(); int renderedHeight = mRenderedBounds.height(); // Update the temp bitmap to be >= rendered dimensions prepareTempBitmapForThisSize(renderedWidth, renderedHeight); frame.renderFrame(frameWidth, frameHeight, mTempBitmap); // Temporary bitmap can be bigger than frame, so we should draw only rendered area of bitmap mRenderSrcRect.set(0, 0, renderedWidth, renderedHeight); mRenderDstRect.set(xOffset, yOffset, xOffset + renderedWidth, yOffset + renderedHeight); canvas.drawBitmap(mTempBitmap, mRenderSrcRect, mRenderDstRect, null); } }
@Override public void renderFrame(int frameNumber, Canvas canvas) { AnimatedImageFrame frame = mAnimatedImage.getFrame(frameNumber); try { if (mAnimatedImage.doesRenderSupportScaling()) { renderImageSupportsScaling(canvas, frame); } else { renderImageDoesNotSupportScaling(canvas, frame); } } finally { frame.dispose(); } }