Optimize TimeRecordView
This commit is contained in:
parent
82bd01d9c8
commit
9c1899fe42
@ -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);
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -11,18 +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.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) {
|
||||||
@ -43,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, 10);
|
super.onMeasure(widthMeasureSpec, 10*50);
|
||||||
setMeasuredDimension(widthMeasureSpec, 10);
|
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;
|
||||||
|
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;
|
int i = 0;
|
||||||
for (VehicleTimeRecord r : this.records) {
|
for (VehicleTimeRecord r : step) {
|
||||||
int left = (int) (this.pixelPerVehicle * r.getPosition());
|
int left = (int) (this.pixelPerVehicle * r.getPosition());
|
||||||
this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
|
this.paint.setColor(getColor(r.getVelocity(), r.getMaxVelocity()));
|
||||||
canvas.drawRect(left, 0, left + this.pixelPerVehicle - 1,
|
canvas.drawRect(left, y, left + this.pixelPerVehicle - 1,
|
||||||
10, this.paint);
|
y+10, this.paint);
|
||||||
i++;
|
i++;
|
||||||
}
|
}
|
||||||
|
y += 10;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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){
|
||||||
|
Loading…
Reference in New Issue
Block a user