开发者

How can I pass an int to my onDraw(Canvas canvas) in Android?

I use a surfaceview to draw a pie chart in Android. In order to know how big the pie slice should be I need to pass a parameter to the onDraw() method. How can I do this? Inside the onDraw() I make a query to a datahelper-class that fetches the right data.

I tried to call a static function in one Activity from the onDraw, and which function returned an integer. But I want something more dynamic, so I can send the integers I need to from the Activity to the onDraw and just get the result in form of a pie 开发者_JAVA技巧chart.

Any suggestions?


One of solutions can be like this:

public class MySurfaceView extends SurfaceView
{
    private MyParameter parameter;

    public void setParameter(MyParameter parameter)
    {
        this.parameter=parameter;
    }

    @Override
    public void onDraw(Canvas canvas)
    {
        if(this.parameter==null)
            return;  //nothing to draw...
        //draw here...
    }
}

//somewhere in code

MySurfaceView sView=(SurfaceView )findViewById(R.id.pie_chart);
//
sView.setParameter(new MyParameter(100,2000, false));

}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