Skip to content

Commit

Permalink
AndroidCanvas improvements for Android 8+
Browse files Browse the repository at this point in the history
  • Loading branch information
devemux86 committed Sep 29, 2019
1 parent 6ff0d3f commit 90be199
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
Expand Up @@ -62,7 +62,7 @@ void drawBitmap(Bitmap bitmap, int srcLeft, int srcTop, int srcRight, int srcBot


void setClip(int left, int top, int width, int height); void setClip(int left, int top, int width, int height);


void setClip(int left, int top, int width, int height, boolean save); void setClip(int left, int top, int width, int height, boolean intersect);


void setClipDifference(int left, int top, int width, int height); void setClipDifference(int left, int top, int width, int height);


Expand Down
Expand Up @@ -259,11 +259,7 @@ public boolean isFilterBitmap() {
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
@Override @Override
public void resetClip() { public void resetClip() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
this.canvas.save();
this.canvas.clipRect(0, 0, getWidth(), getHeight());
this.canvas.restore();
} else {
this.canvas.clipRect(0, 0, getWidth(), getHeight(), Region.Op.REPLACE); this.canvas.clipRect(0, 0, getWidth(), getHeight(), Region.Op.REPLACE);
} }
} }
Expand All @@ -280,18 +276,14 @@ public void setBitmap(Bitmap bitmap) {


@Override @Override
public void setClip(int left, int top, int width, int height) { public void setClip(int left, int top, int width, int height) {
setClip(left, top, width, height, true); setClip(left, top, width, height, false);
} }


@Override @Override
public void setClip(int left, int top, int width, int height, boolean save) { public void setClip(int left, int top, int width, int height, boolean intersect) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (save) { if (intersect) {
this.canvas.save(); this.canvas.clipRect(left, top, left + width, top + height);
}
this.canvas.clipRect(left, top, left + width, top + height);
if (save) {
this.canvas.restore();
} }
} else { } else {
this.setClipInternal(left, top, width, height, Region.Op.REPLACE); this.setClipInternal(left, top, width, height, Region.Op.REPLACE);
Expand All @@ -300,7 +292,11 @@ public void setClip(int left, int top, int width, int height, boolean save) {


@Override @Override
public void setClipDifference(int left, int top, int width, int height) { public void setClipDifference(int left, int top, int width, int height) {
this.setClipInternal(left, top, width, height, Region.Op.DIFFERENCE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
this.canvas.clipOutRect(left, top, left + width, top + height);
} else {
this.setClipInternal(left, top, width, height, Region.Op.DIFFERENCE);
}
} }


@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
Expand Down Expand Up @@ -328,9 +324,7 @@ public void shadeBitmap(Bitmap bitmap, Rectangle hillRect, Rectangle tileRect, f
if (bitmap == null) { if (bitmap == null) {
if (tileRect != null) { if (tileRect != null) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//this.canvas.save();
this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom); this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom);
//this.canvas.restore();
} else { } else {
this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom, Region.Op.REPLACE); this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom, Region.Op.REPLACE);
} }
Expand All @@ -349,9 +343,7 @@ public void shadeBitmap(Bitmap bitmap, Rectangle hillRect, Rectangle tileRect, f
if (horizontalScale < 1 && verticalScale < 1) { if (horizontalScale < 1 && verticalScale < 1) {
// fast path for wide zoom (downscaling) // fast path for wide zoom (downscaling)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//this.canvas.save();
this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom); this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom);
//this.canvas.restore();
} else { } else {
this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom, Region.Op.REPLACE); this.canvas.clipRect((float) tileRect.left, (float) tileRect.top, (float) tileRect.right, (float) tileRect.bottom, Region.Op.REPLACE);
} }
Expand Down
Expand Up @@ -400,11 +400,11 @@ public void setBitmap(Bitmap bitmap) {


@Override @Override
public void setClip(int left, int top, int width, int height) { public void setClip(int left, int top, int width, int height) {
setClip(left, top, width, height, true); setClip(left, top, width, height, false);
} }


@Override @Override
public void setClip(int left, int top, int width, int height, boolean save) { public void setClip(int left, int top, int width, int height, boolean intersect) {
this.graphics2D.setClip(left, top, width, height); this.graphics2D.setClip(left, top, width, height);
} }


Expand Down
Expand Up @@ -449,25 +449,25 @@ static void mergeSameSized(HillshadingBitmap center, HillshadingBitmap neighbor,
sink = center; sink = center;
source = neighbor; source = neighbor;
copyCanvas.setBitmap(sink); copyCanvas.setBitmap(sink);
copyCanvas.setClip(sink.getWidth() - padding, padding, padding, sink.getHeight() - 2 * padding, false); copyCanvas.setClip(sink.getWidth() - padding, padding, padding, sink.getHeight() - 2 * padding, true);
copyCanvas.drawBitmap(source, (source.getWidth() - 2 * padding), 0); copyCanvas.drawBitmap(source, (source.getWidth() - 2 * padding), 0);
} else if (border == HillshadingBitmap.Border.WEST) { } else if (border == HillshadingBitmap.Border.WEST) {
sink = center; sink = center;
source = neighbor; source = neighbor;
copyCanvas.setBitmap(sink); copyCanvas.setBitmap(sink);
copyCanvas.setClip(0, padding, padding, sink.getHeight() - 2 * padding, false); copyCanvas.setClip(0, padding, padding, sink.getHeight() - 2 * padding, true);
copyCanvas.drawBitmap(source, 2 * padding - (source.getWidth()), 0); copyCanvas.drawBitmap(source, 2 * padding - (source.getWidth()), 0);
} else if (border == HillshadingBitmap.Border.NORTH) { } else if (border == HillshadingBitmap.Border.NORTH) {
sink = center; sink = center;
source = neighbor; source = neighbor;
copyCanvas.setBitmap(sink); copyCanvas.setBitmap(sink);
copyCanvas.setClip(padding, 0, sink.getWidth() - 2 * padding, padding, false); copyCanvas.setClip(padding, 0, sink.getWidth() - 2 * padding, padding, true);
copyCanvas.drawBitmap(source, 0, 2 * padding - (source.getHeight())); copyCanvas.drawBitmap(source, 0, 2 * padding - (source.getHeight()));
} else if (border == HillshadingBitmap.Border.SOUTH) { } else if (border == HillshadingBitmap.Border.SOUTH) {
sink = center; sink = center;
source = neighbor; source = neighbor;
copyCanvas.setBitmap(sink); copyCanvas.setBitmap(sink);
copyCanvas.setClip(padding, sink.getHeight() - padding, sink.getWidth() - 2 * padding, padding, false); copyCanvas.setClip(padding, sink.getHeight() - padding, sink.getWidth() - 2 * padding, padding, true);
copyCanvas.drawBitmap(source, 0, (source.getHeight() - 2 * padding)); copyCanvas.drawBitmap(source, 0, (source.getHeight() - 2 * padding));
} }
} }
Expand Down

0 comments on commit 90be199

Please sign in to comment.