我正在尝试将渲染的图像设置到我的ImageView中,而不是打开一个新的Intent但出现错误。

iv.setImageDrawable(ChartFactory.getPieChartView(getBaseContext(), distributionSeries, defaultRenderer));


这是我的主要课程代码,

public class PieChart extends Activity {

    ImageView iv;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv = (ImageView) findViewById (R.id.imageView1);
        CreatePieChart();
    }

    private void CreatePieChart() {

        // Pie Chart Section Names
        String[] code = new String[] { "IOS", "ANDROID" };

        // Pie Chart Section Value
        double[] distribution = { 40, 60 };

        // Color of each Pie Chart Sections
        int[] colors = { Color.GRAY, Color.GREEN };

        // Instantiating CategorySeries to plot Pie Chart
        CategorySeries distributionSeries = new CategorySeries(
                "Mobile Platforms");
        for (int i = 0; i < distribution.length; i++) {
            // Adding a slice with its values and name to the Pie Chart
            distributionSeries.add(code[i], distribution[i]);
        }
        // Instantiating a renderer for the Pie Chart
        DefaultRenderer defaultRenderer = new DefaultRenderer();
        for (int i = 0; i < distribution.length; i++) {
            SimpleSeriesRenderer seriesRenderer = new SimpleSeriesRenderer();
            seriesRenderer.setColor(colors[i]);
            seriesRenderer.setDisplayChartValues(true);
            // Adding a renderer for a slice
            defaultRenderer.addSeriesRenderer(seriesRenderer);
        }
        defaultRenderer.setLegendTextSize(30);
        defaultRenderer.setBackgroundColor(Color.RED);
        defaultRenderer.setBackgroundColor(Color.RED);
        defaultRenderer.setChartTitle("Mobile Platforms");
        defaultRenderer.setChartTitleTextSize(20);
        defaultRenderer.setZoomButtonsVisible(true);
        defaultRenderer.setBackgroundColor(45454545);

        // Creating an intent to plot bar chart using dataset and
        // multipleRenderer
        //Intent intent = ChartFactory.getPieChartIntent(getBaseContext(),
                //distributionSeries, defaultRenderer,"PieChart");

        iv.setImageDrawable(ChartFactory.getPieChartView(getBaseContext(), distributionSeries, defaultRenderer));

        // Start Activity
        //startActivity(intent);

    }


另一个问题是,每当我尝试设置PieChart的背景时,它都会显示默认的黑色,

defaultRenderer.setBackgroundColor(Color.RED);

最佳答案

尝试将渲染的图像添加到Layout而不是ImageView中,

defaultRenderer = ChartFactory.getPieChartView(this, distributionSeries, defaultRenderer);

LinearLayout layout = (LinearLayout) findViewById(R.id.chart);

layout.addView(defaultRenderer, new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));


对于第二个问题,必须在应用任何颜色之前添加setApplyBackgroundColor()。

defaultRenderer.setApplyBackgroundColor(true);
defaultRenderer.setBackgroundColor(Color.RED);

关于android - 将渲染的图像设置为ImageView,而不是使用aChartEngine库在Android中打开新的Intent,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20145167/

10-08 23:10