gpt4 book ai didi

android - 如何将图形设置为无穷大?

转载 作者:行者123 更新时间:2023-11-30 00:00:13 25 4
gpt4 key购买 nike

我找到了示例“如何使用 GraphView 创建绘图”。并针对我的任务进行了修改。接下来的任务是:“构造 y = ax + b 的图形”。但是如何将图形设置为无穷大?或者如何限制 Viewport?我只想切割图形的边缘。该功能看起来不受限制。

我想看到的: What I want to see

我有: What I have

主要 Activity .java:

package practice.application.graphviewtutorial1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.series.DataPoint;
import com.jjoe64.graphview.series.LineGraphSeries;

public class MainActivity extends AppCompatActivity {

LineGraphSeries<DataPoint> series;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Button button = (Button)findViewById(R.id.button2);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double y,x;
x = -5.0;
GraphView graph = (GraphView) findViewById(R.id.graph1);
series = new LineGraphSeries<DataPoint>();
EditText editText = (EditText)findViewById(R.id.editText);
EditText editText2 = (EditText)findViewById(R.id.editText2);
double a = Double.parseDouble(editText.getText().toString());
double b = Double.parseDouble(editText2.getText().toString());;
for(int i =0; i<100; i++) {
x = x + 0.1;
y = a* x + b;
series.appendData(new DataPoint(x, y), true, 100);
}
graph.addSeries(series);
graph.getViewport().setMaxY(series.getHighestValueY());
graph.getViewport().setMaxX(series.getHighestValueX());
}
});
}

activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="practice.application.graphviewtutorial1.MainActivity"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">


<EditText
android:id="@+id/editText"
android:layout_width="195dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="number" />

<Button
android:id="@+id/button2"
android:layout_width="221dp"
android:layout_height="46dp"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.jjoe64.graphview.GraphView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/graph1" />


</LinearLayout>

最佳答案

由于您正在处理线性函数并且正如@Psytho 评论中所指出的,您所需要的只是开始和结束数据点:

检查这段代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

Button button = (Button) findViewById(R.id.button2);

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
double y, x;
x = -5.0;
GraphView graph = (GraphView) findViewById(R.id.graph1);
series = new LineGraphSeries<DataPoint>();
EditText editText = (EditText) findViewById(R.id.editText);
EditText editText2 = (EditText) findViewById(R.id.editText2);
double a = 0, b = 0;
try {
a = Double.parseDouble(editText.getText().toString());
b = Double.parseDouble(editText2.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}

// Start point
x = -10;
y = getY(x, a, b);
series.appendData(new DataPoint(x, y), true, 100);

// End point
x = 10;
y = getY(x, a, b);
series.appendData(new DataPoint(x, y), true, 100);

// Manually set the boundaries
graph.addSeries(series);
graph.getViewport().setMaxY(10);
graph.getViewport().setMaxX(10);

graph.getViewport().setMinY(-10);
graph.getViewport().setMinX(-10);
}
});

这会给你想要的效果。

关于android - 如何将图形设置为无穷大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50067702/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com