Add functionality

This commit is contained in:
Alexander Sytnyk 2017-08-01 13:15:11 +03:00
parent 16804410f2
commit 0f69f594c7
4 changed files with 148 additions and 13 deletions

View file

@ -51,31 +51,31 @@
tools:src="@drawable/ic_action_arrow_down"/> tools:src="@drawable/ic_action_arrow_down"/>
<TextView <TextView
android:id="@+id/distance_text_view" android:id="@+id/measurement_distance_text_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_marginEnd="4dp"
android:layout_marginLeft="8dp" android:layout_marginLeft="8dp"
android:layout_marginRight="4dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_toEndOf="@id/ruler_icon" android:layout_toEndOf="@id/ruler_icon"
android:layout_toRightOf="@id/ruler_icon" android:layout_toRightOf="@id/ruler_icon"
android:text="724 m, "
android:textAppearance="@style/TextAppearance.ListItemTitle" android:textAppearance="@style/TextAppearance.ListItemTitle"
tools:text="724 m, "/> tools:text="724 m,"/>
<TextView <TextView
android:id="@+id/dots_text_view" android:id="@+id/measurement_points_text_view"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_toEndOf="@id/distance_text_view" android:layout_toEndOf="@id/measurement_distance_text_view"
android:layout_toLeftOf="@id/up_down_icon" android:layout_toLeftOf="@id/up_down_icon"
android:layout_toRightOf="@id/distance_text_view" android:layout_toRightOf="@id/measurement_distance_text_view"
android:layout_toStartOf="@id/up_down_icon" android:layout_toStartOf="@id/up_down_icon"
android:text="3 dots"
android:textColor="?android:textColorSecondary" android:textColor="?android:textColorSecondary"
android:textSize="@dimen/default_list_text_size" android:textSize="@dimen/default_list_text_size"
tools:text="3 dots"/> tools:text="points: 3"/>
</RelativeLayout> </RelativeLayout>
<View <View
@ -114,7 +114,7 @@
tools:src="@drawable/ic_action_redo_dark"/> tools:src="@drawable/ic_action_redo_dark"/>
<Button <Button
android:id="@+id/add_dot_button" android:id="@+id/add_point_button"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_alignParentEnd="true" android:layout_alignParentEnd="true"

View file

