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.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
import android.widget.LinearLayout;
|
||||||
import android.widget.TextView;
|
import android.widget.TextView;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
import java.util.Observable;
|
import java.util.Observable;
|
||||||
import java.util.Observer;
|
import java.util.Observer;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity implements Observer {
|
public class MainActivity extends AppCompatActivity implements Observer {
|
||||||
|
|
||||||
protected Track track;
|
protected Track track;
|
||||||
protected TrackView trackView;
|
protected TimeRecordView trackView;
|
||||||
protected Worker worker;
|
protected Worker worker;
|
||||||
|
protected LinearLayout viewStack;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
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(20, 100);
|
this.track = new Track(40, 200);
|
||||||
this.trackView = new TrackView(this, this.track);
|
|
||||||
this.track.addObserver(this);
|
this.track.addObserver(this);
|
||||||
ConstraintLayout layout = (ConstraintLayout) findViewById(R.id.constr_layout);
|
this.viewStack = (LinearLayout) findViewById(R.id.trackViewStack);
|
||||||
layout.addView(this.trackView);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void update(Observable observable, Object o) {
|
public void update(Observable observable, Object o) {
|
||||||
final Track trackRef = this.track;
|
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() {
|
runOnUiThread(new Runnable() {
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
String s = "";
|
TimeRecordView newTrView = new TimeRecordView(mainActivity, newRecords, trackRef.getTrackLength());
|
||||||
for (Vehicle v: trackRef.getVehicles()){
|
viewStackRef.addView(newTrView, 0);
|
||||||
s+= "Vehicle " + v.id + " | "
|
int childCount = viewStackRef.getChildCount();
|
||||||
+ "Pos = " + v.getPosition() + " | "
|
if (childCount > 30) {
|
||||||
+ "Vel = " + v.getCurVelocity()+ " | "
|
viewStackRef.removeViewAt(childCount-1);
|
||||||
+ "\n";
|
|
||||||
;
|
|
||||||
}
|
}
|
||||||
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.View;
|
||||||
import android.view.ViewGroup;
|
import android.view.ViewGroup;
|
||||||
|
|
||||||
public class TrackView extends View {
|
import java.util.List;
|
||||||
protected Paint paint;
|
|
||||||
protected Track track;
|
|
||||||
protected int pixelPerVehicle;
|
|
||||||
|
|
||||||
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);
|
super(context);
|
||||||
this.track = track;
|
this.records = records;
|
||||||
|
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);
|
||||||
@ -22,6 +26,7 @@ public class TrackView extends View {
|
|||||||
|
|
||||||
protected int getColor(float curVelocity, float maxVelocity) {
|
protected int getColor(float curVelocity, float maxVelocity) {
|
||||||
float perc = curVelocity / maxVelocity;
|
float perc = curVelocity / maxVelocity;
|
||||||
|
perc = 1 - perc;
|
||||||
if (perc <= 0.5) {
|
if (perc <= 0.5) {
|
||||||
int red = ((int) (2*perc*0xFF)) << 16;
|
int red = ((int) (2*perc*0xFF)) << 16;
|
||||||
int green = ((int)0xFF) << 8;
|
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
|
@Override
|
||||||
protected void onDraw(Canvas canvas) {
|
protected void onDraw(Canvas canvas) {
|
||||||
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
|
this.pixelPerVehicle = (int) (this.getWidth() / this.trackLength);
|
||||||
//super.onDraw(canvas);
|
|
||||||
int i = 0;
|
int i = 0;
|
||||||
for (Vehicle v : this.track.getVehicles()) {
|
for (VehicleTimeRecord r : this.records) {
|
||||||
int left = (int) (this.pixelPerVehicle * v.getPosition());
|
int left = (int) (this.pixelPerVehicle * r.getPosition());
|
||||||
this.paint.setColor(getColor(v.getCurVelocity(), v.getMaxVelocity()));
|
this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
|
||||||
canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
|
canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
|
||||||
20, this.paint);
|
20, this.paint);
|
||||||
i++;
|
i++;
|
@ -7,6 +7,11 @@ import java.util.Observable;
|
|||||||
|
|
||||||
public class Track extends Observable {
|
public class Track extends Observable {
|
||||||
protected List<Vehicle> vehicles;
|
protected List<Vehicle> vehicles;
|
||||||
|
|
||||||
|
public List<List<VehicleTimeRecord>> getVtrList() {
|
||||||
|
return vtrList;
|
||||||
|
}
|
||||||
|
|
||||||
protected List<List<VehicleTimeRecord>> vtrList;
|
protected List<List<VehicleTimeRecord>> vtrList;
|
||||||
protected float trackLength;
|
protected float trackLength;
|
||||||
protected float sumAvgMemory;
|
protected float sumAvgMemory;
|
||||||
@ -36,7 +41,7 @@ public class Track extends Observable {
|
|||||||
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
protected List<Vehicle> createVehiclesList(int numberVehicles){
|
||||||
List<Vehicle> result = new ArrayList<>();
|
List<Vehicle> result = new ArrayList<>();
|
||||||
for(int i=0;i<numberVehicles;i++){
|
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);
|
result.add(vehicle);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -61,12 +66,12 @@ public class Track extends Observable {
|
|||||||
this.vtrList.add(records);
|
this.vtrList.add(records);
|
||||||
for(Vehicle v: vehicles){
|
for(Vehicle v: vehicles){
|
||||||
v.timeElapse();
|
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);
|
records.add(vtr);
|
||||||
}
|
}
|
||||||
update_avg();
|
update_avg();
|
||||||
this.setChanged();
|
this.setChanged();
|
||||||
this.notifyObservers(this);
|
this.notifyObservers(records);
|
||||||
this.clearChanged();
|
this.clearChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,6 +1,15 @@
|
|||||||
package de.hems.trafficsim;
|
package de.hems.trafficsim;
|
||||||
|
|
||||||
public class VehicleTimeRecord {
|
public class VehicleTimeRecord {
|
||||||
|
protected int id;
|
||||||
|
protected float position;
|
||||||
|
protected float velocity;
|
||||||
|
protected float maxVelocity;
|
||||||
|
|
||||||
|
public float getMaxVelocity() {
|
||||||
|
return maxVelocity;
|
||||||
|
}
|
||||||
|
|
||||||
public float getPosition() {
|
public float getPosition() {
|
||||||
return position;
|
return position;
|
||||||
}
|
}
|
||||||
@ -9,13 +18,10 @@ public class VehicleTimeRecord {
|
|||||||
return velocity;
|
return velocity;
|
||||||
}
|
}
|
||||||
|
|
||||||
protected int id;
|
public VehicleTimeRecord(int id, float position, float velocity, float maxVelocity) {
|
||||||
protected float position;
|
|
||||||
protected float velocity;
|
|
||||||
|
|
||||||
public VehicleTimeRecord(int id, float position, float velocity) {
|
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.position = position; //needed???
|
this.position = position;
|
||||||
this.velocity = velocity;
|
this.velocity = velocity;
|
||||||
|
this.maxVelocity = maxVelocity;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,19 @@
|
|||||||
android:layout_height="match_parent"
|
android:layout_height="match_parent"
|
||||||
tools:context=".MainActivity">
|
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
|
<Button
|
||||||
android:id="@+id/stopButton"
|
android:id="@+id/stopButton"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
@ -20,16 +33,6 @@
|
|||||||
app:layout_constraintHorizontal_bias="0.95"
|
app:layout_constraintHorizontal_bias="0.95"
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
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
|
<Button
|
||||||
android:id="@+id/stepButton"
|
android:id="@+id/stepButton"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
@ -53,4 +56,5 @@
|
|||||||
app:layout_constraintEnd_toEndOf="parent"
|
app:layout_constraintEnd_toEndOf="parent"
|
||||||
app:layout_constraintHorizontal_bias="0.049"
|
app:layout_constraintHorizontal_bias="0.049"
|
||||||
app:layout_constraintStart_toStartOf="parent" />
|
app:layout_constraintStart_toStartOf="parent" />
|
||||||
|
|
||||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
</androidx.constraintlayout.widget.ConstraintLayout>
|
Loading…
Reference in New Issue
Block a user