XYPlot (AndroidPlot library) in ScrollView does no

2019-08-06 16:08发布

问题:

I am trying to put XYPlot (from AndroidPlot library http://androidplot.com/) in ScrollView container, because there will be more stuff in this Activity. Unfortunately all I am getting back is blank space, where the chart should appear (when I remove ScrollView it appears properly).

My layout xml file:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fillViewport="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <com.androidplot.xy.XYPlot
        android:id="@+id/mySimpleXYPlot"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        androidPlot.title="A Simple XY Plot"
        androidPlot.domainLabel="Domain"
        androidPlot.rangeLabel="Range"
        androidPlot.titleWidget.labelPaint.textSize="@dimen/title_font_size"
        androidPlot.domainLabelWidget.labelPaint.textSize="@dimen/domain_label_font_size"
        androidPlot.rangeLabelWidget.labelPaint.textSize="@dimen/range_label_font_size"
        androidPlot.graphWidget.marginTop="20dp"
        androidPlot.graphWidget.marginLeft="15dp"
        androidPlot.graphWidget.marginBottom="30dp"
        androidPlot.graphWidget.marginRight="10dp"
        androidPlot.graphWidget.rangeLabelPaint.textSize="@dimen/range_tick_label_font_size"
        androidPlot.graphWidget.rangeOriginLabelPaint.textSize="@dimen/range_tick_label_font_size"
        androidPlot.graphWidget.domainLabelPaint.textSize="@dimen/domain_tick_label_font_size"
        androidPlot.graphWidget.domainOriginLabelPaint.textSize="@dimen/domain_tick_label_font_size"
        androidPlot.legendWidget.textPaint.textSize="@dimen/legend_text_font_size"
        androidPlot.legendWidget.iconSizeMetrics.heightMetric.value="15dp"
        androidPlot.legendWidget.iconSizeMetrics.widthMetric.value="15dp"
        androidPlot.legendWidget.heightMetric.value="25dp"
        androidPlot.legendWidget.positionMetrics.anchor="right_bottom"
        androidPlot.graphWidget.gridLinePaint.color="#000000"/>
    </LinearLayout>
 </ScrollView>

My onCreate() method:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    XYPlot plot = (XYPlot) findViewById(R.id.mySimpleXYPlot);


    Number[] series1Numbers = {1, 8, 5, 2, 7, 4};

    XYSeries series1 = new SimpleXYSeries(
            Arrays.asList(series1Numbers), SimpleXYSeries.ArrayFormat.Y_VALS_ONLY, "Series1");                             

    LineAndPointFormatter series1Format = new LineAndPointFormatter();
    series1Format.setPointLabelFormatter(new PointLabelFormatter());
    series1Format.configure(getApplicationContext(),
            R.xml.line_point_formatter_with_plf1);

    plot.addSeries(series1, series1Format);

    plot.setTicksPerRangeLabel(3);
    plot.getGraphWidget().setDomainLabelOrientation(-45);

}

It is an example from QuickStart Tutorial, slightly truncated. I have already checked this answer: Androidplot - X-Axis labels cut off

When I change layout to:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
    <com.androidplot.xy.XYPlot
        android:id="@+id/mySimpleXYPlot"
        android:layout_width="match_parent"
        android:layout_height="match_parent" .../>

and add several TextView widgets, the chart appears, but I can't scroll to see them. Finally, when I add more TextView widgets (like a lot), chart doesn't appear again, but I can see all TextView widgets and scroll to see all of them.

Can anyone help?

回答1:

I had a somewhat similar problem. The painful part was to realise that this issue actually combines 2 separate problems.

1.) Layout definition When defining the layout for the XYPlot, instead of android:layout_height="match_parent" define the dp value you need ( something like android:layout_height="200dp")

2.) Hardware acceleration While checking the logcat, I noticed the following output

W/View﹕ View too large to fit into drawing cache, needs 10368000 bytes, only 8294400 available

A quick search led me to this stackoverflow topic. So basically I added android:hardwareAccelerated="false" to the Activity in the AndroidManifest.xml file. After that it worked, but was slow as hell. I ended up with adding androidplot.renderMode="use_background_thread" to the XYPlot layout definition and these changes did the trick for me.

Hopefully this will help.



回答2:

In your ScrollView, add android:fillViewport="true". Then, in your XYPlot, add these attributes:

android:minWidth
android:minHeight

I've set my minWidth and minHeight to my original width and height in the XYPlot. That should do the trick. I also have manually set the height of my XYPlot to 250dp. Not sure if it makes a difference.