@ -9,6 +9,8 @@
3. All your modified/created strings are in the top of the file (to make easier find what\'s translated). 3. All your modified/created strings are in the top of the file (to make easier find what\'s translated).
PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy PLEASE: Have a look at http://code.google.com/p/osmand/wiki/UIConsistency, it may really improve your and our work :-) Thx - Hardy
--> -->
<string name="measurement_points">points: %o</string>
<string name="measurement_distance">%s,</string>
<string name="measurement_tool">Measurement tool</string> <string name="measurement_tool">Measurement tool</string>
<string name="quick_action_resume_pause_navigation">Resume/Pause Navigation</string> <string name="quick_action_resume_pause_navigation">Resume/Pause Navigation</string>
<string name="quick_action_resume_pause_navigation_descr">Press this button to pause the navigation, or to resume it if it was already paused.</string> <string name="quick_action_resume_pause_navigation_descr">Press this button to pause the navigation, or to resume it if it was already paused.</string>

View file

@ -8,6 +8,7 @@ import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.TextView;
import net.osmand.AndroidUtils; import net.osmand.AndroidUtils;
import net.osmand.plus.IconsCache; import net.osmand.plus.IconsCache;
@ -20,6 +21,12 @@ public class MeasurementToolFragment extends Fragment {
private MapActivity mapActivity; private MapActivity mapActivity;
private MeasurementToolLayer measurementLayer; private MeasurementToolLayer measurementLayer;
private TextView distanceTv;
private TextView pointsTv;
private String distanceSt;
private String pointsSt;
private boolean wasCollapseButtonVisible; private boolean wasCollapseButtonVisible;
@Nullable @Nullable
@ -31,11 +38,17 @@ public class MeasurementToolFragment extends Fragment {
final boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls(); final boolean nightMode = mapActivity.getMyApplication().getDaynightHelper().isNightModeForMapControls();
final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme; final int themeRes = nightMode ? R.style.OsmandDarkTheme : R.style.OsmandLightTheme;
distanceSt = mapActivity.getString(R.string.measurement_distance);
pointsSt = mapActivity.getString(R.string.measurement_points);
View view = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_measurement_tool, null); View view = View.inflate(new ContextThemeWrapper(getContext(), themeRes), R.layout.fragment_measurement_tool, null);
final View mainView = view.findViewById(R.id.main_view); final View mainView = view.findViewById(R.id.main_view);
AndroidUtils.setBackground(mapActivity, mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark); AndroidUtils.setBackground(mapActivity, mainView, nightMode, R.drawable.bg_bottom_menu_light, R.drawable.bg_bottom_menu_dark);
distanceTv = (TextView) mainView.findViewById(R.id.measurement_distance_text_view);
pointsTv = (TextView) mainView.findViewById(R.id.measurement_points_text_view);
((ImageView) mainView.findViewById(R.id.ruler_icon)) ((ImageView) mainView.findViewById(R.id.ruler_icon))
.setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_ruler, R.color.color_myloc_distance)); .setImageDrawable(iconsCache.getIcon(R.drawable.ic_action_ruler, R.color.color_myloc_distance));
((ImageView) mainView.findViewById(R.id.up_down_icon)) ((ImageView) mainView.findViewById(R.id.up_down_icon))
@ -45,6 +58,14 @@ public class MeasurementToolFragment extends Fragment {
((ImageView) mainView.findViewById(R.id.next_dot_icon)) ((ImageView) mainView.findViewById(R.id.next_dot_icon))
.setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_redo_dark)); .setImageDrawable(iconsCache.getThemedIcon(R.drawable.ic_action_redo_dark));
mainView.findViewById(R.id.add_point_button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
measurementLayer.addPointOnClick();
updateText();
}
});
enterMeasurementMode(); enterMeasurementMode();
return view; return view;
@ -56,8 +77,14 @@ public class MeasurementToolFragment extends Fragment {
exitMeasurementMode(); exitMeasurementMode();
} }
private void updateText() {
distanceTv.setText(String.format(distanceSt, measurementLayer.getDistanceSt()));
pointsTv.setText(String.format(pointsSt, measurementLayer.getPointsCount()));
}
private void enterMeasurementMode() { private void enterMeasurementMode() {
measurementLayer.setInMeasurementMode(true); measurementLayer.setInMeasurementMode(true);
mapActivity.refreshMap();
mapActivity.disableDrawer(); mapActivity.disableDrawer();
mark(View.INVISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info); mark(View.INVISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info);
mark(View.GONE, R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button, mark(View.GONE, R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button,
@ -70,10 +97,13 @@ public class MeasurementToolFragment extends Fragment {
} else { } else {
wasCollapseButtonVisible = false; wasCollapseButtonVisible = false;
} }
updateText();
} }
private void exitMeasurementMode() { private void exitMeasurementMode() {
measurementLayer.setInMeasurementMode(false); measurementLayer.setInMeasurementMode(false);
mapActivity.refreshMap();
mapActivity.enableDrawer(); mapActivity.enableDrawer();
mark(View.VISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info, mark(View.VISIBLE, R.id.map_left_widgets_panel, R.id.map_right_widgets_panel, R.id.map_center_info,
R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button, R.id.map_route_info_button, R.id.map_menu_button, R.id.map_compass_button, R.id.map_layers_button,
@ -83,6 +113,8 @@ public class MeasurementToolFragment extends Fragment {
if (collapseButton != null && wasCollapseButtonVisible) { if (collapseButton != null && wasCollapseButtonVisible) {
collapseButton.setVisibility(View.VISIBLE); collapseButton.setVisibility(View.VISIBLE);
} }
measurementLayer.clearPoints();
} }
private void mark(int status, int... widgets) { private void mark(int status, int... widgets) {

View file

@ -1,31 +1,132 @@
package net.osmand.plus.measurementtool; package net.osmand.plus.measurementtool;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas; import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import net.osmand.data.LatLon;
import net.osmand.data.QuadPoint;
import net.osmand.data.RotatedTileBox; import net.osmand.data.RotatedTileBox;
import net.osmand.plus.GPXUtilities.WptPt;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.R;
import net.osmand.plus.views.OsmandMapLayer; import net.osmand.plus.views.OsmandMapLayer;
import net.osmand.plus.views.OsmandMapTileView; import net.osmand.plus.views.OsmandMapTileView;
import net.osmand.util.MapUtils;
import java.util.LinkedList;
public class MeasurementToolLayer extends OsmandMapLayer { public class MeasurementToolLayer extends OsmandMapLayer {
private OsmandMapTileView view;
private boolean inMeasurementMode; private boolean inMeasurementMode;
private LinkedList<WptPt> measurementPoints = new LinkedList<>();
private Bitmap centerIconDay;
private Bitmap centerIconNight;
private Bitmap pointIcon;
private Paint bitmapPaint;
private RenderingLineAttributes lineAttrs;
private Path path;
private int marginX;
private int marginY;
@Override
public void initLayer(OsmandMapTileView view) {
this.view = view;
centerIconDay = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_ruler_center_day);
centerIconNight = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_ruler_center_night);
pointIcon = BitmapFactory.decodeResource(view.getResources(), R.drawable.map_pedestrian_location);
bitmapPaint = new Paint();
bitmapPaint.setAntiAlias(true);
bitmapPaint.setDither(true);
bitmapPaint.setFilterBitmap(true);
lineAttrs = new RenderingLineAttributes("rulerLine");
path = new Path();
marginY = pointIcon.getHeight() / 2;
marginX = pointIcon.getWidth() / 2;
}
public boolean isInMeasurementMode() { public boolean isInMeasurementMode() {
return inMeasurementMode; return inMeasurementMode;
} }
public void setInMeasurementMode(boolean inMeasurementMode) { void setInMeasurementMode(boolean inMeasurementMode) {
this.inMeasurementMode = inMeasurementMode; this.inMeasurementMode = inMeasurementMode;
} }
@Override int getPointsCount() {
public void initLayer(OsmandMapTileView view) { return measurementPoints.size();
}
String getDistanceSt() {
float dist = 0;
if (measurementPoints.size() > 0) {
for (int i = 1; i < measurementPoints.size(); i++) {
dist += MapUtils.getDistance(measurementPoints.get(i - 1).lat, measurementPoints.get(i - 1).lon,
measurementPoints.get(i).lat, measurementPoints.get(i).lon);
}
}
return OsmAndFormatter.getFormattedDistance(dist, view.getApplication());
}
void clearPoints() {
measurementPoints.clear();
} }
@Override @Override
public void onDraw(Canvas canvas, RotatedTileBox tileBox, DrawSettings settings) { public void onDraw(Canvas canvas, RotatedTileBox tb, DrawSettings settings) {
if (inMeasurementMode) {
lineAttrs.updatePaints(view, settings, tb);
drawCenterIcon(canvas, tb, tb.getCenterPixelPoint(), settings.isNightMode());
if (measurementPoints.size() > 0) {
path.reset();
for (int i = 0; i < measurementPoints.size(); i++) {
WptPt pt = measurementPoints.get(i);
int locX = tb.getPixXFromLonNoRot(pt.lon);
int locY = tb.getPixYFromLatNoRot(pt.lat);
if (i == 0) {
path.moveTo(locX, locY);
} else {
path.lineTo(locX, locY);
}
if (tb.containsLatLon(pt.lat, pt.lon)) {
canvas.drawBitmap(pointIcon, locX - marginX, locY - marginY, bitmapPaint);
}
}
path.lineTo(tb.getCenterPixelX(), tb.getCenterPixelY());
canvas.drawPath(path, lineAttrs.paint);
}
}
}
private void drawCenterIcon(Canvas canvas, RotatedTileBox tb, QuadPoint center, boolean nightMode) {
canvas.rotate(-tb.getRotate(), center.x, center.y);
if (nightMode) {
canvas.drawBitmap(centerIconNight, center.x - centerIconNight.getWidth() / 2,
center.y - centerIconNight.getHeight() / 2, bitmapPaint);
} else {
canvas.drawBitmap(centerIconDay, center.x - centerIconDay.getWidth() / 2,
center.y - centerIconDay.getHeight() / 2, bitmapPaint);
}
canvas.rotate(tb.getRotate(), center.x, center.y);
}
void addPointOnClick() {
RotatedTileBox tb = view.getCurrentRotatedTileBox();
LatLon l = tb.getLatLonFromPixel(tb.getCenterPixelX(), tb.getCenterPixelY());
WptPt pt = new WptPt();
pt.lat = l.getLatitude();
pt.lon = l.getLongitude();
measurementPoints.add(pt);
view.refreshMap();
} }
@Override @Override