Skip to content

Commit 6d9fe31

Browse files
committed
* Cleaned up widget demo.
* Fixed a bug in XYPlotTest.
1 parent b991b89 commit 6d9fe31

8 files changed

Lines changed: 59 additions & 25 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@
2323
* Defines physical dimensions & scaling characteristics
2424
*/
2525
public class Size {
26+
27+
// convenience value; sets size to 100% width and height of the widget container.
28+
public static Size FILL = new Size(0, SizeLayoutType.FILL, 0, SizeLayoutType.FILL);
29+
2630
private SizeMetric height;
2731
private SizeMetric width;
2832

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
* if the total size of the owning plot is 120 pixels and val is set to 50 then the size of the widget along the associated axis
2626
* is 60; 50% of 120 = 60.
2727
*
28-
* FILL - Widget completely fills along the associated axis, minus
28+
* FILL - Widget completely fills along the associated axis, minus the input size value
2929
*/
3030
public enum SizeLayoutType {
3131
ABSOLUTE,

androidplot-core/src/test/java/com/androidplot/xy/XYPlotTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -437,21 +437,19 @@ public void testSetRangeOrigin() throws Exception {
437437

438438
@Test
439439
public void testConfigure() throws Exception {
440-
//Context context = Mockit.setUpMock(new MockContext());
441-
//Context context = new MockContext.MockContext2();
442440
HashMap<String, String> params = new HashMap<String, String>();
443441
String param1 = "this is a test.";
444442
String param2 = Plot.RenderMode.USE_BACKGROUND_THREAD.toString();
445443
String param3 = "#FF0000";
446444
params.put("title", param1);
447445
params.put("renderMode", param2);
448446
params.put("backgroundPaint.color", param3);
449-
params.put("graphWidget.domainLabelPaint.color", param3);
447+
params.put("graphWidget.domainTickLabelPaint.color", param3);
450448

451449
Configurator.configure(RuntimeEnvironment.application, plot, params);
452450
assertEquals(param1, plot.getTitle());
453451
assertEquals(Plot.RenderMode.USE_BACKGROUND_THREAD, plot.getRenderMode());
454452
assertEquals(Color.parseColor(param3), plot.getBackgroundPaint().getColor());
455-
assertEquals(Color.parseColor(param3), plot.getGraphWidget().getDomainLabelPaint().getColor());
453+
assertEquals(Color.parseColor(param3), plot.getGraphWidget().getDomainTickLabelPaint().getColor());
456454
}
457455
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ public void onCreate(Bundle savedInstanceState)
4141
// initialize our XYPlot reference:
4242
plot = (XYPlot) findViewById(R.id.plot);
4343

44+
plot.getLayoutManager().moveToBottom(plot.getTitleWidget());
45+
4446
// create a couple arrays of y-values to plot:
4547
Number[] series1Numbers = {1, 4, 2, 8, 4, 16, 8, 32, 16, 64};
4648
Number[] series2Numbers = {5, 2, 10, 5, 20, 10, 40, 20, 80, 40};
@@ -90,6 +92,5 @@ public void onCreate(Bundle savedInstanceState)
9092

9193
// rotate domain labels 45 degrees to make them more compact horizontally:
9294
plot.getGraphWidget().setDomainLabelOrientation(-45);
93-
9495
}
9596
}

demoapp/src/main/java/com/androidplot/demos/widget/DemoAppWidgetProvider.java

Lines changed: 45 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@
2020
import android.appwidget.AppWidgetProvider;
2121
import android.content.Context;
2222
import android.graphics.Bitmap;
23+
import android.graphics.Canvas;
2324
import android.graphics.Color;
2425
import android.widget.RemoteViews;
2526
import com.androidplot.demos.R;
27+
import com.androidplot.ui.*;
28+
import com.androidplot.util.PixelUtils;
2629
import com.androidplot.xy.XYSeries;
2730
import com.androidplot.xy.LineAndPointFormatter;
2831
import com.androidplot.xy.SimpleXYSeries;
@@ -36,15 +39,40 @@ public class DemoAppWidgetProvider extends AppWidgetProvider {
3639
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
3740
for (int widgetId : appWidgetIds) {
3841
XYPlot plot = new XYPlot(context, "Widget Example");
39-
//plot.getLayoutParams().height = 100;
40-
//plot.getLayoutParams().width = 100;
41-
plot.measure(150,150);
42-
plot.layout(0,0,150,150);
43-
plot.setDrawingCacheEnabled(true);
42+
final int h = (int) context.getResources().getDimension(R.dimen.sample_widget_height);
43+
final int w = (int) context.getResources().getDimension(R.dimen.sample_widget_width);
4444

45+
plot.getGraphWidget().setMargins(0, 0, 0 , 0);
46+
plot.getGraphWidget().setPadding(0, 0, 0, 0);
47+
plot.getGraphWidget().getGridBox().setPadding(20, 25, 0, 20);
48+
plot.getGraphWidget().getDomainTickLabelPaint().setTextSize(PixelUtils.spToPix(10));
49+
plot.getGraphWidget().getRangeTickLabelPaint().setTextSize(PixelUtils.spToPix(10));
50+
plot.getGraphWidget().getDomainOriginTickLabelPaint().setTextSize(PixelUtils.spToPix(10));
51+
plot.getGraphWidget().getRangeOriginTickLabelPaint().setTextSize(PixelUtils.spToPix(10));
52+
53+
plot.getGraphWidget().position(0, XLayoutStyle.ABSOLUTE_FROM_LEFT, 0,
54+
YLayoutStyle.ABSOLUTE_FROM_TOP, AnchorPosition.LEFT_TOP);
55+
56+
plot.getGraphWidget().setSize(Size.FILL);
57+
58+
plot.getLayoutManager().moveToTop(plot.getTitleWidget());
59+
60+
plot.measure(w, h);
61+
plot.layout(0, 0, w, h);
62+
63+
/*plot.getGraphWidget().setMarginBottom(PixelUtils.dpToPix(40));
64+
plot.getGraphWidget().setMarginLeft(PixelUtils.dpToPix(80));
65+
plot.getGraphWidget().setPaddingLeft(PixelUtils.dpToPix(80));
66+
plot.setPlotMargins(0, 0, 0, 0);
67+
plot.getGraphWidget().position(0, XLayoutStyle.ABSOLUTE_FROM_LEFT, 0,
68+
YLayoutStyle.ABSOLUTE_FROM_TOP, AnchorPosition.LEFT_TOP);
69+
70+
plot.getGraphWidget().setSize(Size.FILL);*/
4571
// Create a couple arrays of y-values to plot:
46-
Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
47-
Number[] series2Numbers = {4, 6, 3, 8, 2, 10};
72+
//Number[] series1Numbers = {1, 8, 5, 2, 7, 4};
73+
//Number[] series2Numbers = {4, 6, 3, 8, 2, 10};
74+
Number[] series1Numbers = {1, 4, 2, 8, 4, 16, 8, 32, 16, 64};
75+
Number[] series2Numbers = {5, 2, 10, 5, 20, 10, 40, 20, 80, 40};
4876

4977
// Turn the above arrays into XYSeries':
5078
XYSeries series1 = new SimpleXYSeries(
@@ -53,13 +81,14 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
5381
"Series1"); // Set the display title of the series
5482

5583
// same as above
56-
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");
84+
XYSeries series2 = new SimpleXYSeries(Arrays.asList(series2Numbers),
85+
SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series2");
5786

5887
// Create a formatter to use for drawing a series using LineAndPointRenderer:
5988
LineAndPointFormatter series1Format = new LineAndPointFormatter(
6089
Color.rgb(0, 200, 0), // line color
6190
Color.rgb(0, 100, 0), // point color
62-
null, null); // fill color (none)
91+
null, null); // fill color (none)
6392

6493
// add a new series' to the xyplot:
6594
plot.addSeries(series1, series1Format);
@@ -72,15 +101,16 @@ public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] a
72101

73102
// reduce the number of range labels
74103
plot.setTicksPerRangeLabel(3);
104+
plot.setTicksPerDomainLabel(2);
75105

76-
// by default, AndroidPlot displays developer guides to aid in laying out your plot.
77-
// To get rid of them call disableAllMarkup():
78-
//plot.disableAllMarkup();
79-
80-
Bitmap bmp = plot.getDrawingCache();
106+
// hide the legend:
107+
plot.getLegendWidget().setVisible(false);
81108

82109
RemoteViews rv = new RemoteViews(context.getPackageName(), R.layout.demo_app_widget);
83-
rv.setBitmap(R.id.imgView, "setImageBitmap", bmp);
110+
111+
Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
112+
plot.draw(new Canvas(bitmap));
113+
rv.setImageViewBitmap(R.id.imgView, bitmap);
84114
appWidgetManager.updateAppWidget(widgetId, rv);
85115
}
86116
}

