Refactor to optimize memory consumption and rendering speed

This commit is contained in:
cepprice 2021-03-21 20:54:58 +05:00
parent f16b46295d
commit 170c70e42b
3 changed files with 45 additions and 30 deletions

View file

@ -77,18 +77,27 @@ public class GpxGeometryWay extends GeometryWay<GpxGeometryWayContext, GeometryW
public static class GeometryArrowsStyle extends GeometryWayStyle<GpxGeometryWayContext> {
private static final float TRACK_WIDTH_THRESHOLD = 8f;
private static final float TRACK_WIDTH_THRESHOLD_DP = 8f;
private static final float ARROW_DISTANCE_MULTIPLIER = 1.5f;
private static final float SPECIAL_ARROW_DISTANCE_MULTIPLIER = 10f;
private final float TRACK_WIDTH_THRESHOLD_PIX;
private Bitmap arrowBitmap;
public static final int OUTER_CIRCLE_COLOR = 0x33000000;
protected int pointColor;
protected int trackColor;
protected float trackWidth;
private float circleAngleOffset;
private float outerCircleRadius;
private float innerCircleRadius;
GeometryArrowsStyle(GpxGeometryWayContext context, int arrowColor, int trackColor, float trackWidth) {
this(context, null, arrowColor, trackColor, trackWidth);
circleAngleOffset = AndroidUtils.dpToPx(context.getCtx(), 1);
outerCircleRadius = AndroidUtils.dpToPx(context.getCtx(), 8);
innerCircleRadius = AndroidUtils.dpToPx(context.getCtx(), 7);
}
GeometryArrowsStyle(GpxGeometryWayContext context, Bitmap arrowBitmap, int arrowColor, int trackColor, float trackWidth) {
@ -97,6 +106,7 @@ public class GpxGeometryWay extends GeometryWay<GpxGeometryWayContext, GeometryW
this.pointColor = arrowColor;
this.trackColor = trackColor;
this.trackWidth = trackWidth;
TRACK_WIDTH_THRESHOLD_PIX = AndroidUtils.dpToPx(context.getCtx(), TRACK_WIDTH_THRESHOLD_DP);
}
@Override
@ -136,8 +146,20 @@ public class GpxGeometryWay extends GeometryWay<GpxGeometryWayContext, GeometryW
return trackWidth;
}
public float getCircleAngleOffset() {
return circleAngleOffset;
}
public float getOuterCircleRadius() {
return outerCircleRadius;
}
public float getInnerCircleRadius() {
return innerCircleRadius;
}
public boolean useSpecialArrow() {
return trackWidth <= AndroidUtils.dpToPx(getCtx(), TRACK_WIDTH_THRESHOLD);
return trackWidth <= TRACK_WIDTH_THRESHOLD_PIX;
}
@Override

View file

@ -11,12 +11,15 @@ import androidx.core.content.ContextCompat;
public class GpxGeometryWayContext extends GeometryWayContext {
private Paint circlePaint;
private final Bitmap specialArrowBitmap;
public GpxGeometryWayContext(Context ctx, float density) {
super(ctx, density);
Paint paint = getPaintIcon();
paint.setStrokeCap(Paint.Cap.ROUND);
setupCirclePaint();
specialArrowBitmap = AndroidUtils.drawableToBitmap(ContextCompat.getDrawable(ctx, R.drawable.mm_special_arrow_up));
}
@ -28,4 +31,16 @@ public class GpxGeometryWayContext extends GeometryWayContext {
public Bitmap getSpecialArrowBitmap() {
return specialArrowBitmap;
}
public Paint getCirclePaint() {
return circlePaint;
}
private void setupCirclePaint() {
circlePaint = new Paint();
circlePaint.setDither(true);
circlePaint.setAntiAlias(true);
circlePaint.setStyle(Paint.Style.FILL);
circlePaint.setColor(0x33000000);
}
}

View file

@ -5,7 +5,6 @@ import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
@ -25,11 +24,8 @@ public class GpxGeometryWayDrawer extends GeometryWayDrawer<GpxGeometryWayContex
private static class ArrowPathPoint extends PathPoint {
private Bitmap circleBitmap;
ArrowPathPoint(float x, float y, double angle, GeometryWayStyle<?> style) {
super(x, y, angle, style);
createCircleBitmap((GeometryArrowsStyle) style);
}
@Override
@ -63,32 +59,14 @@ public class GpxGeometryWayDrawer extends GeometryWayDrawer<GpxGeometryWayContex
}
private void drawCircle(Canvas canvas, GeometryArrowsStyle style) {
float offset = circleBitmap.getWidth() / 2f;
float angleOffset = AndroidUtils.dpToPx(style.getCtx(), 1);
double rad = Math.toRadians(angle + 90);
float x = (float) (this.x - offset - angleOffset * Math.cos(rad));
float y = (float) (this.y - offset - angleOffset * Math.sin(rad));
canvas.drawBitmap(circleBitmap, x, y, null);
}
private void createCircleBitmap(GeometryArrowsStyle style) {
Context ctx = style.getCtx();
int size = AndroidUtils.dpToPx(ctx, 16);
circleBitmap = Bitmap.createBitmap(size, size, style.getPointBitmap().getConfig());
Paint paint = new Paint(Paint.DITHER_FLAG | Paint.ANTI_ALIAS_FLAG);
paint.setStyle(Paint.Style.FILL);
Canvas c = new Canvas(circleBitmap);
paint.setColor(0x33000000);
Path path = new Path();
path.addCircle(size / 2f, size / 2f, AndroidUtils.dpToPx(ctx, 8), Path.Direction.CW);
c.drawPath(path, paint);
float x = (float) (this.x - style.getCircleAngleOffset() * Math.cos(rad));
float y = (float) (this.y - style.getCircleAngleOffset() * Math.sin(rad));
Paint paint = style.getContext().getCirclePaint();
paint.setColor(GeometryArrowsStyle.OUTER_CIRCLE_COLOR);
canvas.drawCircle(x, y, style.getOuterCircleRadius(), paint);
paint.setColor(style.getTrackColor());
path.reset();
path.addCircle(size / 2f, size / 2f, AndroidUtils.dpToPx(ctx, 7), Path.Direction.CW);
c.drawPath(path, paint);
canvas.drawCircle(x, y, style.getInnerCircleRadius(), paint);
}
}
}