Skip to content

Commit d7019ab

Browse files
authored
1.5.6 Changes (halfhp#81)
* adds convenience methods for saving and restoring pan / zoom state * halfhp#80 Target SDK 28 * readme updates
1 parent f58cd6b commit d7019ab

File tree

14 files changed

+122
-42
lines changed

14 files changed

+122
-42
lines changed

.circleci/config.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616

1717
# CircleCI maintains a library of pre-built images
1818
# documented at https://circleci.com/docs/2.0/circleci-images/
19-
- image: circleci/android:api-26-alpha
19+
- image: circleci/android:api-28-alpha
2020

2121
working_directory: ~/repo
2222

androidplot-core/src/main/java/com/androidplot/pie/PieRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ protected void drawSegment(Canvas canvas, RectF bounds, Segment seg, SegmentForm
170170
canvas.drawLine(r1Inner.x, r1Inner.y, r1Outer.x, r1Outer.y, f.getRadialEdgePaint());
171171
canvas.drawLine(r2Inner.x, r2Inner.y, r2Outer.x, r2Outer.y, f.getRadialEdgePaint());
172172
} else {
173-
canvas.save(Canvas.CLIP_SAVE_FLAG);
173+
canvas.save();
174174
Path chart = new Path();
175175
chart.addCircle(cx, cy, outerRad, Path.Direction.CW);
176176
Path inside = new Path();

androidplot-core/src/main/java/com/androidplot/ui/LayoutManager.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public void draw(Canvas canvas) throws PlotRenderException {
9292
}
9393
for (Widget widget : elements()) {
9494
try {
95-
canvas.save(Canvas.ALL_SAVE_FLAG);
95+
canvas.save();
9696
PositionMetrics metrics = widget.getPositionMetrics();
9797
float elementWidth = widget.getWidthPix(displayDims.paddedRect.width());
9898
float elementHeight = widget.getHeightPix(displayDims.paddedRect.height());
@@ -140,7 +140,7 @@ public void draw(Canvas canvas) throws PlotRenderException {
140140

141141
private static void drawSpacing(Canvas canvas, RectF outer, RectF inner, Paint paint) {
142142
try {
143-
canvas.save(Canvas.ALL_SAVE_FLAG);
143+
canvas.save();
144144
canvas.clipRect(inner, Region.Op.DIFFERENCE);
145145
canvas.drawRect(outer, paint);
146146
} finally {

androidplot-core/src/main/java/com/androidplot/ui/SeriesRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected abstract void onRender(Canvas canvas, RectF plotArea, SeriesType serie
8181

8282
public void drawSeriesLegendIcon(Canvas canvas, RectF rect, SeriesFormatterType formatter) {
8383
try {
84-
canvas.save(Canvas.ALL_SAVE_FLAG);
84+
canvas.save();
8585
canvas.clipRect(rect, Region.Op.INTERSECT);
8686
doDrawLegendIcon(canvas, rect, formatter);
8787
} finally {

androidplot-core/src/main/java/com/androidplot/ui/widget/TextLabelWidget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ public void doOnDraw(Canvas canvas, RectF widgetRect) {
101101
Anchor.CENTER);
102102

103103
try {
104-
canvas.save(Canvas.ALL_SAVE_FLAG);
104+
canvas.save();
105105
canvas.translate(start.x, start.y);
106106
switch (orientation) {
107107
case HORIZONTAL:

androidplot-core/src/main/java/com/androidplot/xy/LineAndPointRenderer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ protected void renderPath(Canvas canvas, RectF plotArea, Path path, PointF first
309309
RectF thisRegionRectF = thisRegionTransformed.asRectF();
310310
if (thisRegionRectF != null) {
311311
try {
312-
canvas.save(Canvas.ALL_SAVE_FLAG);
312+
canvas.save();
313313
canvas.clipPath(path);
314314
canvas.drawRect(thisRegionRectF, regionFormatter.getPaint());
315315
} finally {

androidplot-core/src/main/java/com/androidplot/xy/PanZoom.java

Lines changed: 69 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import android.graphics.RectF;
44
import android.graphics.PointF;
5+
import android.support.annotation.NonNull;
56
import android.view.*;
67

78
import com.androidplot.*;
89
import com.androidplot.util.*;
910

11+
import java.io.Serializable;
1012
import java.util.*;
1113

1214
/**
@@ -35,6 +37,7 @@ public class PanZoom implements View.OnTouchListener {
3537
// rectangle created by the space between two fingers
3638
protected RectF fingersRect;
3739
private View.OnTouchListener delegate;
40+
private State state = new State();
3841

3942
// Definition of the touch states
4043
protected enum DragState {
@@ -95,29 +98,83 @@ public enum ZoomLimit {
9598
MIN_TICKS
9699
}
97100

98-
protected PanZoom(XYPlot plot, Pan pan, Zoom zoom) {
101+
// TODO: consider making this immutable / threadsafe
102+
public static class State implements Serializable {
103+
private Number domainLowerBoundary;
104+
private Number domainUpperBoundary;
105+
private Number rangeLowerBoundary;
106+
private Number rangeUpperBoundary;
107+
private BoundaryMode domainBoundaryMode;
108+
private BoundaryMode rangeBoundaryMode;
109+
110+
public void setDomainBoundaries(Number lowerBoundary, Number upperBoundary, BoundaryMode mode) {
111+
this.domainLowerBoundary = lowerBoundary;
112+
this.domainUpperBoundary = upperBoundary;
113+
this.domainBoundaryMode = mode;
114+
}
115+
116+
public void setRangeBoundaries(Number lowerBoundary, Number upperBoundary, BoundaryMode mode) {
117+
this.rangeLowerBoundary = lowerBoundary;
118+
this.rangeUpperBoundary = upperBoundary;
119+
this.rangeBoundaryMode = mode;
120+
}
121+
122+
public void applyDomainBoundaries(@NonNull XYPlot plot) {
123+
plot.setDomainBoundaries(domainLowerBoundary, domainUpperBoundary, domainBoundaryMode);
124+
}
125+
126+
public void applyRangeBoundaries(@NonNull XYPlot plot) {
127+
plot.setRangeBoundaries(rangeLowerBoundary, rangeUpperBoundary, rangeBoundaryMode);
128+
}
129+
130+
public void apply(@NonNull XYPlot plot) {
131+
applyDomainBoundaries(plot);
132+
applyRangeBoundaries(plot);
133+
}
134+
}
135+
136+
protected PanZoom(@NonNull XYPlot plot, Pan pan, Zoom zoom) {
99137
this.plot = plot;
100138
this.pan = pan;
101139
this.zoom = zoom;
102140
this.zoomLimit = ZoomLimit.OUTER;
103141
}
104142

105143
// additional constructor not to break api
106-
protected PanZoom(XYPlot plot, Pan pan, Zoom zoom, ZoomLimit limit) {
144+
protected PanZoom(@NonNull XYPlot plot, Pan pan, Zoom zoom, ZoomLimit limit) {
107145
this.plot = plot;
108146
this.pan = pan;
109147
this.zoom = zoom;
110148
this.zoomLimit = limit;
111149
}
112150

151+
public State getState() {
152+
return this.state;
153+
}
154+
155+
public void setState(@NonNull State state) {
156+
this.state = state;
157+
state.apply(plot);
158+
}
159+
160+
protected void adjustRangeBoundary(Number lower, Number upper, BoundaryMode mode) {
161+
state.setRangeBoundaries(lower, upper, mode);
162+
state.applyRangeBoundaries(plot);
163+
}
164+
165+
protected void adjustDomainBoundary(Number lower, Number upper, BoundaryMode mode) {
166+
state.setDomainBoundaries(lower, upper, mode);
167+
state.applyDomainBoundaries(plot);
168+
}
169+
113170
/**
114171
* Convenience method for enabling pan/zoom behavior on an instance of {@link XYPlot}, using
115172
* a default behavior of {@link Pan#BOTH} and {@link Zoom#SCALE}.
116173
* Use {@link PanZoom#attach(XYPlot, Pan, Zoom, ZoomLimit)} for finer grain control of this behavior.
117174
* @param plot
118175
* @return
119176
*/
120-
public static PanZoom attach(XYPlot plot) {
177+
public static PanZoom attach(@NonNull XYPlot plot) {
121178
return attach(plot, Pan.BOTH, Zoom.SCALE);
122179
}
123180

@@ -130,7 +187,7 @@ public static PanZoom attach(XYPlot plot) {
130187
* @param zoom
131188
* @return
132189
*/
133-
public static PanZoom attach(XYPlot plot, Pan pan, Zoom zoom) {
190+
public static PanZoom attach(@NonNull XYPlot plot, @NonNull Pan pan, @NonNull Zoom zoom) {
134191
return attach(plot,pan,zoom, ZoomLimit.OUTER);
135192
}
136193

@@ -142,7 +199,7 @@ public static PanZoom attach(XYPlot plot, Pan pan, Zoom zoom) {
142199
* @param limit
143200
* @return
144201
*/
145-
public static PanZoom attach(XYPlot plot, Pan pan, Zoom zoom, ZoomLimit limit) {
202+
public static PanZoom attach(@NonNull XYPlot plot, @NonNull Pan pan, @NonNull Zoom zoom, @NonNull ZoomLimit limit) {
146203
PanZoom pz = new PanZoom(plot, pan, zoom, limit);
147204
plot.setOnTouchListener(pz);
148205
return pz;
@@ -237,12 +294,12 @@ protected void pan(final MotionEvent motionEvent) {
237294
if (EnumSet.of(Pan.HORIZONTAL, Pan.BOTH).contains(pan)) {
238295
Region newBounds = new Region();
239296
calculatePan(oldFirstFinger, newBounds, true);
240-
plot.setDomainBoundaries(newBounds.getMin(), newBounds.getMax(), BoundaryMode.FIXED);
297+
adjustDomainBoundary(newBounds.getMin(), newBounds.getMax(), BoundaryMode.FIXED);
241298
}
242299
if (EnumSet.of(Pan.VERTICAL, Pan.BOTH).contains(pan)) {
243300
Region newBounds = new Region();
244301
calculatePan(oldFirstFinger, newBounds, false);
245-
plot.setRangeBoundaries(newBounds.getMin(), newBounds.getMax(), BoundaryMode.FIXED);
302+
adjustRangeBoundary(newBounds.getMin(), newBounds.getMax(), BoundaryMode.FIXED);
246303
}
247304

248305
plot.redraw();
@@ -291,10 +348,9 @@ protected void calculatePan(final PointF oldFirstFinger, Region bounds, final bo
291348
}
292349

293350
protected boolean isValidScale(float scale) {
294-
if (Float.isInfinite(scale) || Float.isNaN(scale) || scale > -0.001 && scale < 0.001) {
295-
return false;
296-
}
297-
return true;
351+
return !Float.isInfinite(scale)
352+
&& !Float.isNaN(scale)
353+
&& (!(scale > -0.001) || !(scale < 0.001));
298354
}
299355

300356
protected void zoom(final MotionEvent motionEvent) {
@@ -349,14 +405,14 @@ protected void zoom(final MotionEvent motionEvent) {
349405
Zoom.STRETCH_BOTH,
350406
Zoom.SCALE).contains(zoom)) {
351407
calculateZoom(newRect, scaleX, true);
352-
plot.setDomainBoundaries(newRect.left, newRect.right, BoundaryMode.FIXED);
408+
adjustDomainBoundary(newRect.left, newRect.right, BoundaryMode.FIXED);
353409
}
354410
if (EnumSet.of(
355411
Zoom.STRETCH_VERTICAL,
356412
Zoom.STRETCH_BOTH,
357413
Zoom.SCALE).contains(zoom)) {
358414
calculateZoom(newRect, scaleY, false);
359-
plot.setRangeBoundaries(newRect.top, newRect.bottom, BoundaryMode.FIXED);
415+
adjustRangeBoundary(newRect.top, newRect.bottom, BoundaryMode.FIXED);
360416
}
361417
plot.redraw();
362418
}

androidplot-core/src/main/java/com/androidplot/xy/XYGraphWidget.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -847,7 +847,7 @@ protected void drawData(Canvas canvas) throws PlotRenderException {
847847
}
848848
try {
849849
if (isGridClippingEnabled) {
850-
canvas.save(Canvas.ALL_SAVE_FLAG);
850+
canvas.save();
851851
canvas.clipRect(gridRect, android.graphics.Region.Op.INTERSECT);
852852
}
853853

build.gradle

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ allprojects {
2323
}
2424

2525
ext {
26-
theCompileSdkVersion = 26
27-
theTargetSdkVersion = 26
26+
theCompileSdkVersion = 28
27+
theTargetSdkVersion = 28
2828
theMinSdkVersion = 5
29-
theVersionName = '1.5.5'
29+
theVersionName = '1.5.6'
3030
theVersionCode = 0
3131
}
3232

demoapp/src/main/java/com/androidplot/demos/SimpleXYPlotActivity.java

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2015 AndroidPlot.com
2+
* Copyright 2018 AndroidPlot.com
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,7 +21,6 @@
2121
import android.os.Bundle;
2222
import android.support.annotation.NonNull;
2323

24-
import com.androidplot.ui.Insets;
2524
import com.androidplot.util.PixelUtils;
2625
import com.androidplot.xy.CatmullRomInterpolator;
2726
import com.androidplot.xy.LineAndPointFormatter;
@@ -49,7 +48,7 @@ public void onCreate(Bundle savedInstanceState)
4948
setContentView(R.layout.simple_xy_plot_example);
5049

5150
// initialize our XYPlot reference:
52-
plot = (XYPlot) findViewById(R.id.plot);
51+
plot = findViewById(R.id.plot);
5352

5453
// create a couple arrays of y-values to plot:
5554
final Number[] domainLabels = {1, 2, 3, 6, 7, 8, 9, 10, 13, 14};
@@ -101,19 +100,5 @@ public Object parseObject(String source, @NonNull ParsePosition pos) {
101100
return null;
102101
}
103102
});
104-
105-
new Thread(new Runnable() {
106-
107-
@Override
108-
public void run() {
109-
try {
110-
Thread.sleep(2000);
111-
} catch (InterruptedException e) {
112-
throw new RuntimeException(e);
113-
}
114-
plot.getGraph().setGridInsets(new Insets(120, 120, 120, 120));
115-
plot.redraw();
116-
}
117-
}).start();
118103
}
119104
}

0 commit comments

Comments
 (0)