Added direction arrow to favorites on dashboard

This commit is contained in:
Denis 2014-12-10 17:13:58 +02:00
parent f4f29cc587
commit c8718f8a42
8 changed files with 197 additions and 94 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.3 KiB

View file

@ -28,11 +28,20 @@
android:textColor="@color/dashboard_black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/distance"
android:textSize="12sp"
android:textColor="@color/dashboard_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<LinearLayout android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:visibility="gone"
android:id="@+id/direction"
android:layout_width="10dp"
android:layout_height="10dp"/>
<TextView android:id="@+id/distance"
android:textSize="12sp"
android:textColor="@color/dashboard_blue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
<ImageButton android:id="@+id/navigate_to"

View file

@ -41,6 +41,7 @@ import net.osmand.plus.activities.MapActivityActions;
import net.osmand.plus.activities.OsmandListActivity;
import net.osmand.plus.dialogs.DirectionsDialogs;
import net.osmand.plus.render.RenderingIcons;
import net.osmand.plus.views.DirectionDrawable;
import net.osmand.util.Algorithms;
import net.osmand.util.OpeningHoursParser;
import net.osmand.util.OpeningHoursParser.OpeningHours;
@ -117,7 +118,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
private Handler uiHandler;
private OsmandSettings settings;
private Path directionPath = new Path();
private float width = 24;
private float height = 24;
@ -235,8 +236,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
uiHandler = new Handler();
searchFilter = (EditText) findViewById(R.id.SearchFilter);
searchFilterLayout = findViewById(R.id.SearchFilterLayout);
directionPath = createDirectionPath();
settings = ((OsmandApplication) getApplication()).getSettings();
searchFilter.addTextChangedListener(new TextWatcher(){
@ -296,33 +296,6 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
getListView().addFooterView(ll);
}
private Path createDirectionPath() {
int h = 15;
int w = 4;
float sarrowL = 8; // side of arrow
float harrowL = (float) Math.sqrt(2) * sarrowL; // hypotenuse of arrow
float hpartArrowL = (float) (harrowL - w) / 2;
Path path = new Path();
path.moveTo(width / 2, height - (height - h) / 3);
path.rMoveTo(w / 2, 0);
path.rLineTo(0, -h);
path.rLineTo(hpartArrowL, 0);
path.rLineTo(-harrowL / 2, -harrowL / 2); // center
path.rLineTo(-harrowL / 2, harrowL / 2);
path.rLineTo(hpartArrowL, 0);
path.rLineTo(0, h);
Matrix pathTransform = new Matrix();
WindowManager mgr = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
DisplayMetrics dm = new DisplayMetrics();
mgr.getDefaultDisplay().getMetrics(dm);
pathTransform.postScale(dm.density, dm.density);
path.transform(pathTransform);
width *= dm.density;
height *= dm.density;
return path;
}
@Override
protected void onResume() {
super.onResume();
@ -793,7 +766,7 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
}
}
if(loc != null){
DirectionDrawable draw = new DirectionDrawable();
DirectionDrawable draw = new DirectionDrawable(SearchPOIActivity.this, width, height, false);
Float h = heading;
float a = h != null ? h : 0;
draw.setAngle(mes[1] - a + 180);
@ -903,56 +876,4 @@ public class SearchPOIActivity extends OsmandListActivity implements OsmAndCompa
b.show();
}
class DirectionDrawable extends Drawable {
Paint paintRouteDirection;
private float angle;
public DirectionDrawable(){
paintRouteDirection = new Paint();
paintRouteDirection.setStyle(Style.FILL_AND_STROKE);
paintRouteDirection.setColor(getResources().getColor(R.color.color_unknown));
paintRouteDirection.setAntiAlias(true);
}
public void setOpenedColor(int opened){
if(opened == 0){
paintRouteDirection.setColor(getResources().getColor(R.color.color_ok));
} else if(opened == -1){
paintRouteDirection.setColor(getResources().getColor(R.color.color_unknown));
} else {
paintRouteDirection.setColor(getResources().getColor(R.color.color_warning));
}
}
public void setAngle(float angle){
this.angle = angle;
}
@Override
public void draw(Canvas canvas) {
canvas.rotate(angle, width/2, height/2);
canvas.drawPath(directionPath, paintRouteDirection);
}
@Override
public int getOpacity() {
return 0;
}
@Override
public void setAlpha(int alpha) {
paintRouteDirection.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paintRouteDirection.setColorFilter(cf);
}
}
}

