BackgroundType refactor

This commit is contained in:
Dima-1 2020-06-26 15:33:35 +03:00
parent ec9f46f233
commit 0e7e543bbe
6 changed files with 52 additions and 49 deletions

View file

@ -84,6 +84,7 @@
<dimen name="favorites_select_group_button_width">142dp</dimen>
<dimen name="favorites_icon_top_margin">13dp</dimen>
<dimen name="favorites_select_icon_button_right_padding">9dp</dimen>
<dimen name="point_background_comment_offset_y">14dp</dimen>
<dimen name="local_index_check_right_margin">10dp</dimen>
<dimen name="dialog_elements_vertical_margin">16dp</dimen>

View file

@ -1,6 +1,9 @@
package net.osmand.data;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
@ -311,7 +314,7 @@ public class FavouritePoint implements Serializable, LocationPoint {
CIRCLE("circle", R.string.shared_string_circle, R.drawable.bg_point_circle),
OCTAGON("octagon", R.string.shared_string_octagon, R.drawable.bg_point_octagon),
SQUARE("square", R.string.shared_string_square, R.drawable.bg_point_square),
COMMENT("comment", -1, R.drawable.bg_point_comment);
COMMENT("comment", R.string.poi_dialog_comment, R.drawable.bg_point_comment);
private String typeName;
@StringRes
private int nameId;
@ -344,6 +347,27 @@ public class FavouritePoint implements Serializable, LocationPoint {
}
return defaultValue;
}
public boolean isSelected() {
return this != COMMENT;
}
public int getOffsetY(Context ctx, float textScale) {
return this == COMMENT ? Math.round(ctx.getResources()
.getDimensionPixelSize(R.dimen.point_background_comment_offset_y) * textScale) : 0;
}
public Bitmap getTouchBackground(Context ctx, boolean isSmall) {
return getMapBackgroundIconId(ctx, "center", isSmall);
}
public Bitmap getMapBackgroundIconId(Context ctx, String layer, boolean isSmall) {
Resources res = ctx.getResources();
String iconName = res.getResourceEntryName(getIconId());
String suffix = isSmall ? "_small" : "";
return BitmapFactory.decodeResource(res, res.getIdentifier("ic_" + iconName + "_" + layer + suffix,
"drawable", ctx.getPackageName()));
}
}
public static FavouritePoint fromWpt(@NonNull WptPt pt, @NonNull Context ctx) {

View file

@ -65,28 +65,24 @@ public class PointImageDrawable extends Drawable {
private PointImageDrawable(PointInfo pointInfo) {
this.withShadow = pointInfo.withShadow;
this.synced = pointInfo.synced;
Resources res = pointInfo.ctx.getResources();
UiUtilities uiUtilities = ((OsmandApplication) pointInfo.ctx.getApplicationContext()).getUIUtilities();
Context ctx = pointInfo.ctx;
Resources res = ctx.getResources();
UiUtilities uiUtilities = ((OsmandApplication) ctx.getApplicationContext()).getUIUtilities();
int overlayIconId = pointInfo.overlayIconId;
int uiIconId;
mapIcon = uiUtilities.getIcon(getMapIconId(pointInfo.ctx, overlayIconId), R.color.color_white);
mapIcon = uiUtilities.getIcon(getMapIconId(ctx, overlayIconId), R.color.color_white);
uiIconId = overlayIconId;
int col = pointInfo.color == 0 ? res.getColor(R.color.color_favorite) : pointInfo.color;
uiListIcon = uiUtilities.getIcon(uiIconId, R.color.color_white);
int uiBackgroundIconId = pointInfo.backgroundType.getIconId();
BackgroundType backgroundType = pointInfo.backgroundType;
int uiBackgroundIconId = backgroundType.getIconId();
uiBackgroundIcon = uiUtilities.getPaintedIcon(uiBackgroundIconId, col);
int mapBackgroundIconIdTop = getMapBackgroundIconId(pointInfo, "top", false);
int mapBackgroundIconIdCenter = getMapBackgroundIconId(pointInfo, "center", false);
int mapBackgroundIconIdBottom = getMapBackgroundIconId(pointInfo, "bottom", false);
mapIconBackgroundTop = BitmapFactory.decodeResource(res, mapBackgroundIconIdTop);
mapIconBackgroundCenter = BitmapFactory.decodeResource(res, mapBackgroundIconIdCenter);
mapIconBackgroundBottom = BitmapFactory.decodeResource(res, mapBackgroundIconIdBottom);
int mapBackgroundIconIdTopSmall = getMapBackgroundIconId(pointInfo, "top", true);
int mapBackgroundIconIdCenterSmall = getMapBackgroundIconId(pointInfo, "center", true);
int mapBackgroundIconIdBottomSmall = getMapBackgroundIconId(pointInfo, "bottom", true);
mapIconBackgroundTopSmall = BitmapFactory.decodeResource(res, mapBackgroundIconIdTopSmall);
mapIconBackgroundCenterSmall = BitmapFactory.decodeResource(res, mapBackgroundIconIdCenterSmall);
mapIconBackgroundBottomSmall = BitmapFactory.decodeResource(res, mapBackgroundIconIdBottomSmall);
mapIconBackgroundTop = backgroundType.getMapBackgroundIconId(ctx, "top", false);
mapIconBackgroundCenter = backgroundType.getMapBackgroundIconId(ctx, "center", false);
mapIconBackgroundBottom = backgroundType.getMapBackgroundIconId(ctx, "bottom", false);
mapIconBackgroundTopSmall = backgroundType.getMapBackgroundIconId(ctx, "top", true);
mapIconBackgroundCenterSmall = backgroundType.getMapBackgroundIconId(ctx, "center", true);
mapIconBackgroundBottomSmall = backgroundType.getMapBackgroundIconId(ctx, "bottom", true);
syncedStroke = BitmapFactory.decodeResource(res, R.drawable.ic_shield_marker_point_stroke);
syncedColor = BitmapFactory.decodeResource(res, R.drawable.ic_shield_marker_point_color);
syncedShadow = BitmapFactory.decodeResource(res, R.drawable.ic_shield_marker_point_shadow);
@ -102,15 +98,6 @@ public class PointImageDrawable extends Drawable {
.replaceFirst("mx_", "mm_"), "drawable", ctx.getPackageName());
}
private int getMapBackgroundIconId(PointInfo pointInfo, String layer, boolean isSmall) {
Context ctx = pointInfo.ctx;
int iconId = pointInfo.backgroundType.getIconId();
String iconName = ctx.getResources().getResourceEntryName(iconId);
String suffix = isSmall ? "_small" : "";
return ctx.getResources().getIdentifier("ic_" + iconName + "_" + layer + suffix,
"drawable", ctx.getPackageName());
}
@Override
protected void onBoundsChange(Rect bounds) {
super.onBoundsChange(bounds);

View file

@ -380,7 +380,7 @@ public abstract class PointEditorFragmentNew extends BaseOsmAndFragment {
private void createShapeSelector() {
FlowLayout selectShape = view.findViewById(R.id.select_shape);
for (BackgroundType backgroundType : BackgroundType.values()) {
if (backgroundType.getNameId() != -1) {
if (backgroundType.isSelected()) {
selectShape.addView(createShapeItemView(backgroundType, selectShape),
new FlowLayout.LayoutParams(0, 0));
}

View file

@ -155,10 +155,11 @@ public class OsmBugsLayer extends OsmandMapLayer implements IContextMenuProvider
iconId = R.drawable.mx_special_symbol_check_mark;
backgroundColorRes = R.color.osm_bug_resolved_icon_color;
}
BackgroundType backgroundType = BackgroundType.COMMENT;
PointImageDrawable pointImageDrawable = PointImageDrawable.getOrCreate(activity,
ContextCompat.getColor(activity, backgroundColorRes), true, false, iconId,
BackgroundType.COMMENT);
int offsetY = pointImageDrawable.getIntrinsicHeight() / 2;
backgroundType);
int offsetY = backgroundType.getOffsetY(activity, textScale);
pointImageDrawable.drawPoint(canvas, x, y - offsetY, textScale, false);
}
this.fullObjectsLatLon = fullObjectsLatLon;

View file

@ -2,9 +2,7 @@ package net.osmand.plus.views;
import android.Manifest;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PointF;
@ -225,16 +223,17 @@ public class ContextMenuLayer extends OsmandMapLayer {
}
}
}
float scale = 1f;
float textScale = 1f;
if (!pressedLatLonSmall.isEmpty() || !pressedLatLonFull.isEmpty()) {
scale = activity.getMyApplication().getSettings().TEXT_SCALE.get();
textScale = activity.getMyApplication().getSettings().TEXT_SCALE.get();
}
for (LatLon latLon : pressedLatLonSmall.keySet()) {
int x = (int) box.getPixXFromLatLon(latLon.getLatitude(), latLon.getLongitude());
int y = (int) box.getPixYFromLatLon(latLon.getLatitude(), latLon.getLongitude());
Bitmap pressedBitmapSmall = getBackground(pressedLatLonSmall.get(latLon), true);
BackgroundType background = pressedLatLonSmall.get(latLon);
Bitmap pressedBitmapSmall = background.getTouchBackground(activity, true);
Rect destRect = getIconDestinationRect(
x, y, pressedBitmapSmall.getWidth(), pressedBitmapSmall.getHeight(), scale);
x, y, pressedBitmapSmall.getWidth(), pressedBitmapSmall.getHeight(), textScale);
canvas.drawBitmap(pressedBitmapSmall, null, destRect, paint);
}
for (LatLon latLon : pressedLatLonFull.keySet()) {
@ -242,10 +241,10 @@ public class ContextMenuLayer extends OsmandMapLayer {
int y = (int) box.getPixYFromLatLon(latLon.getLatitude(), latLon.getLongitude());
BackgroundType background = pressedLatLonFull.get(latLon);
Bitmap pressedBitmap = getBackground(background, false);
int offsetY = BackgroundType.COMMENT.equals(background) ? pressedBitmap.getHeight() / 2 : 0;
Bitmap pressedBitmap = background.getTouchBackground(activity, false);
int offsetY = background.getOffsetY(activity, textScale);
Rect destRect = getIconDestinationRect(
x, y - offsetY, pressedBitmap.getWidth(), pressedBitmap.getHeight(), scale);
x, y - offsetY, pressedBitmap.getWidth(), pressedBitmap.getHeight(), textScale);
canvas.drawBitmap(pressedBitmap, null, destRect, paint);
}
@ -271,15 +270,6 @@ public class ContextMenuLayer extends OsmandMapLayer {
}
}
private Bitmap getBackground(BackgroundType backgroundType, boolean isSmall) {
Context ctx = view.getContext();
Resources res = view.getResources();
String iconName = res.getResourceEntryName(backgroundType.getIconId());
String suffix = isSmall ? "_small" : "";
return BitmapFactory.decodeResource(res, res.getIdentifier("ic_" + iconName + "_center" + suffix,
"drawable", ctx.getPackageName()));
}
public void setSelectOnMap(CallbackWithObject<LatLon> selectOnMap) {
this.selectOnMap = selectOnMap;
}
@ -927,9 +917,9 @@ public class ContextMenuLayer extends OsmandMapLayer {
backgroundType = BackgroundType.getByTypeName(
((GPXUtilities.WptPt) o).getBackgroundType(), DEFAULT_BACKGROUND_TYPE);
}
if (lt.isPresentInFullObjects(latLon) && !pressedLatLonFull.keySet().contains(latLon)) {
if (lt.isPresentInFullObjects(latLon) && !pressedLatLonFull.containsKey(latLon)) {
pressedLatLonFull.put(latLon, backgroundType);
} else if (lt.isPresentInSmallObjects(latLon) && !pressedLatLonSmall.keySet().contains(latLon)) {
} else if (lt.isPresentInSmallObjects(latLon) && !pressedLatLonSmall.containsKey(latLon)) {
pressedLatLonSmall.put(latLon, backgroundType);
}
}