在Android中的Widget中显示实时图形

时间:2016-04-01 06:30:01

标签: android widget android-widget achartengine

我想在小部件中使用图形。就像This一样!我想使用<?xml version="1.0" encoding="utf-8"?> <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:context="com.spykid.navig.navig_sample.Main2Activity"> <android.support.design.widget.AppBarLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:theme="@style/AppTheme.AppBarOverlay"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar" android:layout_width="match_parent" android:layout_height="?attr/actionBarSize" android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" /> </android.support.design.widget.AppBarLayout> <include layout="@layout/content_main" /> <android.support.design.widget.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@android:drawable/ic_dialog_email" /> </android.support.design.widget.CoordinatorLayout> 制作图表。使用AchartEngine的图表在普通应用程序中工作,但如何在窗口小部件中实现它?

这是我的代码:

AchartEngine

我想在小部件应用中使用Above代码。请帮帮我。!!

public class MainActivity extends ActionBarActivity {

    private XYMultipleSeriesDataset mDataset = getDemoDataset();
    private XYMultipleSeriesRenderer mRenderer = getDemoRenderer();
    private GraphicalView mChartView;

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

        setRendererStyling();

        if (mChartView == null) {
            LinearLayout layout = (LinearLayout) findViewById(R.id.chart);

            mChartView = ChartFactory.getLineChartView(MainActivity.this,
                        mDataset, mRenderer);

            mRenderer.setSelectableBuffer(100);

            layout.addView(mChartView, new LayoutParams(
                    LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        } else {
            mChartView.repaint();
        }

    }

    private void setRendererStyling() {

        mRenderer.setApplyBackgroundColor(true);
        mRenderer.setBackgroundColor(Color.argb(100, 50, 50, 50));
        mRenderer.setAxisTitleTextSize(16);
        mRenderer.setChartTitleTextSize(20);
        mRenderer.setLabelsTextSize(15);
        mRenderer.setLegendTextSize(15);
        mRenderer.setMargins(new int[] { 20, 30, 15, 0 });
        mRenderer.setZoomButtonsVisible(true);
        mRenderer.setPointSize(8);

    }

    private XYMultipleSeriesRenderer getDemoRenderer() {
        XYMultipleSeriesRenderer renderer = new XYMultipleSeriesRenderer();
        renderer.setMargins(new int[] { 20, 30, 15, 0 });
        XYSeriesRenderer r = new XYSeriesRenderer();
        r.setColor(Color.BLUE);
        r.setPointStyle(PointStyle.SQUARE);
        // r.setFillBelowLine(true);
        // r.setFillBelowLineColor(Color.WHITE);
        r.setFillPoints(true);
        renderer.addSeriesRenderer(r);

        r = new XYSeriesRenderer();
        r.setPointStyle(PointStyle.CIRCLE);
        r.setColor(Color.GREEN);
        r.setFillPoints(true);
        renderer.addSeriesRenderer(r);
        renderer.setAxesColor(Color.DKGRAY);
        renderer.setLabelsColor(Color.LTGRAY);
        return renderer;
    }

    private XYMultipleSeriesDataset getDemoDataset() {

        double[] seriesFirstY = { 20, -20, 63, 150, -49, 24, 91, -34, -8 };
        double[] seriesSecondY = { 10, 70, -40, -20, 130, 24, 167, -34, 80 };

        double[] seriesFirstX = { 0, 8, 14, 20, 35, 40, 56, 80, 109 };
        double[] seriesSecondX = { 0, 24, 67, 75, 95, 134, 149, 168, 180 };

        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();

        XYSeries firstSeries = new XYSeries("series One");
        for (int i = 0; i < 9; i++)
            firstSeries.add(seriesFirstX[i], seriesFirstY[i]);
        dataset.addSeries(firstSeries);

        XYSeries secondSeries = new XYSeries("series Two");
        for (int j = 0; j < 9; j++)
            secondSeries.add(seriesSecondX[j], seriesSecondY[j]);
        dataset.addSeries(secondSeries);
        return dataset;
    }

我尝试了这段代码,但它不起作用......请帮助我。

0 个答案:

没有答案