[英]Android java, duplicated code
我正在使用MPAndroidChart創建4個圖形。 我是一個初學者,不確定如何在不復制代碼的情況下創建圖形。
示例圖2
mChart2 = (LineChart)findViewById(R.id.resultGraph2);
mChart2.setOnChartValueSelectedListener(this);
mChart2.getDescription().setEnabled(false);
mChart2.setTouchEnabled(true);
mChart2.setDragDecelerationFrictionCoef(0.9f);
mChart2.setDragEnabled(true);
mChart2.setScaleEnabled(true);
mChart2.setDrawGridBackground(true);
mChart2.setHighlightPerDragEnabled(true);
mChart2.setPinchZoom(true);
mChart2.setBackgroundColor(Color.WHITE);
setData2();
mChart2.animateX(2500);
mChart2.getLegend().setEnabled(false);
mChart2.setExtraLeftOffset(5);
mChart2.setExtraBottomOffset(10);
示例圖4
mChart4 = (LineChart)findViewById(R.id.resultGraph4);
mChart4.setOnChartValueSelectedListener(this);
mChart4.getDescription().setEnabled(false);
mChart4.setTouchEnabled(true);
mChart4.setDragDecelerationFrictionCoef(0.9f);
mChart4.setDragEnabled(true);
mChart4.setScaleEnabled(true);
mChart4.setDrawGridBackground(true);
mChart4.setHighlightPerDragEnabled(true);
mChart4.setPinchZoom(true);
mChart4.setBackgroundColor(Color.WHITE);
setData4();
mChart4.animateX(2500);
mChart4.getLegend().setEnabled(false);
mChart4.setExtraLeftOffset(5);
mChart4.setExtraBottomOffset(10);
它們都有相同的設置。 有沒有更好的方式編寫它,而無需重復代碼?
例如:
mChart2 = (LineChart)findViewById(R.id.resultGraph2);
mChart4 = (LineChart)findViewById(R.id.resultGraph4);
applySettings(mChart2);
applySettings(mChart4);
//...
void applySettings(LineChart chart)
{
chart.setTouchEnabled(true);
chart.setScaleEnabled(true);
// etc...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.