Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/366.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android Studio:在ScrollView中调用多个画布以显示一个接一个的图形(画布)_Java_Android_Canvas_Scrollview - Fatal编程技术网

Java Android Studio:在ScrollView中调用多个画布以显示一个接一个的图形(画布)

Java Android Studio:在ScrollView中调用多个画布以显示一个接一个的图形(画布),java,android,canvas,scrollview,Java,Android,Canvas,Scrollview,我正在为一个学校项目编写一个代码,其中必须加载一个数据文件(CSV文件、文本文件等),从获得的数据中,应用程序将数据传递到自定义绘图视图,onDraw方法将根据数据绘制/绘制图形 我的目标是让应用程序显示两个图形,一个接一个(堆叠)。加载第一组数据并绘制第一个图形。然后,加载的数据以不同的方法用于不同的计算。然后使用新数据再次调用自定义绘制视图以绘制第二个图形 当我运行应用程序时,两个图表都被绘制,但由于图形的x轴和y轴'被编码为在某些固定像素处绘制,因此第二个图形绘制在第一个图形上,因此只有第

我正在为一个学校项目编写一个代码,其中必须加载一个数据文件(CSV文件、文本文件等),从获得的数据中,应用程序将数据传递到自定义绘图视图,onDraw方法将根据数据绘制/绘制图形

我的目标是让应用程序显示两个图形,一个接一个(堆叠)。加载第一组数据并绘制第一个图形。然后,加载的数据以不同的方法用于不同的计算。然后使用新数据再次调用自定义绘制视图以绘制第二个图形

当我运行应用程序时,两个图表都被绘制,但由于图形的x轴和y轴'被编码为在某些固定像素处绘制,因此第二个图形绘制在第一个图形上,因此只有第二个图形可见

有没有什么方法可以让我画出这两个图形,这样它就不会重叠,而是看起来像是在ScrollView中堆叠的

我的代码如下所示,但我已经去掉了我认为不太重要的计算。任何帮助和指点都将不胜感激

MainActivity.java:

@覆盖
活动结果上的受保护无效(……){
super.onActivityResult(……);
开关(1){
案例1:
Graph Graph=this.findViewById(R.id.graph1);
setData(data);//加载的数据被传递到graph视图
Graph drawGraph2=this.findViewById(R.id.graph2);
graph2.setData(this.newCalculate(data));
打破
}
}
Graph.java

公共类图扩展视图{
私有油漆mPaint=新油漆();
private final int zero=700;//在700像素处标记图形的0行
公共void setData(数据){
......
}
公共图(上下文上下文,属性集属性集){
super(上下文、属性集);
}
@凌驾
测量时的保护空隙(内部宽度测量等级、内部高度测量等级){
超级测量(宽度测量、高度测量);
int widthMode=MeasureSpec.getMode(widthmasurespec);
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
设置测量尺寸(宽度尺寸、高度尺寸);
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
plotUnit(画布);//在图形上打印点
axisLabel(画布);//标记轴
axisLine(画布);//绘制轴
xyAxisMarker(画布);//标记轴
}
专用void绘图单元(画布){
......
//由于数据具有负值,因此图形反转,0开始
//在700像素(私有最终整数为零)处定义图形的
}
专用虚空axisLabel(画布){
......
}
专用void axisLine(画布,int-inset){
......
}
专用空心标记(画布){
......
}
更新 activity_main.xml


在垂直方向的
线性布局中,不能有两个视图的高度与父视图的高度相匹配。这是不可能的,因为这些视图的高度必须等于父视图的高度,但同时,它们必须一个接一个地排序,从而导致父视图高度的两倍

如果您将父视图的高度想象为
10dp
,那么每个
图形
视图也必须是10dp,这意味着父视图的高度必须是
20dp
,而不是
10dp
。这将永远循环,因此Android做一件简单的事情:视图位于第一个子视图下方,具有
Android:layou高度=“匹配父项”
将具有高度
0dp
,或者如果它们的高度是固定的,则它们将在布局外部绘制,并且不可见

例子

Android Studio IDE中布局编辑器设计选项卡的屏幕截图

在这里你可以看到:

  • 红色视图作为父线性布局
  • 紫色视图作为第一个子视图,其高度与其父级高度匹配
  • 在布局外部绘制的轮廓视图,因为它是由第一个子级使用android:layout_height=“match_parent”
推出的
  • 还有一个视图被压缩到0高度,因此不可见。您可以在XML代码中看到它
  • 此示例的XML代码:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="@android:color/holo_red_light">
    
        <LinearLayout
            android:background="@color/colorPrimary"
            android:layout_width="60dp"
            android:layout_height="match_parent" <!-- this view's height is a problem -->
            android:orientation="vertical"/>
    
        <LinearLayout
            android:background="@android:color/holo_green_dark"
            android:layout_width="120dp"
            android:layout_height="match_parent" <!-- height is not fixed, then it will be 0 -->
            android:orientation="vertical"/>
    
        <LinearLayout
            android:background="@android:color/holo_green_light"
            android:layout_width="120dp"
            android:layout_height="40dp" <!-- height is fixed, it is outlined outside of a layout -->
            android:orientation="vertical"/>
    
    </LinearLayout>
    
    结果是(我使用了两个按钮而不是图形视图):

    提示:

    • 如果要使用
      ScrollView
      ,则需要在运行时设置固定高度或定义高度
    • 去掉
      private final int zero=700;//将图形的0行标记为700像素
      。不要直接使用像素值,因为这将导致容易出错的UI。这将是“在我的手机上工作,而在其他手机上不工作”的情况。使用视图的高度作为0行

    发布您的XML布局。问题可能就在那里。@JeneaVranceanu我已经编辑了帖子并添加了我的XML布局
    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <Button
            android:id="@+id/loadbutton"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Open Data File"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />
    
        <firstapp.drawtwograph.Graph
            android:id="@+id/graph1"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@+id/graph2"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/loadbutton" />
    
        <firstapp.drawtwograph.Graph
            android:id="@+id/graph2"
            android:layout_width="wrap_content"
            android:layout_height="0dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/graph1" />
    </androidx.constraintlayout.widget.ConstraintLayout>