FoldLineView
這是一個折線圖的自定義View,用戶可以自定義添加線的條數,具體使用方法如下。
項目地址:https://github.com/NoClay/UtilsDemo
屬性
自定義屬性如下:
<declare-styleable name="FoldLineView">
<attr name="backLineColor" format="color"/>
背景線的顏色
<attr name="lineColor" format="color"/>
折線的顏色,可以不設置,通過Java代碼可以設置每條線的顏色
<attr name="labelColor" format="color"/>
標籤的顏色,即x,y軸的顏色
<attr name="labelSize" format="dimension"/>
標籤的字體的大小
<attr name="xScaleNum" format="integer"/>
x軸的格子的個數,默認爲6
<attr name="yScaleNum" format="integer"/>
y軸的格子的個數,默認爲9
<attr name="yStart" format="float"/>
y軸的範圍,爲低谷值
<attr name="yEnd" format="float"/>
y軸的範圍,爲高峯值
</declare-styleable>
方法
普通方法
以上屬性均可以通過set,get方法進行獲取,此處不再列舉
public List<Integer> getColors()
public void setColors(List<Integer> colors)
public void startDrawing()
public void stopDrawing()
控制是否刷新視圖,注意在方法調用時,start只能啓動沒有開始的視圖,建議在onResume方法中調用startDrawing,在onPause中調用stopDrawing
如何定義對應的數據格式
通過接口實現對應的方法
public class Data implements FoldLineInterface public interface FoldLineInterface { List<Float> getLinesAsList(); 獲取對應折線上的點,有多少個點,就代表需要有多少條折線 即獲取當x軸爲某一個值的時候,y軸的數據情況 String getLabel(); 獲取x軸的標籤 }
通過繼承抽象類實現
public class Data extends FoldLineBean public abstract class FoldLineBean implements FoldLineInterface{ @Override public List<Float> getLinesAsList() { return null; } @Override public String getLabel() { return null; } }
如何傳入數據
利用FoldLineAdapter傳入,無論是實現接口還是繼承,均通過該方法設置
public class DemoFoldLineView extends AppCompatActivity implements FoldLineView.onScrollChartListener{
FoldLineView mFoldLineView;
List<MeasureFoldLine> data;
List<Integer> colors;
FoldLineAdapter<MeasureFoldLine> mAdapter;
private static final String TAG = "DemoFoldLineView";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_demo_fold_line_view);
setupView();
setupData();
setupColor();
mAdapter = new FoldLineAdapter<>(data);
mFoldLineView.setColors(colors);
mFoldLineView.setAdapter(mAdapter);
mFoldLineView.setOnScrollChartListener(this);
mFoldLineView.startDrawing();
}
private void setupColor() {
if (colors == null){
colors = new ArrayList<>();
}
colors.add(Color.RED);
colors.add(Color.GREEN);
colors.add(Color.BLUE);
}
private void setupData() {
if (data == null){
data = new ArrayList<>();
}else{
data.clear();
}
Random random = new Random();
for (int i = 0; i < 100; i++) {
MeasureFoldLine m = new MeasureFoldLine(
(float)(random.nextInt(600) - 300),
(float)(random.nextInt(600) - 300),
(float)(random.nextInt(600) - 300),
i +"");
data.add(m);
}
}
private void setupView() {
mFoldLineView = (FoldLineView) findViewById(R.id.foldLine);
}
@Override
protected void onResume() {
super.onResume();
mFoldLineView.startDrawing();
}
@Override
protected void onPause() {
super.onPause();
mFoldLineView.stopDrawing();
}
@Override
public void onScroll(int left, int right) {
Log.d(TAG, "onScroll: left = " + left);
Log.d(TAG, "onScroll: right = " + right);
}
}
接口定義
public interface OnScrollChartListener {
void onScroll(int left, int right);
}
當滑動的時候,會默認的調用該接口,返回滑動時窗口的左邊序號和右邊序號
注意:實現該接口的時候,默認接口是在子線程調用的,請利用Handler和Message機制切換到UI線程。