MPAndroidChart中的getWidth()和getHeight更改

时间:2016-01-23 19:33:08

标签: android mpandroidchart

我想通过Android应用程序了解一个问题。

我使用MPAndroidChart库来显示BarChart。我使用barchart.setDescriptionPosition(x, y);将位置更改为图表的顶部。

现在,我定义了x-y-positions

        float x = barchart.getWidth()/2f + barchart.getViewPortHandler().offsetLeft();
        float y = barchart.getViewPortHandler().offsetTop();

编辑:首先调用barchart.getWidth() = 0,显然,UI未完全设置。但我在createView()中调用onActivityCreated()onCreateView()之后调用x-。为什么会这样?

奇怪的是,当我开始活动时,它看起来像这样: enter image description here

但是当我向左滑动时,它看起来像这样:

enter image description here

问题在于,不是所有的" Station 1"显示,只有结束" 1"在视图1中,与视图2相比,"站2"正确地放在中间。

因此,我定义y-positionspublic class ShowDetailedResult extends AppCompatActivity implements View.OnTouchListener{ ShowDetailedResultFragment detailedFragment; float downX= 0; float downY = 0; float upX = 0; float upY = 0; int stationsnbr = 0; int NBROFSTATIONS = 8; final static String KEYSTATION = "STATIONNBR"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_detailed_result); detailedFragment = new ShowDetailedResultFragment(); if (savedInstanceState!=null){ stationsnbr = savedInstanceState.getInt(KEYSTATION,0); } else { stationsnbr = 0; } detailedFragment.setPositions(stationsnbr); getSupportFragmentManager().beginTransaction().add(R.id.show_detailed_result_container,detailedFragment).commit(); View view =(View)findViewById(R.id.showDetailedRootView); view.setOnTouchListener(this); View contView = (View)findViewById(R.id.show_detailed_result_container); contView.setOnTouchListener(this); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { // Save the user's current game state savedInstanceState.putInt(KEYSTATION, stationsnbr); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); } @Override public boolean onTouch(View v, MotionEvent event) { final float MIN_DISTANCE = 150; switch (event.getAction()){ case MotionEvent.ACTION_DOWN: downX = event.getX(); downY = event.getY(); return true; case MotionEvent.ACTION_UP: upX = event.getX(); upY = event.getY(); float deltaX = downX - upX; float deltaY = downY - upY; if (Math.abs(deltaX) > MIN_DISTANCE){ if (deltaX < 0 ){ stationsnbr--; if (stationsnbr < 0){ stationsnbr = NBROFSTATIONS-1; } detailedFragment.setPositions(stationsnbr); detailedFragment.createView(); return true; } else { stationsnbr++; if (stationsnbr >= NBROFSTATIONS){ stationsnbr = 0; } detailedFragment.setPositions(stationsnbr); detailedFragment.createView(); } } } return false; } } 的方式有问题。

以下是活动代码:

public class ShowDetailedResultFragment extends Fragment {
    BarChart barchart;
    ArrayList<Float> detailedResults;
    final int STATIONS [][] = {{0,1,2},{3,4,5},{6,7,8}, {9,10, 19,20, 21,22}, {11,12,13}, {14,15,16}, {17, 18} , {23,24} };
    int STATIONNBR = 0;
    public ShowDetailedResultFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_show_detailed_result, container, false);
    }

    private ArrayList<BarEntry> getEntryList(){
        ArrayList<BarEntry> tmpList = new ArrayList<BarEntry>();
        int stations[] = STATIONS[STATIONNBR];
        for (int i = 0; i < stations.length; i++){
            BarEntry tmpEntry = new BarEntry(detailedResults.get(stations[i]), i );
            tmpList.add(tmpEntry);
        }
        return tmpList;
    }

    ArrayList<String> getXvals(int length){
        ArrayList<String> tmpList = new ArrayList<String>();
        for (int i = 0; i < length; i++){
            tmpList.add(Integer.toString(i));
        }
        return tmpList;
    }
    public void createView(){
        ArrayList<BarEntry> resultList = getEntryList();
        BarDataSet set = new BarDataSet(resultList,"Skeet");
        ArrayList<BarDataSet> dataSets = new ArrayList<BarDataSet>();
        dataSets.add(set);

        ArrayList<String> xVals = getXvals(resultList.size());
        BarData data = new BarData(xVals, dataSets);
        barchart.getXAxis().setDrawLabels(false);
        barchart.getAxisRight().setEnabled(false);
        barchart.setVisibleYRangeMaximum(110, YAxis.AxisDependency.LEFT);
        barchart.setData(data);
        barchart.setTouchEnabled(false);
        float x = barchart.getWidth()/2f + barchart.getViewPortHandler().offsetLeft();
        float y = barchart.getViewPortHandler().offsetTop();
    //    System.out.println("x y: " + x + " " + y);
        barchart.setDescriptionPosition(x, y);
        barchart.setDescription("Station " + Integer.toString(STATIONNBR + 1));
        barchart.invalidate();
    }
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        Utils.init(getContext());
        detailedResults = Statistic.getDetailedResults("Skeet",LiveSession.ROUNDLENGTH);
        barchart = (BarChart)getActivity().findViewById(R.id.barchart);
        createView();
    }

    public void setPositions(int stationsToShow){
        STATIONNBR = stationsToShow;
    }

}

这是片段:

createView()

大多数事情发生的函数是(x,y)-coordinates,是否可以在首次创建活动时未正确设置View的大小?

创建活动时(31,15)(255,15),刷到第2站会显示getWidth()。所以 <div class="col-xs-4 col-md-4 col-lg-4"> <figure style="background:green;height:500px;"> <img class="img-iPhone" src="images/1t.png" alt=""> </figure> <h2>1st screen</h2> </div> <!-- /.col-md-4 --> <div class="col-xs-4 col-md-4 col-lg-4"> <!-- Images showcase --> <figure style="background:blue;height:500px;"> <img class="img-iPhone" src="images/1t.png" alt=""> </figure> <h2>2nd screen</h2> </div> <!-- /.col-md-4 --> <div class="col-xs-4 col-md-4 col-lg-4"> <figure style="background:red;height:500px;"> <img class="img-iPhone" src="images/1t.png" alt=""> </figure> <h2>3rd screen</h2> </div>两者都不一样。

0 个答案:

没有答案