demoapp/src/main/res/layout/demo_app_widget.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
<ImageView
2424
xmlns:android="http://schemas.android.com/apk/res/android"
2525
android:id="@+id/imgView"
26-
android:background="@android:drawable/alert_dark_frame"
2726
android:scaleType="centerInside"
2827
android:layout_width="fill_parent"
2928
android:layout_height="fill_parent">

demoapp/src/main/res/values/dimens.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@
2929
<dimen name="range_tick_label_font_size">15sp</dimen>
3030
<dimen name="domain_tick_label_font_size">15sp</dimen>
3131
<dimen name="legend_text_font_size">20sp</dimen>
32+
33+
<dimen name="sample_widget_height">72dp</dimen>
34+
<dimen name="sample_widget_width">294dp</dimen>
3235
</resources>

demoapp/src/main/res/xml/demo_app_widget_provider_info.xml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717
-->
1818

1919
<appwidget-provider xmlns:android="http://schemas.android.com/apk/res/android"
20-
android:minWidth="294dp"
21-
android:minHeight="72dp"
20+
android:minWidth="@dimen/sample_widget_width"
21+
android:minHeight="@dimen/sample_widget_height"
2222
android:updatePeriodMillis="86400000"
23-
android:previewImage="@drawable/ic_launcher"
2423
android:initialLayout="@layout/demo_app_widget"
2524
android:resizeMode="horizontal|vertical">
2625
</appwidget-provider>

0 commit comments

Comments
 (0)