Optimize TimeRecordView

This commit is contained in:
Loch Christian (uib05376) 2020-11-09 21:32:06 +01:00
parent 82bd01d9c8
commit 9c1899fe42
3 changed files with 39 additions and 24 deletions

View File

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

View File

@ -11,18 +11,17 @@ import java.util.List;
public class TimeRecordView extends View {
protected Paint paint;
protected List<VehicleTimeRecord> records;
protected Track track;
protected int pixelPerVehicle;
protected float trackLength;
public TimeRecordView(Context context, List<VehicleTimeRecord> records, float trackLength) {
public TimeRecordView(Context context, Track track) {
super(context);
this.records = records;
this.trackLength = trackLength;
this.track = track;
this.paint = new Paint();
paint.setColor(Color.BLACK);
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) {
@ -43,25 +42,32 @@ public class TimeRecordView extends View {
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, 10);
setMeasuredDimension(widthMeasureSpec, 10);
super.onMeasure(widthMeasureSpec, 10*50);
setMeasuredDimension(widthMeasureSpec, 10*50);
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
this.pixelPerVehicle = (int) (this.getWidth() / this.track.getTrackLength());
}
@Override
protected void onDraw(Canvas canvas) {
this.pixelPerVehicle = (int) (this.getWidth() / this.trackLength);
int i = 0;
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,
10, this.paint);
i++;
int y = 0;
List<List<VehicleTimeRecord>> stepList = this.track.getVtrList();
for (int curStepIdx = stepList.size()-1; curStepIdx >= stepList.size()-1-50 && curStepIdx >= 0; curStepIdx--) {
List<VehicleTimeRecord> step = stepList.get(curStepIdx);
int i = 0;
for (VehicleTimeRecord r : step) {
int left = (int) (this.pixelPerVehicle * r.getPosition());
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();
this.setChanged();
this.notifyObservers(records);
this.notifyObservers();
this.clearChanged();
try {
Thread.sleep(125);
} catch (InterruptedException ex) {
}
}
public float avg_step(int step){