Androidplot饼图ontouch无法正常工作

时间:2017-10-04 04:52:05

标签: androidplot

我的代码:

public class ChartPieDemoActivity extends AppCompatActivity
{
  PieChart chartPie;

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

    chartPie = (PieChart)findViewById(R.id.apChartViewer);

    createChart();
  }


  private void chart_OnTouch(MotionEvent motionEvent)
  {
    if(motionEvent.getAction() == MotionEvent.ACTION_DOWN)
    {
      PointF click = new PointF(motionEvent.getX(), motionEvent.getY());
      if(chartPie.getPie().containsPoint(click))
      {
        Segment segment = chartPie.getRenderer(PieRenderer.class).getContainingSegment(click);
        if(segment != null)
        {
          Toast.makeText(this, segment.getTitle() + " : " + String.valueOf(segment.getValue()), Toast.LENGTH_LONG).show());
        }
      }
    }
  }

  public void createChart()
  {
    final float padding = PixelUtils.dpToPix(10);
    chartPie.getPie().setPadding(padding, padding, padding, padding);

    Segment s1 = new Segment("", 18);
//    Segment s2 = new Segment("", 0);
    Segment s3 = new Segment("", 2);
  //  Segment s4 = new Segment("", 2);
    //Segment s5 = new Segment("", 3);
//    Segment s6 = new Segment("", 4);

    SegmentFormatter sf1 = new SegmentFormatter(Color.RED, Color.RED);
    SegmentFormatter sf2 = new SegmentFormatter(Color.CYAN, Color.CYAN);
    SegmentFormatter sf3 = new SegmentFormatter(Color.GREEN, Color.GREEN);
    SegmentFormatter sf4 = new SegmentFormatter(Color.DKGRAY, Color.DKGRAY);
    SegmentFormatter sf5 = new SegmentFormatter(Color.YELLOW, Color.YELLOW);
    SegmentFormatter sf6 = new SegmentFormatter(Color.MAGENTA, Color.MAGENTA);

    chartPie.addSegment(s1, sf1);
  //  chartPie.addSegment(s2, sf2);
    chartPie.addSegment(s3, sf3);
    //chartPie.addSegment(s4, sf4);
    //chartPie.addSegment(s5, sf5);
    //chartPie.addSegment(s6, sf6);

    chartPie.setClickable(true);
    chartPie.getBorderPaint().setColor(Color.TRANSPARENT);
    chartPie.getBackgroundPaint().setColor(Color.WHITE);
    chartPie.setTitle("Total Items");
    chartPie.getTitle().setWidth(PixelUtils.dpToPix(300));
    chartPie.getTitle().setHeight(PixelUtils.dpToPix(24));
    chartPie.getTitle().setMarginBottom(20);
    chartPie.getTitle().getLabelPaint().setColor(Color.BLACK);

    chartPie.setOnTouchListener(new View.OnTouchListener()
    {
      @Override
      public boolean onTouch(View v, MotionEvent event)
      {
        chart_OnTouch(event);
        return false;
      }
    });

    PieRenderer pieRenderer = chartPie.getRenderer(PieRenderer.class);
    pieRenderer.setExtentDegs(360);
  }
}

这会产生 Pie with two segments

当我单击黑色矩形区域中的任意位置时,onTouch事件始终返回空段。如果我的饼中只有一个段,或者如果一个段是零值而另一个段是值,则单击的段将返回null,这也会发生。为什么? (我使用的是1.5.1)

0 个答案:

没有答案