Compare commits

...

2 Commits

Author SHA1 Message Date
Loch Christian (uib05376)
9c1899fe42 Optimize TimeRecordView 2020-11-09 21:32:06 +01:00
Loch Christian (uib05376)
82bd01d9c8 Change View background to black, add control widgets 2020-11-09 20:04:55 +01:00
5 changed files with 226 additions and 63 deletions

View File

@ -25,6 +25,7 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"]) implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0' implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1' implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.cardview:cardview:1.0.0'
testImplementation 'junit:junit:4.12' testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2' androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0' androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'

View File

@ -4,6 +4,7 @@ import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout; import androidx.constraintlayout.widget.ConstraintLayout;
import android.os.Bundle; import android.os.Bundle;
import android.os.Looper;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.LinearLayout; import android.widget.LinearLayout;
@ -24,7 +25,7 @@ public class MainActivity extends AppCompatActivity implements Observer {
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
this.track = new Track(40, 200); this.track = new Track(25, 100);
this.track.addObserver(this); this.track.addObserver(this);
this.viewStack = (LinearLayout) findViewById(R.id.trackViewStack); this.viewStack = (LinearLayout) findViewById(R.id.trackViewStack);
@ -35,17 +36,20 @@ public class MainActivity extends AppCompatActivity implements Observer {
final Track trackRef = this.track; final Track trackRef = this.track;
//final TimeRecordView viewRef = this.trackView; //final TimeRecordView viewRef = this.trackView;
final LinearLayout viewStackRef = this.viewStack; final LinearLayout viewStackRef = this.viewStack;
final List<VehicleTimeRecord> newRecords = (List<VehicleTimeRecord>) o; //final List<VehicleTimeRecord> newRecords = (List<VehicleTimeRecord>) o;
final MainActivity mainActivity = this; final MainActivity mainActivity = this;
runOnUiThread(new Runnable() { runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
TimeRecordView newTrView = new TimeRecordView(mainActivity, newRecords, trackRef.getTrackLength()); TimeRecordView newTrView = new TimeRecordView(mainActivity, trackRef);
viewStackRef.addView(newTrView, 0); if (viewStackRef.getChildCount() > 0)
int childCount = viewStackRef.getChildCount(); viewStackRef.removeViewAt(0);
if (childCount > 30) { viewStackRef.addView(newTrView);
viewStackRef.removeViewAt(childCount-1); //viewStackRef.addView(newTrView, 0);
} //int childCount = viewStackRef.getChildCount();
//if (childCount > 30) {
// viewStackRef.removeViewAt(childCount-1);
//}
} }
}); });

View File

@ -11,17 +11,17 @@ import java.util.List;
public class TimeRecordView extends View { public class TimeRecordView extends View {
protected Paint paint; protected Paint paint;
protected List<VehicleTimeRecord> records; protected Track track;
protected int pixelPerVehicle; protected int pixelPerVehicle;
protected float trackLength;
public TimeRecordView(Context context, List<VehicleTimeRecord> records, float trackLength) { public TimeRecordView(Context context, Track track) {
super(context); super(context);
this.records = records; this.track = track;
this.trackLength = trackLength;
this.paint = new Paint(); this.paint = new Paint();
paint.setColor(Color.BLACK); paint.setColor(Color.BLACK);
paint.setStyle(Paint.Style.FILL_AND_STROKE); paint.setStyle(Paint.Style.FILL_AND_STROKE);
this.setBackgroundColor(Color.BLACK);
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
} }
protected int getColor(float curVelocity, float maxVelocity) { protected int getColor(float curVelocity, float maxVelocity) {
@ -42,25 +42,32 @@ public class TimeRecordView extends View {
@Override @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, 20); super.onMeasure(widthMeasureSpec, 10*50);
setMeasuredDimension(widthMeasureSpec, 20); setMeasuredDimension(widthMeasureSpec, 10*50);
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
} }
@Override @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) { protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh); super.onSizeChanged(w, h, oldw, oldh);
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
} }
@Override @Override
protected void onDraw(Canvas canvas) { protected void onDraw(Canvas canvas) {
this.pixelPerVehicle = (int) (this.getWidth() / this.trackLength); int y = 0;
int i = 0; List<List<VehicleTimeRecord>> stepList = this.track.getVtrList();
for (VehicleTimeRecord r : this.records) { for (int curStepIdx = stepList.size()-1; curStepIdx >= stepList.size()-1-50 && curStepIdx >= 0; curStepIdx--) {
int left = (int) (this.pixelPerVehicle * r.getPosition()); List<VehicleTimeRecord> step = stepList.get(curStepIdx);
this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity())); int i = 0;
canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1, for (VehicleTimeRecord r : step) {
20, this.paint); int left = (int) (this.pixelPerVehicle * r.getPosition());
i++; this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
canvas.drawRect(left, y, left + this.pixelPerVehicle - 1,
y+10, this.paint);
i++;
}
y += 10;
} }
} }
} }

