Implement view history
This commit is contained in:
parent
e9c6f25a66
commit
09d4c45ff0
@ -6,48 +6,46 @@ import androidx.constraintlayout.widget.ConstraintLayout;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.LinearLayout;
|
||||
import android.widget.TextView;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Observable;
|
||||
import java.util.Observer;
|
||||
|
||||
public class MainActivity extends AppCompatActivity implements Observer {
|
||||
|
||||
protected Track track;
|
||||
protected TrackView trackView;
|
||||
protected TimeRecordView trackView;
|
||||
protected Worker worker;
|
||||
protected LinearLayout viewStack;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
this.track = new Track(20, 100);
|
||||
this.trackView = new TrackView(this, this.track);
|
||||
this.track = new Track(40, 200);
|
||||
this.track.addObserver(this);
|
||||
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
|
||||
layout.addView(this.trackView);
|
||||
this.viewStack = (LinearLayout) findViewById(R.id.trackViewStack);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void update(Observable observable, Object o) {
|
||||
final Track trackRef = this.track;
|
||||
final TrackView viewRef = this.trackView;
|
||||
//final TimeRecordView viewRef = this.trackView;
|
||||
final LinearLayout viewStackRef = this.viewStack;
|
||||
final List<VehicleTimeRecord> newRecords = (List<VehicleTimeRecord>) o;
|
||||
final MainActivity mainActivity = this;
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
String s = "";
|
||||
for (Vehicle v: trackRef.getVehicles()){
|
||||
s+= "Vehicle " + v.id + " | "
|
||||
+ "Pos = " + v.getPosition() + " | "
|
||||
+ "Vel = " + v.getCurVelocity()+ " | "
|
||||
+ "\n";
|
||||
;
|
||||
TimeRecordView newTrView = new TimeRecordView(mainActivity, newRecords, trackRef.getTrackLength());
|
||||
viewStackRef.addView(newTrView, 0);
|
||||
int childCount = viewStackRef.getChildCount();
|
||||
if (childCount > 30) {
|
||||
viewStackRef.removeViewAt(childCount-1);
|
||||
}
|
||||
s += "Average: " + trackRef.getLastAvg();
|
||||
TextView view = (TextView) findViewById(R.id.debugTextView);
|
||||
view.setText(s);
|
||||
view.invalidate();
|
||||
viewRef.invalidate();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -7,14 +7,18 @@ import android.graphics.Paint;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
|
||||
public class TrackView extends View {
|
||||
protected Paint paint;
|
||||
protected Track track;
|
||||
protected int pixelPerVehicle;
|
||||
import java.util.List;
|
||||
|
||||
public TrackView(Context context, Track track) {
|
||||
public class TimeRecordView extends View {
|
||||
protected Paint paint;
|
||||
protected List<VehicleTimeRecord> records;
|
||||
protected int pixelPerVehicle;
|
||||
protected float trackLength;
|
||||
|
||||
public TimeRecordView(Context context, List<VehicleTimeRecord> records, float trackLength) {
|
||||
super(context);
|
||||
this.track = track;
|
||||
this.records = records;
|
||||
this.trackLength = trackLength;
|
||||
this.paint = new Paint();
|
||||
paint.setColor(Color.BLACK);
|
||||
paint.setStyle(Paint.Style.FILL_AND_STROKE);
|
||||
@ -22,6 +26,7 @@ public class TrackView extends View {
|
||||
|
||||
protected int getColor(float curVelocity, float maxVelocity) {
|
||||
float perc = curVelocity / maxVelocity;
|
||||
perc = 1 - perc;
|
||||
if (perc <= 0.5) {
|
||||
int red = ((int) (2*perc*0xFF)) << 16;
|
||||
int green = ((int)0xFF) << 8;
|
||||
@ -35,14 +40,24 @@ public class TrackView extends View {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
|
||||
super.onMeasure(widthMeasureSpec, 20);
|
||||
setMeasuredDimension(widthMeasureSpec, 20);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
|
||||
super.onSizeChanged(w, h, oldw, oldh);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onDraw(Canvas canvas) {
|
||||
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
|
||||
//super.onDraw(canvas);
|
||||
this.pixelPerVehicle = (int) (this.getWidth() / this.trackLength);
|
||||
int i = 0;
|
||||
for (Vehicle v : this.track.getVehicles()) {
|
||||
int left = (int) (this.pixelPerVehicle * v.getPosition());
|
||||
this.paint.setColor(getColor(v.getCurVelocity(), v.getMaxVelocity()));
|
||||
for (VehicleTimeRecord r : this.records) {
|
||||
int left = (int) (this.pixelPerVehicle * r.getPosition());
|
||||
this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
|
||||
canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
|
||||
20, this.paint);
|
||||
i++;
|
@ -7,6 +7,11 @@ import java.util.Observable;
|
||||
|
||||
public class Track extends Observable {
|
||||
protected List<Vehicle> vehicles;
|
||||
|
||||
public List<List<VehicleTimeRecord>> getVtrList() {
|
||||
return vtrList;
|
||||
}
|
||||
|
||||
protected List<List<VehicleTimeRecord>> vtrList;
|
||||
protected float trackLength;
|
||||
protected float sumAvgMemory;
|
||||
@ -36,7 +41,7 @@ public class Track extends Observable {
|
||||
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
||||
List<Vehicle> result = new ArrayList<>();
|
||||
for(int i=0;i<numberVehicles;i++){
|
||||
Vehicle vehicle = new Vehicle(i, i, 5, 0.2f, this.trackLength);
|
||||
Vehicle vehicle = new Vehicle(i, i, 6, 0.3f, this.trackLength);
|
||||
result.add(vehicle);
|
||||
}
|
||||
|
||||
@ -61,12 +66,12 @@ public class Track extends Observable {
|
||||
this.vtrList.add(records);
|
||||
for(Vehicle v: vehicles){
|
||||
v.timeElapse();
|
||||
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity);
|
||||
VehicleTimeRecord vtr = new VehicleTimeRecord(v.id, v.position, v.curVelocity, v.maxVelocity);
|
||||
records.add(vtr);
|
||||
}
|
||||
update_avg();
|
||||
this.setChanged();
|
||||
this.notifyObservers(this);
|
||||
this.notifyObservers(records);
|
||||
this.clearChanged();
|
||||
}
|
||||
|
||||
|
@ -1,6 +1,15 @@
|
||||
package de.hems.trafficsim;
|
||||
|
||||
public class VehicleTimeRecord {
|
||||
protected int id;
|
||||
protected float position;
|
||||
protected float velocity;
|
||||
protected float maxVelocity;
|
||||
|
||||
public float getMaxVelocity() {
|
||||
return maxVelocity;
|
||||
}
|
||||
|
||||
public float getPosition() {
|
||||
return position;
|
||||
}
|
||||
@ -9,13 +18,10 @@ public class VehicleTimeRecord {
|
||||
return velocity;
|
||||
}
|
||||
|
||||
protected int id;
|
||||
protected float position;
|
||||
protected float velocity;
|
||||
|
||||
public VehicleTimeRecord(int id, float position, float velocity) {
|
||||
public VehicleTimeRecord(int id, float position, float velocity, float maxVelocity) {
|
||||
this.id = id;
|
||||
this.position = position; //needed???
|
||||
this.position = position;
|
||||
this.velocity = velocity;
|
||||
this.maxVelocity = maxVelocity;
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,19 @@
|
||||
android:layout_height="match_parent"
|
||||
tools:context=".MainActivity">
|
||||
|
||||
<LinearLayout
|
||||
android:id="@+id/trackViewStack"
|
||||
android:layout_width="0dp"
|
||||
android:layout_height="0dp"
|
||||
android:layout_marginStart="8dp"
|
||||
android:layout_marginTop="8dp"
|
||||
android:layout_marginEnd="8dp"
|
||||
android:orientation="vertical"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/stopButton"
|
||||
android:layout_width="wrap_content"
|
||||
@ -20,16 +33,6 @@
|
||||
app:layout_constraintHorizontal_bias="0.95"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/debugTextView"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="16dp"
|
||||
android:layout_marginTop="16dp"
|
||||
android:text="TextView"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/stepButton"
|
||||
android:layout_width="wrap_content"
|
||||
@ -53,4 +56,5 @@
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintHorizontal_bias="0.049"
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user