Skip to content
Permalink
Browse files

Allow alpha in tile layer drawable bitmaps (#1143)

  • Loading branch information
mg4gh authored and devemux86 committed Dec 23, 2019
1 parent 514da6b commit 634cea3130220e5303f31fb8cd6bde7583074e5c
@@ -5,6 +5,7 @@
* Copyright 2017 usrusr
* Copyright 2019 cpt1gl0
* Copyright 2019 Adrian Batzill
* Copyright 2019 mg4gh
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -24,17 +25,17 @@
public interface GraphicContext {
void drawBitmap(Bitmap bitmap, int left, int top);

void drawBitmap(Bitmap bitmap, int left, int top, Filter filter);
void drawBitmap(Bitmap bitmap, int left, int top, float alpha, Filter filter);

void drawBitmap(Bitmap bitmap, Matrix matrix);

void drawBitmap(Bitmap bitmap, Matrix matrix, Filter filter);
void drawBitmap(Bitmap bitmap, Matrix matrix, float alpha, Filter filter);

void drawBitmap(Bitmap bitmap, int srcLeft, int srcTop, int srcRight, int srcBottom,
int dstLeft, int dstTop, int dstRight, int dstBottom);

void drawBitmap(Bitmap bitmap, int srcLeft, int srcTop, int srcRight, int srcBottom,
int dstLeft, int dstTop, int dstRight, int dstBottom, Filter filter);
int dstLeft, int dstTop, int dstRight, int dstBottom, float alpha, Filter filter);

void drawCircle(int x, int y, int radius, Paint paint);

@@ -16,11 +16,7 @@
*/
package org.mapsforge.core.mapelements;

import org.mapsforge.core.graphics.Bitmap;
import org.mapsforge.core.graphics.Canvas;
import org.mapsforge.core.graphics.Display;
import org.mapsforge.core.graphics.Filter;
import org.mapsforge.core.graphics.Matrix;
import org.mapsforge.core.graphics.*;
import org.mapsforge.core.model.Point;
import org.mapsforge.core.model.Rectangle;

@@ -81,6 +77,6 @@ public void draw(Canvas canvas, Point origin, Matrix matrix, Filter filter) {
} else {
matrix.rotate(theta);
}
canvas.drawBitmap(this.symbol, matrix, filter);
canvas.drawBitmap(this.symbol, matrix, 1, filter);
}
}
@@ -5,6 +5,7 @@
* Copyright 2017 usrusr
* Copyright 2019 cpt1gl0
* Copyright 2019 Adrian Batzill
* Copyright 2019 mg4gh
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -109,12 +110,19 @@ public void drawBitmap(Bitmap bitmap, int left, int top) {
}

