Render themes: PNG scaling, fix #595
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright 2013 Hannes Janetzek
|
||||
* Copyright 2016 Longri
|
||||
* Copyright 2016 devemux86
|
||||
* Copyright 2016-2018 devemux86
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
*
|
||||
@@ -23,6 +23,8 @@ import android.graphics.BitmapFactory;
|
||||
import android.opengl.GLES20;
|
||||
import android.opengl.GLUtils;
|
||||
|
||||
import org.oscim.backend.CanvasAdapter;
|
||||
import org.oscim.utils.GraphicUtils;
|
||||
import org.oscim.utils.IOUtils;
|
||||
|
||||
import java.io.ByteArrayOutputStream;
|
||||
@@ -31,7 +33,7 @@ import java.io.InputStream;
|
||||
import static android.graphics.Bitmap.Config.ARGB_8888;
|
||||
|
||||
public class AndroidBitmap implements org.oscim.backend.canvas.Bitmap {
|
||||
final Bitmap mBitmap;
|
||||
Bitmap mBitmap;
|
||||
|
||||
public AndroidBitmap(InputStream inputStream) {
|
||||
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
||||
@@ -43,6 +45,12 @@ public class AndroidBitmap implements org.oscim.backend.canvas.Bitmap {
|
||||
mBitmap = bitmap;
|
||||
}
|
||||
|
||||
public AndroidBitmap(InputStream inputStream, int width, int height, int percent) {
|
||||
this(inputStream);
|
||||
float[] newSize = GraphicUtils.imageSize(getWidth(), getHeight(), CanvasAdapter.getScale(), width, height, percent);
|
||||
scaleTo((int) newSize[0], (int) newSize[1]);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return mBitmap != null;
|
||||
@@ -117,4 +125,17 @@ public class AndroidBitmap implements org.oscim.backend.canvas.Bitmap {
|
||||
IOUtils.closeQuietly(outputStream);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void scaleTo(int width, int height) {
|
||||
if (getWidth() != width || getHeight() != height) {
|
||||
// The effect of the filter argument to createScaledBitmap is not well documented in the
|
||||
// official android docs, but according to
|
||||
// http://stackoverflow.com/questions/2895065/what-does-the-filter-parameter-to-createscaledbitmap-do
|
||||
// passing true results in smoother edges, less pixelation.
|
||||
// If smoother corners improve the readability of map labels is perhaps debatable.
|
||||
android.graphics.Bitmap scaledBitmap = android.graphics.Bitmap.createScaledBitmap(mBitmap, width, height, true);
|
||||
mBitmap = scaledBitmap;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
* Copyright 2016-2017 devemux86
|
||||
* Copyright 2016-2018 devemux86
|
||||
* Copyright 2017 Longri
|
||||
*
|
||||
* This file is part of the OpenScienceMap project (http://www.opensciencemap.org).
|
||||
@@ -58,6 +58,11 @@ public final class AndroidGraphics extends CanvasAdapter {
|
||||
return new AndroidBitmap(inputStream);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap decodeBitmapImpl(InputStream inputStream, int width, int height, int percent) {
|
||||
return new AndroidBitmap(inputStream, width, height, percent);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Bitmap decodeSvgBitmapImpl(InputStream inputStream, int width, int height, int percent) throws IOException {
|
||||
return new AndroidSvgBitmap(inputStream, width, height, percent);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/*
|
||||
* Copyright 2010, 2011, 2012 mapsforge.org
|
||||
* Copyright 2013-2014 Ludwig M Brinckmann
|
||||
* Copyright 2014-2017 devemux86
|
||||
* Copyright 2014-2018 devemux86
|
||||
*
|
||||
* 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,6 +24,7 @@ import android.graphics.RectF;
|
||||
import com.caverock.androidsvg.SVG;
|
||||
|
||||
import org.oscim.backend.CanvasAdapter;
|
||||
import org.oscim.utils.GraphicUtils;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
@@ -41,34 +42,12 @@ public class AndroidSvgBitmap extends AndroidBitmap {
|
||||
|
||||
double scale = scaleFactor / Math.sqrt((picture.getHeight() * picture.getWidth()) / defaultSize);
|
||||
|
||||
float bitmapWidth = (float) (picture.getWidth() * scale);
|
||||
float bitmapHeight = (float) (picture.getHeight() * scale);
|
||||
float[] bmpSize = GraphicUtils.imageSize(picture.getWidth(), picture.getHeight(), (float) scale, width, height, percent);
|
||||
|
||||
float aspectRatio = (1f * picture.getWidth()) / picture.getHeight();
|
||||
|
||||
if (width != 0 && height != 0) {
|
||||
// both width and height set, override any other setting
|
||||
bitmapWidth = width;
|
||||
bitmapHeight = height;
|
||||
} else if (width == 0 && height != 0) {
|
||||
// only width set, calculate from aspect ratio
|
||||
bitmapWidth = height * aspectRatio;
|
||||
bitmapHeight = height;
|
||||
} else if (width != 0 && height == 0) {
|
||||
// only height set, calculate from aspect ratio
|
||||
bitmapHeight = width / aspectRatio;
|
||||
bitmapWidth = width;
|
||||
}
|
||||
|
||||
if (percent != 100) {
|
||||
bitmapWidth *= percent / 100f;
|
||||
bitmapHeight *= percent / 100f;
|
||||
}
|
||||
|
||||
android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bitmapWidth),
|
||||
(int) Math.ceil(bitmapHeight), Bitmap.Config.ARGB_8888);
|
||||
android.graphics.Bitmap bitmap = android.graphics.Bitmap.createBitmap((int) Math.ceil(bmpSize[0]),
|
||||
(int) Math.ceil(bmpSize[1]), Bitmap.Config.ARGB_8888);
|
||||
Canvas canvas = new Canvas(bitmap);
|
||||
canvas.drawPicture(picture, new RectF(0, 0, bitmapWidth, bitmapHeight));
|
||||
canvas.drawPicture(picture, new RectF(0, 0, bmpSize[0], bmpSize[1]));
|
||||
|
||||
return bitmap;
|
||||
} catch (Exception e) {
|
||||
|
||||
Reference in New Issue
Block a user