From 963abd78fd9d8506d855496bdbeb5c949c5df0fe Mon Sep 17 00:00:00 2001 From: vshcherb Date: Tue, 8 Oct 2013 00:24:26 +0200 Subject: [PATCH] Fix icons rendering --- .../src/net/osmand/RenderingContext.java | 1 + .../plus/render/MapRenderRepositories.java | 1 + .../osmand/plus/render/OsmandRenderer.java | 18 +++++++++++---- .../osmand/plus/render/RenderingIcons.java | 22 +------------------ .../net/osmand/plus/render/TextRenderer.java | 14 +++++++++--- .../net/osmand/plus/views/POIMapLayer.java | 2 +- 6 files changed, 29 insertions(+), 29 deletions(-) diff --git a/OsmAnd-java/src/net/osmand/RenderingContext.java b/OsmAnd-java/src/net/osmand/RenderingContext.java index c7fac135da..4297912146 100644 --- a/OsmAnd-java/src/net/osmand/RenderingContext.java +++ b/OsmAnd-java/src/net/osmand/RenderingContext.java @@ -46,6 +46,7 @@ public class RenderingContext { public int lastRenderedKey = 0; // be aware field is using in C++ + public float screenDensityRatio = 1; public int shadowRenderingMode = ShadowRenderingMode.BLUR_SHADOW.value; public int shadowRenderingColor = 0xff969696; public String renderingDebugInfo; diff --git a/OsmAnd/src/net/osmand/plus/render/MapRenderRepositories.java b/OsmAnd/src/net/osmand/plus/render/MapRenderRepositories.java index 3f48bf64fa..0064fc7f61 100644 --- a/OsmAnd/src/net/osmand/plus/render/MapRenderRepositories.java +++ b/OsmAnd/src/net/osmand/plus/render/MapRenderRepositories.java @@ -568,6 +568,7 @@ public class MapRenderRepositories { currentRenderingContext.nightMode = nightMode; currentRenderingContext.useEnglishNames = prefs.USE_ENGLISH_NAMES.get(); currentRenderingContext.setDensityValue(mapDensity); + currentRenderingContext.screenDensityRatio = mapDensity / Math.max(1, requestedBox.getDensity()) ; // init rendering context currentRenderingContext.tileDivisor = tileDivisor; if (checkWhetherInterrupted()) { diff --git a/OsmAnd/src/net/osmand/plus/render/OsmandRenderer.java b/OsmAnd/src/net/osmand/plus/render/OsmandRenderer.java index 8f4ee61263..dad4b4a743 100644 --- a/OsmAnd/src/net/osmand/plus/render/OsmandRenderer.java +++ b/OsmAnd/src/net/osmand/plus/render/OsmandRenderer.java @@ -32,6 +32,8 @@ import android.graphics.Canvas; import android.graphics.ColorFilter; import android.graphics.DashPathEffect; import android.graphics.Paint; +import android.graphics.Rect; +import android.graphics.RectF; import android.graphics.Paint.Cap; import android.graphics.Paint.Style; import android.graphics.Path; @@ -301,7 +303,7 @@ public class OsmandRenderer { int[] alreadyDrawnIcons = new int[iconsW * iconsH / 32]; for (IconDrawInfo icon : rc.iconsToDraw) { if (icon.resId != null) { - Bitmap ico = RenderingIcons.getSmallPoiIcon(context, icon.resId); + Bitmap ico = RenderingIcons.getIcon(context, icon.resId); if (ico != null) { if (icon.y >= 0 && icon.y < rc.height && icon.x >= 0 && icon.x < rc.width) { int z = (((int) icon.x / skewConstant) + ((int) icon.y / skewConstant) * iconsW); @@ -314,8 +316,17 @@ public class OsmandRenderer { // check bit b if it is set if (((ind >> b) & 1) == 0) { alreadyDrawnIcons[i] = ind | (1 << b); - cv.drawBitmap(ico, icon.x - ico.getWidth() / 2, - icon.y - ico.getHeight() / 2, paintIcon); + float left = icon.x - ico.getWidth() / 2 * rc.screenDensityRatio; + float top = icon.y - ico.getHeight() / 2 * rc.screenDensityRatio; + if(rc.screenDensityRatio != 1f){ + RectF rf = new RectF(left, top, left + ico.getWidth() * rc.screenDensityRatio , + top + ico.getHeight() * rc.screenDensityRatio); + Rect src = new Rect(0, 0, ico.getWidth(), ico + .getHeight()); + cv.drawBitmap(ico, src, rf, paintIcon); + } else { + cv.drawBitmap(ico, left, top, paintIcon); + } } } } @@ -564,7 +575,6 @@ public class OsmandRenderer { if (shadowColor == 0) { shadowRadius = 0; } - System.out.println("Shadow radius " + shadowRadius); p.setShadowLayer(shadowRadius, 0, 0, shadowColor); } } diff --git a/OsmAnd/src/net/osmand/plus/render/RenderingIcons.java b/OsmAnd/src/net/osmand/plus/render/RenderingIcons.java index a2b484b95e..45583ecf48 100644 --- a/OsmAnd/src/net/osmand/plus/render/RenderingIcons.java +++ b/OsmAnd/src/net/osmand/plus/render/RenderingIcons.java @@ -16,8 +16,6 @@ import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.Drawable; -import android.util.DisplayMetrics; -import android.view.WindowManager; public class RenderingIcons { private static final Log log = PlatformUtil.getLog(RenderingIcons.class); @@ -26,7 +24,7 @@ public class RenderingIcons { private static Map smallIcons = new LinkedHashMap(); private static Map bigIcons = new LinkedHashMap(); private static Map iconsBmp = new LinkedHashMap(); - private static DisplayMetrics dm; +// private static DisplayMetrics dm; public static boolean containsIcon(String s){ return icons.containsKey(s); @@ -83,29 +81,11 @@ public class RenderingIcons { return null; } - public static Bitmap getSmallPoiIcon(Context ctx, String s) { - Integer resId = smallIcons.get(s); - if (resId != null) { - return BitmapFactory.decodeResource(ctx.getResources(), resId, null); - } - return null; - } - public static Bitmap getIcon(Context ctx, String s) { if (!iconsBmp.containsKey(s)) { Integer resId = icons.get(s); if (resId != null) { - if (dm == null) { - dm = new DisplayMetrics(); - WindowManager wmgr = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE); - wmgr.getDefaultDisplay().getMetrics(dm); - } -// BitmapFactory.Options options = new BitmapFactory.Options(); -// options.inScaled = false; -// options.inTargetDensity = dm.densityDpi; -// options.inDensity = dm.densityDpi; Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), resId, null); -// Bitmap bmp = UnscaledBitmapLoader.loadFromResource(ctx.getResources(), resId.intValue(), null, dm); iconsBmp.put(s, bmp); } else { iconsBmp.put(s, null); diff --git a/OsmAnd/src/net/osmand/plus/render/TextRenderer.java b/OsmAnd/src/net/osmand/plus/render/TextRenderer.java index 2dc96ad7ef..d50a3c31a5 100644 --- a/OsmAnd/src/net/osmand/plus/render/TextRenderer.java +++ b/OsmAnd/src/net/osmand/plus/render/TextRenderer.java @@ -242,11 +242,19 @@ public class TextRenderer { if (text.shieldRes != null) { Bitmap ico = RenderingIcons.getIcon(context, text.shieldRes); if (ico != null) { - float left = text.centerX - ico.getWidth() / 2 + float left = text.centerX - ico.getWidth() / 2 * rc.screenDensityRatio - 0.5f; - float top = text.centerY - ico.getHeight() / 2 + float top = text.centerY - ico.getHeight() / 2 * rc.screenDensityRatio - rc.getDensityValue(4.5f); - cv.drawBitmap(ico, left, top, paintIcon); + if(rc.screenDensityRatio != 1f){ + RectF rf = new RectF(left, top, left + ico.getWidth() * rc.screenDensityRatio , + top + ico.getHeight() * rc.screenDensityRatio); + Rect src = new Rect(0, 0, ico.getWidth(), ico + .getHeight()); + cv.drawBitmap(ico, src, rf, paintIcon); + } else { + cv.drawBitmap(ico, left, top, paintIcon); + } } } diff --git a/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java b/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java index 903aea16b4..73bcfcc7d0 100644 --- a/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/POIMapLayer.java @@ -183,7 +183,7 @@ public class POIMapLayer extends OsmandMapLayer implements ContextMenuLayer.ICon id = tag.toString(); } if(id != null){ - Bitmap bmp = RenderingIcons.getSmallPoiIcon(view.getContext(), id); + Bitmap bmp = RenderingIcons.getIcon(view.getContext(), id); if(bmp != null){ canvas.drawBitmap(bmp, x - bmp.getWidth() / 2, y - bmp.getHeight() / 2, paintIcon); }