ฉันกำลังเขียนโค้ดสำหรับโครงการของโรงเรียนที่ต้องโหลดไฟล์ข้อมูล (ไฟล์ CSV ไฟล์ข้อความ ฯลฯ) และจากข้อมูลที่ได้รับ แอปจะส่งข้อมูลไปยังมุมมองการวาดที่กำหนดเอง และวิธีการ onDraw จะวาด /plot กราฟตามข้อมูล
เป้าหมายของฉันคือให้แอปแสดงกราฟ 2 อันต่อกัน (ซ้อนกัน) โหลดข้อมูลชุดแรกแล้ววาดกราฟชุดที่ 1 ข้อมูลที่โหลดจะถูกนำมาใช้สำหรับการคำนวณที่แตกต่างกันด้วยวิธีการอื่น มุมมองการวาดแบบกำหนดเองจะถูกเรียกอีกครั้งพร้อมกับข้อมูลใหม่เพื่อวาดกราฟที่ 2
เมื่อฉันเรียกใช้แอป แผนภูมิทั้งสองจะถูกวาด แต่เนื่องจากแกน x และ y' ของกราฟถูกเขียนโค้ดให้วาดที่พิกเซลคงที่บางพิกเซล กราฟที่ 2 จะถูกวาดทับกราฟแรก ดังนั้นจึงมองเห็นเฉพาะกราฟที่ 2 เท่านั้น
มีวิธีใดบ้างที่ฉันสามารถวาดกราฟ 2 อันเพื่อไม่ให้ทับซ้อนกันและดูเหมือนจะซ้อนกันใน ScrollView แทน
รหัสของฉันแสดงอยู่ด้านล่าง แต่ฉันได้กำจัดการคำนวณที่ฉันคิดว่าไม่สำคัญออกไปแล้ว ความช่วยเหลือและคำแนะนำใด ๆ จะได้รับการชื่นชมอย่างมาก!
MainActivity.java:
@Override
protected void onActivityResult(......) {
super.onActivityResult(......);
switch (1) {
case 1:
Graph graph = this.findViewById(R.id.graph1);
graph.setData(data); // the loaded data is passed to Graph View
Graph drawGraph2 = this.findViewById(R.id.graph2);
graph2.setData(this.newCalculate(data));
break;
}
}
Graph.java
public class Graph extends View {
private Paint mPaint = new Paint();
private final int zero = 700; // mark the 0 line of graph at 700 pixels
public void setData(data){
......
}
public Graph(Context context, AttributeSet attributeSet) {
super(context, attributeSet);
}
@Override
protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(widthSize, heightSize);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
plotUnit(canvas); // plot points on graph
axisLabel(canvas); // label axis
axisLine(canvas); // draw axis
xyAxisMarker(canvas); // mark axis
}
private void plotUnit(Canvas canvas) {
......
// Due to data having negative values, the graph is inverted and the 0 starts
// of the graph is defined at 700 pixels (private final int zero)
}
private void axisLabel(Canvas canvas) {
......
}
private void axisLine(Canvas canvas, int inset) {
......
}
private void xyAxisMarker(Canvas canvas) {
......
}
อัปเดต
กิจกรรม_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="@+id/loadbutton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Data File" />
<firstapp.drawtwograph.Graph
android:id="@+id/graph1"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<firstapp.drawtwograph.Graph
android:id="@+id/graph2"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>