Long screenshots: Fix/simplify ImageTileSet.toBitmap

Bug: 179378294
Test: manual

There were subtle mistakes in the drawing which caused
bounds to fail when the image had tiles at negative
offsets. toBitmap now delegates to the drawable impl
which can already be cropped with setBounds

In addition this fixes mistakes in TiledImageDrawable
where translation is applied in the wrong direction
and cropRect applied in the wrong position.

Change-Id: Ica28e47031cb28353a49cdd657666f17115c4f6c
This commit is contained in:
Mark Renouf
2021-02-10 21:39:57 -05:00
parent e8d910de02
commit cb077ba635
2 changed files with 9 additions and 14 deletions

View File

@@ -22,6 +22,7 @@ import android.graphics.Rect;
import android.graphics.RenderNode;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import androidx.annotation.UiThread;
@@ -141,22 +142,16 @@ class ImageTileSet {
* getHeight()).
*/
Bitmap toBitmap(Rect bounds) {
Log.d(TAG, "exporting with bounds: " + bounds);
if (mTiles.isEmpty()) {
return null;
}
final RenderNode output = new RenderNode("Bitmap Export");
output.setPosition(0, 0, getWidth(), getHeight());
output.setPosition(0, 0, bounds.width(), bounds.height());
RecordingCanvas canvas = output.beginRecording();
canvas.translate(-getLeft(), -getTop());
// Additional translation to account for the requested bounds
canvas.translate(-bounds.left, -bounds.top);
canvas.clipRect(bounds);
for (ImageTile tile : mTiles) {
canvas.save();
canvas.translate(tile.getLeft(), tile.getTop());
canvas.drawRenderNode(tile.getDisplayList());
canvas.restore();
}
Drawable drawable = getDrawable();
drawable.setBounds(bounds);
drawable.draw(canvas);
output.endRecording();
return HardwareRenderer.createHardwareBitmap(output, bounds.width(), bounds.height());
}

View File

@@ -56,7 +56,7 @@ public class TiledImageDrawable extends Drawable {
mNode = new RenderNode("TiledImageDrawable");
}
mNode.setPosition(0, 0, mTiles.getWidth(), mTiles.getHeight());
Canvas canvas = mNode.beginRecording(mTiles.getWidth(), mTiles.getHeight());
Canvas canvas = mNode.beginRecording();
// Align content (virtual) top/left with 0,0, within the render node
canvas.translate(-mTiles.getLeft(), -mTiles.getTop());
for (int i = 0; i < mTiles.size(); i++) {
@@ -79,8 +79,8 @@ public class TiledImageDrawable extends Drawable {
if (canvas.isHardwareAccelerated()) {
Rect bounds = getBounds();
canvas.save();
canvas.clipRect(bounds);
canvas.translate(bounds.left, bounds.top);
canvas.clipRect(0, 0, bounds.width(), bounds.height());
canvas.translate(-bounds.left, -bounds.top);
canvas.drawRenderNode(mNode);
canvas.restore();
} else {