From c5547827f4db482dc6da8ec98121b2e726c791d5 Mon Sep 17 00:00:00 2001 From: Nazar-Kutz Date: Thu, 14 May 2020 12:23:20 +0300 Subject: [PATCH] use drawable instead of bitmap, delete unnecessary code --- OsmAnd/src/net/osmand/AndroidUtils.java | 5 -- .../mapmarkers/MapMarkersGroupsFragment.java | 53 ++++++++++--------- .../mapmarkers/MapMarkersHistoryFragment.java | 36 ++++++------- .../MapMarkersItemTouchHelperCallback.java | 31 +++++------ .../plus/render/UnscaledBitmapLoader.java | 45 ---------------- .../osmand/plus/views/FavouritesLayer.java | 1 - 6 files changed, 55 insertions(+), 116 deletions(-) delete mode 100644 OsmAnd/src/net/osmand/plus/render/UnscaledBitmapLoader.java diff --git a/OsmAnd/src/net/osmand/AndroidUtils.java b/OsmAnd/src/net/osmand/AndroidUtils.java index 10effe8363..895bfad482 100644 --- a/OsmAnd/src/net/osmand/AndroidUtils.java +++ b/OsmAnd/src/net/osmand/AndroidUtils.java @@ -711,11 +711,6 @@ public class AndroidUtils { return new BitmapDrawable(ctx.getResources(), flipBitmapHorizontally(bitmap)); } - public static Bitmap bitmapFromDrawableRes(@NonNull Context ctx, @DrawableRes int drawableResId) { - Drawable drawable = ContextCompat.getDrawable(ctx, drawableResId); - return drawableToBitmap(drawable); - } - public static Bitmap drawableToBitmap(Drawable drawable) { if (drawable instanceof BitmapDrawable) { BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable; diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java index 194231f4f8..5d368b7580 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersGroupsFragment.java @@ -1,11 +1,9 @@ package net.osmand.plus.mapmarkers; -import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffColorFilter; import android.graphics.Rect; +import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; @@ -23,7 +21,6 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.snackbar.Snackbar; -import net.osmand.AndroidUtils; import net.osmand.GPXUtilities.WptPt; import net.osmand.Location; import net.osmand.data.Amenity; @@ -48,9 +45,9 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL public static final String TAG = "MapMarkersGroupsFragment"; + private OsmandApplication app; private MapMarkersGroupsAdapter adapter; private Paint backgroundPaint = new Paint(); - private Paint iconPaint = new Paint(); private Paint textPaint = new Paint(); private Snackbar snackbar; private View mainView; @@ -60,6 +57,12 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL private boolean locationUpdateStarted; private boolean compassUpdateAllowed = true; + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + app = getMyApplication(); + } + @Nullable @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { @@ -89,9 +92,6 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL backgroundPaint.setColor(ContextCompat.getColor(getActivity(), night ? R.color.divider_color_dark : R.color.divider_color_light)); backgroundPaint.setStyle(Paint.Style.FILL_AND_STROKE); backgroundPaint.setAntiAlias(true); - iconPaint.setAntiAlias(true); - iconPaint.setFilterBitmap(true); - iconPaint.setDither(true); textPaint.setTextSize(getResources().getDimension(R.dimen.default_desc_text_size)); textPaint.setFakeBoldText(true); textPaint.setAntiAlias(true); @@ -106,8 +106,6 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { private float marginSides = getResources().getDimension(R.dimen.list_content_padding); - private Bitmap deleteBitmap = AndroidUtils.bitmapFromDrawableRes(mapActivity, R.drawable.ic_action_delete_dark); - private Bitmap historyBitmap = AndroidUtils.bitmapFromDrawableRes(mapActivity, R.drawable.ic_action_history); private boolean iconHidden; @Override @@ -149,24 +147,27 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL colorIcon = night ? R.color.icon_color_default_dark : R.color.icon_color_default_light; colorText = night ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light; } - if (colorIcon != 0) { - iconPaint.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(getActivity(), colorIcon), PorterDuff.Mode.SRC_IN)); - } - textPaint.setColor(ContextCompat.getColor(getActivity(), colorText)); + textPaint.setColor(ContextCompat.getColor(app, colorText)); + Drawable icon = app.getUIUtilities().getIcon( + dX > 0 ? R.drawable.ic_action_history : R.drawable.ic_action_delete_dark, + colorIcon); + int iconWidth = icon.getIntrinsicWidth(); + int iconHeight = icon.getIntrinsicHeight(); float textMarginTop = ((float) itemView.getHeight() - (float) textHeight) / 2; + float iconMarginTop = ((float) itemView.getHeight() - (float) iconHeight) / 2; + int iconTopY = itemView.getTop() + (int) iconMarginTop; + int iconLeftX; if (dX > 0) { + iconLeftX = itemView.getLeft() + (int) marginSides; c.drawRect(itemView.getLeft(), itemView.getTop(), dX, itemView.getBottom(), backgroundPaint); - float iconMarginTop = ((float) itemView.getHeight() - (float) historyBitmap.getHeight()) / 2; - c.drawBitmap(historyBitmap, itemView.getLeft() + marginSides, itemView.getTop() + iconMarginTop, iconPaint); - c.drawText(moveToHistoryStr, itemView.getLeft() + 2 * marginSides + historyBitmap.getWidth(), - itemView.getTop() + textMarginTop + textHeight, textPaint); + c.drawText(moveToHistoryStr, itemView.getLeft() + 2 * marginSides + iconWidth, itemView.getTop() + textMarginTop + textHeight, textPaint); } else { + iconLeftX = itemView.getRight() - iconWidth - (int) marginSides; c.drawRect(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom(), backgroundPaint); - float iconMarginTop = ((float) itemView.getHeight() - (float) deleteBitmap.getHeight()) / 2; - c.drawBitmap(deleteBitmap, itemView.getRight() - deleteBitmap.getWidth() - marginSides, itemView.getTop() + iconMarginTop, iconPaint); - c.drawText(delStr, itemView.getRight() - deleteBitmap.getWidth() - 2 * marginSides - delStrWidth, - itemView.getTop() + textMarginTop + textHeight, textPaint); + c.drawText(delStr, itemView.getRight() - iconWidth - 2 * marginSides - delStrWidth, itemView.getTop() + textMarginTop + textHeight, textPaint); } + icon.setBounds(iconLeftX, iconTopY, iconLeftX + iconWidth, iconTopY + iconHeight); + icon.draw(c); } super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } @@ -189,10 +190,10 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL final MapMarker marker = (MapMarker) item; int snackbarStringRes; if (direction == ItemTouchHelper.RIGHT) { - mapActivity.getMyApplication().getMapMarkersHelper().moveMapMarkerToHistory((MapMarker) item); + app.getMapMarkersHelper().moveMapMarkerToHistory((MapMarker) item); snackbarStringRes = R.string.marker_moved_to_history; } else { - mapActivity.getMyApplication().getMapMarkersHelper().removeMarker((MapMarker) item); + app.getMapMarkersHelper().removeMarker((MapMarker) item); snackbarStringRes = R.string.item_removed; } updateAdapter(); @@ -201,9 +202,9 @@ public class MapMarkersGroupsFragment extends Fragment implements OsmAndCompassL @Override public void onClick(View view) { if (direction == ItemTouchHelper.RIGHT) { - mapActivity.getMyApplication().getMapMarkersHelper().restoreMarkerFromHistory(marker, 0); + app.getMapMarkersHelper().restoreMarkerFromHistory(marker, 0); } else { - mapActivity.getMyApplication().getMapMarkersHelper().addMarker(marker); + app.getMapMarkersHelper().addMarker(marker); } updateAdapter(); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersHistoryFragment.java b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersHistoryFragment.java index a339a0df37..a472dbf3a7 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersHistoryFragment.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/MapMarkersHistoryFragment.java @@ -1,11 +1,9 @@ package net.osmand.plus.mapmarkers; -import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffColorFilter; import android.graphics.Rect; +import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; import android.view.LayoutInflater; @@ -22,7 +20,6 @@ import androidx.recyclerview.widget.RecyclerView; import com.google.android.material.snackbar.Snackbar; -import net.osmand.AndroidUtils; import net.osmand.plus.MapMarkersHelper; import net.osmand.plus.MapMarkersHelper.MapMarker; import net.osmand.plus.OsmandApplication; @@ -39,7 +36,6 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel private MapMarkersHistoryAdapter adapter; private OsmandApplication app; private Paint backgroundPaint = new Paint(); - private Paint iconPaint = new Paint(); private Paint textPaint = new Paint(); private Snackbar snackbar; @@ -58,9 +54,6 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel backgroundPaint.setColor(ContextCompat.getColor(getActivity(), night ? R.color.divider_color_dark : R.color.divider_color_light)); backgroundPaint.setStyle(Paint.Style.FILL_AND_STROKE); backgroundPaint.setAntiAlias(true); - iconPaint.setAntiAlias(true); - iconPaint.setFilterBitmap(true); - iconPaint.setDither(true); textPaint.setTextSize(getResources().getDimension(R.dimen.default_desc_text_size)); textPaint.setFakeBoldText(true); textPaint.setAntiAlias(true); @@ -84,8 +77,6 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel ItemTouchHelper.SimpleCallback simpleItemTouchCallback = new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT) { private float marginSides = getResources().getDimension(R.dimen.list_content_padding); - private Bitmap deleteBitmap = AndroidUtils.bitmapFromDrawableRes(app, R.drawable.ic_action_delete_dark); - private Bitmap resetBitmap = AndroidUtils.bitmapFromDrawableRes(app, R.drawable.ic_action_reset_to_default_dark); private boolean iconHidden; @Override @@ -118,22 +109,27 @@ public class MapMarkersHistoryFragment extends Fragment implements MapMarkersHel colorIcon = night ? R.color.icon_color_default_dark : R.color.icon_color_default_light; colorText = night ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light; } - if (colorIcon != 0) { - iconPaint.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(getActivity(), colorIcon), PorterDuff.Mode.SRC_IN)); - } - textPaint.setColor(ContextCompat.getColor(getActivity(), colorText)); + textPaint.setColor(ContextCompat.getColor(app, colorText)); + Drawable icon = app.getUIUtilities().getIcon( + dX > 0 ? R.drawable.ic_action_delete_dark : R.drawable.ic_action_reset_to_default_dark, + colorIcon); + int iconWidth = icon.getIntrinsicWidth(); + int iconHeight = icon.getIntrinsicHeight(); float textMarginTop = ((float) itemView.getHeight() - (float) textHeight) / 2; + float iconMarginTop = ((float) itemView.getHeight() - (float) iconHeight) / 2; + int iconTopY = itemView.getTop() + (int) iconMarginTop; + int iconLeftX; if (dX > 0) { + iconLeftX = itemView.getLeft() + (int) marginSides; c.drawRect(itemView.getLeft(), itemView.getTop(), dX, itemView.getBottom(), backgroundPaint); - float iconMarginTop = ((float) itemView.getHeight() - (float) deleteBitmap.getHeight()) / 2; - c.drawBitmap(deleteBitmap, itemView.getLeft() + marginSides, itemView.getTop() + iconMarginTop, iconPaint); - c.drawText(delStr, itemView.getLeft() + 2 * marginSides + deleteBitmap.getWidth(), itemView.getTop() + textMarginTop + textHeight, textPaint); + c.drawText(delStr, itemView.getLeft() + 2 * marginSides + iconWidth, itemView.getTop() + textMarginTop + textHeight, textPaint); } else { + iconLeftX = itemView.getRight() - iconWidth - (int) marginSides; c.drawRect(itemView.getRight() + dX, itemView.getTop(), itemView.getRight(), itemView.getBottom(), backgroundPaint); - float iconMarginTop = ((float) itemView.getHeight() - (float) resetBitmap.getHeight()) / 2; - c.drawBitmap(resetBitmap, itemView.getRight() - resetBitmap.getWidth() - marginSides, itemView.getTop() + iconMarginTop, iconPaint); - c.drawText(activateStr, itemView.getRight() - resetBitmap.getWidth() - 2 * marginSides - activateStrWidth, itemView.getTop() + textMarginTop + textHeight, textPaint); + c.drawText(activateStr, itemView.getRight() - iconWidth - 2 * marginSides - activateStrWidth, itemView.getTop() + textMarginTop + textHeight, textPaint); } + icon.setBounds(iconLeftX, iconTopY, iconLeftX + iconWidth, iconTopY + iconHeight); + icon.draw(c); } super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } diff --git a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java index 5e18b89f8a..2150bf246d 100644 --- a/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java +++ b/OsmAnd/src/net/osmand/plus/mapmarkers/adapters/MapMarkersItemTouchHelperCallback.java @@ -1,18 +1,16 @@ package net.osmand.plus.mapmarkers.adapters; -import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Paint; -import android.graphics.PorterDuff; -import android.graphics.PorterDuffColorFilter; import android.graphics.Rect; +import android.graphics.drawable.Drawable; import android.view.View; import androidx.core.content.ContextCompat; import androidx.recyclerview.widget.ItemTouchHelper; import androidx.recyclerview.widget.RecyclerView; -import net.osmand.AndroidUtils; +import net.osmand.plus.OsmandApplication; import net.osmand.plus.R; import net.osmand.plus.activities.MapActivity; @@ -23,12 +21,9 @@ public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback private boolean swipeEnabled = true; private Paint backgroundPaint = new Paint(); - private Paint iconPaint = new Paint(); private Paint textPaint = new Paint(); private float marginSides; - private Bitmap deleteBitmap; - private Bitmap historyBitmap; private boolean iconHidden; private boolean night; @@ -47,16 +42,11 @@ public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback this.mapActivity = mapActivity; this.adapter = adapter; marginSides = mapActivity.getResources().getDimension(R.dimen.list_content_padding); - deleteBitmap = AndroidUtils.bitmapFromDrawableRes(mapActivity, R.drawable.ic_action_delete_dark); - historyBitmap = AndroidUtils.bitmapFromDrawableRes(mapActivity, R.drawable.ic_action_history); night = !mapActivity.getMyApplication().getSettings().isLightContent(); backgroundPaint.setColor(ContextCompat.getColor(mapActivity, night ? R.color.divider_color_dark : R.color.divider_color_light)); backgroundPaint.setStyle(Paint.Style.FILL_AND_STROKE); backgroundPaint.setAntiAlias(true); - iconPaint.setAntiAlias(true); - iconPaint.setFilterBitmap(true); - iconPaint.setDither(true); textPaint.setTextSize(mapActivity.getResources().getDimension(R.dimen.default_desc_text_size)); textPaint.setFakeBoldText(true); textPaint.setAntiAlias(true); @@ -100,6 +90,7 @@ public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE && viewHolder instanceof MapMarkerItemViewHolder) { + OsmandApplication app = mapActivity.getMyApplication(); if (!iconHidden && isCurrentlyActive) { ((MapMarkerItemViewHolder) viewHolder).optionsBtn.setVisibility(View.GONE); iconHidden = true; @@ -115,17 +106,19 @@ public class MapMarkersItemTouchHelperCallback extends ItemTouchHelper.Callback colorIcon = night ? R.color.icon_color_default_dark : R.color.icon_color_default_light; colorText = night ? R.color.text_color_secondary_dark : R.color.text_color_secondary_light; } - if (colorIcon != 0) { - iconPaint.setColorFilter(new PorterDuffColorFilter(ContextCompat.getColor(mapActivity, colorIcon), PorterDuff.Mode.SRC_IN)); - } textPaint.setColor(ContextCompat.getColor(mapActivity, colorText)); + Drawable icon = app.getUIUtilities().getIcon(R.drawable.ic_action_history, colorIcon); + int iconWidth = icon.getIntrinsicWidth(); + int iconHeight = icon.getIntrinsicHeight(); float textMarginTop = ((float) itemView.getHeight() - (float) textHeight) / 2; + float iconMarginTop = ((float) itemView.getHeight() - (float) iconHeight) / 2; + int iconTopY = itemView.getTop() + (int) iconMarginTop; + int iconLeftX = itemView.getLeft() + (int) marginSides; c.drawRect(itemView.getLeft(), itemView.getTop(), dX, itemView.getBottom(), backgroundPaint); - float iconMarginTop = ((float) itemView.getHeight() - (float) historyBitmap.getHeight()) / 2; - c.drawBitmap(historyBitmap, itemView.getLeft() + marginSides, itemView.getTop() + iconMarginTop, iconPaint); - c.drawText(moveToHistoryStr, itemView.getLeft() + 2 * marginSides + historyBitmap.getWidth(), + c.drawText(moveToHistoryStr, itemView.getLeft() + 2 * marginSides + iconWidth, itemView.getTop() + textMarginTop + textHeight, textPaint); - + icon.setBounds(iconLeftX, iconTopY, iconLeftX + iconWidth, iconTopY + iconHeight); + icon.draw(c); } super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } diff --git a/OsmAnd/src/net/osmand/plus/render/UnscaledBitmapLoader.java b/OsmAnd/src/net/osmand/plus/render/UnscaledBitmapLoader.java deleted file mode 100644 index 59ce504107..0000000000 --- a/OsmAnd/src/net/osmand/plus/render/UnscaledBitmapLoader.java +++ /dev/null @@ -1,45 +0,0 @@ -package net.osmand.plus.render; -import android.content.res.Resources; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.graphics.BitmapFactory.Options; -import android.os.Build; -import android.util.DisplayMetrics; - -public abstract class UnscaledBitmapLoader { - - public static final UnscaledBitmapLoader instance; - - static { - instance = Integer.parseInt(Build.VERSION.SDK) < 4 ? new Old() : new New(); - } - - public static Bitmap loadFromResource(Resources resources, int resId, BitmapFactory.Options options, DisplayMetrics densityDpi) { - return instance.load(resources, resId, options, densityDpi); - } - - private static class Old extends UnscaledBitmapLoader { - - @Override - Bitmap load(Resources resources, int resId, Options options, DisplayMetrics densityDpi) { - return BitmapFactory.decodeResource(resources, resId, options); - } - - } - - private static class New extends UnscaledBitmapLoader { - - @Override - Bitmap load(Resources resources, int resId, Options options, DisplayMetrics dm) { - options = new BitmapFactory.Options(); - options.inScaled = false; - options.inTargetDensity = dm.densityDpi; - options.inDensity = dm.densityDpi; - return BitmapFactory.decodeResource(resources, resId, options); - } - - } - - abstract Bitmap load(Resources resources, int resId, BitmapFactory.Options options, DisplayMetrics densityDpi); - -} \ No newline at end of file diff --git a/OsmAnd/src/net/osmand/plus/views/FavouritesLayer.java b/OsmAnd/src/net/osmand/plus/views/FavouritesLayer.java index cf25e38814..402fd70fb8 100644 --- a/OsmAnd/src/net/osmand/plus/views/FavouritesLayer.java +++ b/OsmAnd/src/net/osmand/plus/views/FavouritesLayer.java @@ -3,7 +3,6 @@ package net.osmand.plus.views; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; -import android.graphics.Color; import android.graphics.Paint; import android.graphics.PointF; import android.graphics.PorterDuff;