我想在Android编程视图上重绘

时间:2014-01-29 16:40:39

标签: android android-graphview

我正在创建一个应用程序,显示用户给出的输入图表。使用一些库在线性布局上绘制图形。我希望线性布局在每次用户单击绘制按钮时重绘用户请求的新功能。我尝试使用layout.invalidate()但这不起作用在我的app.please帮助..下面是代码snipet:

bb.setOnClickListener(new OnClickListener()
{
 public void onClick(View v)
    {
       String text = ee.getText().toString(); // getting the user expression input
       LinearLayout layout = (LinearLayout) findViewById(R.id.graph2);
       layout.setVisibility(View.INVISIBLE);

       Expression data = Expression.createExpression(text) ;

       if(text == ""){
           Toast.makeText(getApplicationContext(), "please enter a valid equation", Toast.LENGTH_LONG).show();
            layout.setVisibility(View.INVISIBLE);
           }
       else
           { 
               draw(data) ; 
               layout.setVisibility(View.VISIBLE);
               layout.invalidate();
           }                                                       
         } 
       });

public void draw(Expression x)
    {
        final GraphView graphing = new LineGraphView(this, "sketch");
        int num = 350;
        GraphViewData[] array = new GraphViewData[num];
        double w=0;
        for (int i=0; i<num; i++) {
           w += 0.2;
           array[i] = new GraphViewData(i, x.evaluate(w,0,0));  }


        // add data
        graphing.addSeries(new GraphViewSeries(array));
        // set view port, start=2, size=40
        graphing.setViewPort(0, 120);
        graphing.getGraphViewStyle().setNumHorizontalLabels(2);
        graphing.setScrollable(true);
        // optional - activate scaling / zooming
        graphing.setScalable(true);

        LinearLayout layout = (LinearLayout) findViewById(R.id.graph2);
        layout.addView(graphing);
        layout.invalidate();
    }

1 个答案:

答案 0 :(得分:0)

首先:

graphing.setViewPort(0, 120);

是对的吗?对我来说,看起来你将视图宽度设置为0。

接下来,您确定不想通过从布局中删除旧视图来放弃旧结果吗?

最后,添加或删除子视图时,布局会自动失效。