@Override
public void drawBitmap(Bitmap bitmap, int left, int top, Filter filter) {
public void drawBitmap(Bitmap bitmap, int left, int top, float alpha, Filter filter) {
int oldAlpha = this.bitmapPaint.getAlpha();
if (alpha != 1) {
this.bitmapPaint.setAlpha((int) (alpha * 255));
}
applyFilter(filter);
this.canvas.drawBitmap(AndroidGraphicFactory.getBitmap(bitmap), left, top, bitmapPaint);
if (filter != Filter.NONE) {
bitmapPaint.setColorFilter(null);
}
if (alpha != 1) {
this.bitmapPaint.setAlpha(oldAlpha);
}
}

@Override
@@ -123,12 +131,19 @@ public void drawBitmap(Bitmap bitmap, Matrix matrix) {
}

@Override
public void drawBitmap(Bitmap bitmap, Matrix matrix, Filter filter) {
public void drawBitmap(Bitmap bitmap, Matrix matrix, float alpha, Filter filter) {
int oldAlpha = this.bitmapPaint.getAlpha();
if (alpha != 1) {
this.bitmapPaint.setAlpha((int) (alpha * 255));
}
applyFilter(filter);
this.canvas.drawBitmap(AndroidGraphicFactory.getBitmap(bitmap), AndroidGraphicFactory.getMatrix(matrix), bitmapPaint);
if (filter != Filter.NONE) {
bitmapPaint.setColorFilter(null);
}
if (alpha != 1) {
this.bitmapPaint.setAlpha(oldAlpha);
}
}

@Override
@@ -142,17 +157,22 @@ public void drawBitmap(Bitmap bitmap, int srcLeft, int srcTop, int srcRight, int

@Override
public void drawBitmap(Bitmap bitmap, int srcLeft, int srcTop, int srcRight, int srcBottom,
int dstLeft, int dstTop, int dstRight, int dstBottom, Filter filter) {
int dstLeft, int dstTop, int dstRight, int dstBottom, float alpha, Filter filter) {
int oldAlpha = this.bitmapPaint.getAlpha();
if (alpha != 1) {
this.bitmapPaint.setAlpha((int) (alpha * 255));
}
applyFilter(filter);

this.canvas.drawBitmap(AndroidGraphicFactory.getBitmap(bitmap),
new Rect(srcLeft, srcTop, srcRight, srcBottom),
new Rect(dstLeft, dstTop, dstRight, dstBottom),
this.bitmapPaint);

if (filter != Filter.NONE) {
this.bitmapPaint.setColorFilter(null);
}
if (alpha != 1) {
this.bitmapPaint.setAlpha(oldAlpha);
}
}

@Override
@@ -5,6 +5,7 @@
* Copyright 2019 cpt1gl0
* Copyright 2019 Adrian Batzill
* Copyright 2019 Matthew Egeler
* Copyright 2019 mg4gh
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -158,8 +159,15 @@ public void drawBitmap(Bitmap bitmap, int left, int top) {
}

@Override
public void drawBitmap(Bitmap bitmap, int left, int top, Filter filter) {
public void drawBitmap(Bitmap bitmap, int left, int top, float alpha, Filter filter) {
Composite composite = this.graphics2D.getComposite();
if (alpha != 1) {
this.graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
}
this.graphics2D.drawImage(applyFilter(AwtGraphicFactory.getBitmap(bitmap), filter), left, top, null);
if (alpha != 1) {
this.graphics2D.setComposite(composite);
}
}

@Override
@@ -169,8 +177,15 @@ public void drawBitmap(Bitmap bitmap, Matrix matrix) {
}

@Override
public void drawBitmap(Bitmap bitmap, Matrix matrix, Filter filter) {
public void drawBitmap(Bitmap bitmap, Matrix matrix, float alpha, Filter filter) {
Composite composite = this.graphics2D.getComposite();
if (alpha != 1) {
this.graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
}
this.graphics2D.drawRenderedImage(applyFilter(AwtGraphicFactory.getBitmap(bitmap), filter), AwtGraphicFactory.getAffineTransform(matrix));
if (alpha != 1) {
this.graphics2D.setComposite(composite);
}
}

@Override
@@ -184,11 +199,18 @@ public void drawBitmap(Bitmap bitmap, int srcLeft, int srcTop, int srcRight, int

@Override
public void drawBitmap(Bitmap bitmap, int srcLeft, int srcTop, int srcRight, int srcBottom,
int dstLeft, int dstTop, int dstRight, int dstBottom, Filter filter) {
int dstLeft, int dstTop, int dstRight, int dstBottom, float alpha, Filter filter) {
Composite composite = this.graphics2D.getComposite();
if (alpha != 1) {
this.graphics2D.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
}
this.graphics2D.drawImage(applyFilter(AwtGraphicFactory.getBitmap(bitmap), filter),
dstLeft, dstTop, dstRight, dstBottom,
srcLeft, srcTop, srcRight, srcBottom,
null);
if (alpha != 1) {
this.graphics2D.setComposite(composite);
}
}

@Override
@@ -3,6 +3,7 @@
* Copyright 2014 Ludwig M Brinckmann
* Copyright 2015-2019 devemux86
* Copyright 2019 cpt1gl0
* Copyright 2019 mg4gh
*
* This program is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
@@ -37,6 +38,7 @@
import java.util.Set;

public abstract class TileLayer<T extends Job> extends Layer {
private float alpha = 1.0f;
protected final boolean hasJobQueue;
protected final boolean isTransparent;
protected JobQueue<T> jobQueue;
@@ -108,7 +110,7 @@ public void draw(BoundingBox boundingBox, byte zoomLevel, Canvas canvas, Point t
this.jobQueue.add(job);
}
retrieveLabelsOnly(job);
canvas.drawBitmap(bitmap, (int) Math.round(point.x), (int) Math.round(point.y), this.displayModel.getFilter());
canvas.drawBitmap(bitmap, (int) Math.round(point.x), (int) Math.round(point.y), this.alpha, this.displayModel.getFilter());
bitmap.decrementRefCount();
}
}
@@ -176,7 +178,7 @@ private void drawParentTileBitmap(Canvas canvas, Point point, Tile tile) {
canvas.drawBitmap(bitmap,
(int) (translateX / scaleFactor), (int) (translateY / scaleFactor), (int) ((translateX + tileSize) / scaleFactor), (int) ((translateY + tileSize) / scaleFactor),
x, y, x + tileSize, y + tileSize,
this.displayModel.getFilter());
this.alpha, this.displayModel.getFilter());

canvas.setAntiAlias(antiAlias);
canvas.setFilterBitmap(filterBitmap);
@@ -186,7 +188,7 @@ private void drawParentTileBitmap(Canvas canvas, Point point, Tile tile) {
this.matrix.scale(scaleFactor, scaleFactor);

canvas.setClip(x, y, this.displayModel.getTileSize(), this.displayModel.getTileSize());
canvas.drawBitmap(bitmap, this.matrix, this.displayModel.getFilter());
canvas.drawBitmap(bitmap, this.matrix, this.alpha, this.displayModel.getFilter());
canvas.resetClip();
}

@@ -195,6 +197,10 @@ private void drawParentTileBitmap(Canvas canvas, Point point, Tile tile) {
}
}

public float getAlpha() {
return this.alpha;
}

/**
* @return the first parent object of the given object whose tileCacheBitmap is cached (may be null).
*/
@@ -216,4 +222,8 @@ private Tile getCachedParentTile(Tile tile, int level) {
public TileCache getTileCache() {
return this.tileCache;
}

public void setAlpha(float alpha) {
this.alpha = Math.max(0, Math.min(1, alpha));

This comment has been minimized.

Copy link
@devemux86

devemux86 Dec 23, 2019

Collaborator

Clamp alpha on layer's setter for convenience.

}
}

0 comments on commit 634cea3

Please sign in to comment.
You can’t perform that action at this time.