View file

@ -3,6 +3,7 @@ package net.osmand.plus.dashboard;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.TypedValue;
@ -13,16 +14,16 @@ import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import net.osmand.Location;
import net.osmand.data.FavouritePoint;
import net.osmand.data.LatLon;
import net.osmand.plus.FavouritesDbHelper;
import net.osmand.plus.OsmAndAppCustomization;
import net.osmand.plus.OsmAndFormatter;
import net.osmand.plus.R;
import net.osmand.plus.*;
import net.osmand.plus.activities.MapActivity;
import net.osmand.plus.activities.search.SearchPOIActivity;
import net.osmand.plus.base.FavoriteImageDrawable;
import net.osmand.plus.dialogs.DirectionsDialogs;
import net.osmand.plus.helpers.FontCache;
import net.osmand.plus.views.DirectionDrawable;
import net.osmand.util.MapUtils;
import java.util.ArrayList;
@ -33,8 +34,13 @@ import java.util.List;
/**
* Created by Denis on 24.11.2014.
*/
public class DashFavoritesFragment extends DashBaseFragment {
public class DashFavoritesFragment extends DashBaseFragment implements OsmAndLocationProvider.OsmAndCompassListener, OsmAndLocationProvider.OsmAndLocationListener {
public static final String TAG = "DASH_FAVORITES_FRAGMENT";
private net.osmand.Location location = null;
private Float heading = null;
private List<ImageView> arrows = new ArrayList<ImageView>();
List<FavouritePoint> points = new ArrayList<FavouritePoint>();
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.dash_favorites_fragment, container, false);
@ -56,16 +62,29 @@ public class DashFavoritesFragment extends DashBaseFragment {
return view;
}
@Override
public void onPause() {
super.onPause();
getLocationProvider().removeCompassListener(this);
getLocationProvider().removeLocationListener(this);
}
@Override
public void onResume() {
super.onResume();
getLocationProvider().addLocationListener(this);
getLocationProvider().addCompassListener(this);
location = getLocationProvider().getLastKnownLocation();
updateLocation(location);
setupFavorites();
}
private void setupFavorites(){
View mainView = getView();
final FavouritesDbHelper helper = getMyApplication().getFavorites();
final List<FavouritePoint> points = new ArrayList<FavouritePoint>(helper.getFavouritePoints());
points = new ArrayList<FavouritePoint>(helper.getFavouritePoints());
arrows.clear();
if (points.size() == 0){
(mainView.findViewById(R.id.main_fav)).setVisibility(View.GONE);
return;
@ -96,6 +115,14 @@ public class DashFavoritesFragment extends DashBaseFragment {
TextView name = (TextView) view.findViewById(R.id.name);
TextView label = (TextView) view.findViewById(R.id.distance);
ImageView icon = (ImageView) view.findViewById(R.id.icon);
ImageView direction = (ImageView) view.findViewById(R.id.direction);
if(location != null){
direction.setVisibility(View.VISIBLE);
updateArrow(point, direction);
arrows.add(direction);
}
name.setText(point.getName());
icon.setImageDrawable(FavoriteImageDrawable.getOrCreate(getActivity(), point.getColor()));
LatLon lastKnownMapLocation = getMyApplication().getSettings().getLastKnownMapLocation();
@ -125,4 +152,44 @@ public class DashFavoritesFragment extends DashBaseFragment {
favorites.addView(view);
}
}
private void updateArrows() {
if (location == null) {
return;
}
for(int i =0; i<arrows.size(); i++){
updateArrow(points.get(i), arrows.get(i));
}
}
private void updateArrow(FavouritePoint point, ImageView direction) {
float[] mes = new float[2];
LatLon l = new LatLon(point.getLatitude(), point.getLongitude());
Location.distanceBetween(l.getLatitude(), l.getLongitude(), location.getLatitude(), location.getLongitude(), mes);
DirectionDrawable draw = new DirectionDrawable(getActivity(), 10, 10, true);
Float h = heading;
float a = h != null ? h : 0;
draw.setAngle(mes[1] - a + 180);
direction.setImageDrawable(draw);
}
@Override
public void updateCompassValue(float value) {
heading = value;
updateArrows();
}
@Override
public void updateLocation(Location location) {
if (location == null){
return;
}
this.location = location;
updateArrows();
}
private OsmAndLocationProvider getLocationProvider() {
return getMyApplication().getLocationProvider();
}
}

View file

@ -0,0 +1,106 @@
package net.osmand.plus.views;
import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import net.osmand.plus.R;
/**
* Created by Denis on 10.12.2014.
*/
public class DirectionDrawable extends Drawable {
Paint paintRouteDirection;
float width;
float height;
Context ctx;
boolean favorite;
private float angle;
public DirectionDrawable(Context ctx, float width, float height, boolean favorite) {
this.favorite = favorite;
this.ctx = ctx;
this.width = width;
this.height = height;
paintRouteDirection = new Paint();
paintRouteDirection.setStyle(Paint.Style.FILL_AND_STROKE);
paintRouteDirection.setColor(ctx.getResources().getColor(R.color.color_unknown));
paintRouteDirection.setAntiAlias(true);
}
public void setOpenedColor(int opened) {
if (opened == 0) {
paintRouteDirection.setColor(ctx.getResources().getColor(R.color.color_ok));
} else if (opened == -1) {
paintRouteDirection.setColor(ctx.getResources().getColor(R.color.color_unknown));
} else {
paintRouteDirection.setColor(ctx.getResources().getColor(R.color.color_warning));
}
}
public void setAngle(float angle) {
this.angle = angle;
}
@Override
public void draw(Canvas canvas) {
if (favorite) {
canvas.rotate(angle, canvas.getHeight()/2, canvas.getWidth()/2);
Bitmap arrow = BitmapFactory.decodeResource(ctx.getResources(), R.drawable.ic_destination_arrow);
canvas.drawBitmap(arrow, 0, 0, new Paint());
} else {
canvas.rotate(angle, width / 2, height / 2);
Path directionPath = createDirectionPath();
canvas.drawPath(directionPath, paintRouteDirection);
}
}
@Override
public int getOpacity() {
return 0;
}
@Override
public void setAlpha(int alpha) {
paintRouteDirection.setAlpha(alpha);
}
@Override
public void setColorFilter(ColorFilter cf) {
paintRouteDirection.setColorFilter(cf);
}
private Path createDirectionPath() {
int h = 15;
int w = 4;
float sarrowL = 8; // side of arrow
float harrowL = (float) Math.sqrt(2) * sarrowL; // hypotenuse of arrow
float hpartArrowL = (float) (harrowL - w) / 2;
Path path = new Path();
path.moveTo(width / 2, height - (height - h) / 3);
path.rMoveTo(w / 2, 0);
path.rLineTo(0, -h);
path.rLineTo(hpartArrowL, 0);
path.rLineTo(-harrowL / 2, -harrowL / 2); // center
path.rLineTo(-harrowL / 2, harrowL / 2);
path.rLineTo(hpartArrowL, 0);
path.rLineTo(0, h);
DisplayMetrics dm = new DisplayMetrics();
Matrix pathTransform = new Matrix();
WindowManager mgr = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
mgr.getDefaultDisplay().getMetrics(dm);
pathTransform.postScale(dm.density, dm.density);
path.transform(pathTransform);
width *= dm.density;
height *= dm.density;
return path;
}
}