Android图表不起作用

时间:2018-07-01 00:44:57

标签: android listview charts android-drawable

我一直在努力找出为什么我的图表无法生成。我有一个HomeExchange类,其对象由用户输入,然后成功存储在列表中。

我想绘制一个饼图,以显示具有与“ getNrCamere()”属性相同的“ nrCamere”属性值的对象总数。但是它只显示一个红色圆圈。我做错了什么?谢谢

public class Grafic extends View {
public Grafic(Context context) {
    super(context);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    HashMap<Integer,Integer> contor = new HashMap<Integer, Integer>();

    for(HomeExchange hm:lista) {
        int f=0;
        try {
            f = contor.get(hm.getNrCamere());
        } catch(Exception ert) { }
        f++;
        contor.put(hm.getNrCamere(),f);
    }

    for(Map.Entry<Integer,Integer> pair:contor.entrySet()) {
        contor.put(pair.getKey(),(int)((float)pair.getValue()/(float)lista.size()*360));
    }

    int unghi=0;
    for(Map.Entry<Integer,Integer> pair : contor.entrySet()){
        ShapeDrawable shape=new ShapeDrawable(new ArcShape(unghi,pair.getValue()/ lista.size() * 360));
        unghi+=pair.getValue()/ lista.size() * 360;
        shape.setBounds(100,100,800,800);
        Random r=new Random(255);
        shape.getPaint().setColor(Color.rgb(r.nextInt(),r.nextInt(),r.nextInt()));
        shape.draw(canvas);
    }
}
}

和:

public class CentralizatorGraficActivity extends AppCompatActivity {

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

    Grafic g=new Grafic(getApplicationContext());
    setContentView(g);
}
}

0 个答案:

没有答案