View File

@ -71,8 +71,13 @@ public class Track extends Observable {
} }
update_avg(); update_avg();
this.setChanged(); this.setChanged();
this.notifyObservers(records); this.notifyObservers();
this.clearChanged(); this.clearChanged();
try {
Thread.sleep(125);
} catch (InterruptedException ex) {
}
} }
public float avg_step(int step){ public float avg_step(int step){

View File

@ -8,53 +8,199 @@
tools:context=".MainActivity"> tools:context=".MainActivity">
<LinearLayout <LinearLayout
android:id="@+id/trackViewStack" android:layout_width="match_parent"
android:layout_width="0dp" android:layout_height="match_parent"
android:layout_height="0dp"
android:layout_marginStart="8dp" android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp" android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:orientation="vertical" android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> app:layout_constraintTop_toTopOf="parent">
<Button <View
android:id="@+id/stopButton" android:id="@+id/divider3"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="1dp"
android:layout_marginBottom="16dp" android:background="?android:attr/listDivider" />
android:enabled="false"
android:onClick="onStopButtonClick"
android:text="Stop"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.95"
app:layout_constraintStart_toStartOf="parent" />
<Button <LinearLayout
android:id="@+id/stepButton" android:layout_width="match_parent"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_height="wrap_content" android:orientation="vertical"
android:layout_marginBottom="16dp" android:paddingTop="8dp"
android:onClick="onStepButtonClick" android:paddingBottom="8dp">
android:text="Step"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent" />
<Button <TextView
android:id="@+id/playButton" android:id="@+id/textView4"
android:layout_width="wrap_content" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_marginBottom="16dp" android:text="Track length" />
android:onClick="onPlayButtonClick"
android:text="Play" <SeekBar
app:layout_constraintBottom_toBottomOf="parent" android:id="@+id/seekBar3"
app:layout_constraintEnd_toEndOf="parent" style="@style/Widget.AppCompat.SeekBar.Discrete"
app:layout_constraintHorizontal_bias="0.049" android:layout_width="match_parent"
app:layout_constraintStart_toStartOf="parent" /> android:layout_height="wrap_content"
android:max="10"
android:progress="3" />
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Number of vehicles" />
<SeekBar
android:id="@+id/seekBar2"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="3" />
<TextView
android:id="@+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Maximum velocity" />
<SeekBar
android:id="@+id/seekBar4"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="3" />
<TextView
android:id="@+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Brake probability" />
<SeekBar
android:id="@+id/seekBar5"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="10"
android:progress="3" />
</LinearLayout>
<View
android:id="@+id/divider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Average velocity (overall):" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Average velocity (last step):" />
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
<View
android:id="@+id/divider2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="?android:attr/listDivider" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="8dp"
android:paddingBottom="8dp">
<LinearLayout
android:id="@+id/trackViewStack"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintBottom_toTopOf="@+id/linearLayout"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="8dp">
</LinearLayout>
<LinearLayout
android:id="@+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/playButton">
<Button
android:id="@+id/playButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onPlayButtonClick"
android:text="Play" />
<Button
android:id="@+id/stepButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onStepButtonClick"
android:text="Step" />
<Button
android:id="@+id/stopButton"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:onClick="onStopButtonClick"
android:text="Stop" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout> </androidx.constraintlayout.widget.ConstraintLayout>