Trajectory pause, continue to question

Since the trajectory is completed in the child thread inside, we naturally think of waiting threads, wake up, that is, wait, notify the problem
so we add the following code in the course of the campaign on it

IF (PAUSE) {
the try {
lock.wait ();
} the catch (InterruptedException E) {
e.printStackTrace ();
}
}
}
1
2
3
4
5
6
7
8
how to let him resume activities? Click to notify

void reStartMove public () {
the synchronized (Lock) {
PAUSE = to false;
lock.notify ();
}
}
The complete code is as follows:


/ **
* the Description: <MoveCarCustomThread> <br>
* Author: mxdl <br>
* a Date: 2019/7/10 <br>
* Version: V1.0.0 <br>
* Update: <br>
* /
public class MoveCarCustomThread the thread {the extends
public static String Final = MoveCarCustomThread.class.getSimpleName the TAG ();
Private Handler moveCarHandler; // send data asynchronous message processor
private Object lock = new Object () ; // thread-locking
private boolean moveing = false; / / whether the thread is moving
private boolean pause = false; // pause state, true to suspend
private boolean stop = false; // stop state is true then stopped moving
private WeakReference <MainActivity> mActivityWeakReference; // prevent memory caused Activity content leak
private MOVE_STATE currMoveState = MOVE_STATE.START_STATUS;

public void setCurrMoveState(MOVE_STATE currMoveState) {
this.currMoveState = currMoveState;
}

MOVE_STATE getCurrMoveState public () {
return currMoveState;
}
public MoveCarCustomThread (the MainActivity Activity) {
mActivityWeakReference new new WeakReference = <> (Activity);
}
// Pause mobile
public void pauseMove () {
PAUSE = to true;
}
after // set pause, again it calls the mobile
public void reStartMove () {
the synchronized (Lock) {
PAUSE = to false;
lock.notify ();
}
}

public void stopMove() {
stop = true;
if(moveCarHandler != null){
moveCarHandler.removeCallbacksAndMessages(null);
}
if(mActivityWeakReference.get() != null){
mActivityWeakReference.get().mLatLngList.clear();
mActivityWeakReference.get().mMainHandler.removeCallbacksAndMessages(null);
}
}

public Handler getMoveCarHandler() {
return moveCarHandler;
}
public boolean isMoveing() {
return moveing;
}


@Override
public void RUN () {
super.run ();
// set the thread to the thread loop
Looper.prepare ();
moveCarHandler = new new Handler () {
@Override
public void the handleMessage (the Message MSG) {
super.handleMessage (MSG );
// sent, by locking to ensure data synchronization enqueue
the synchronized (lock) {
! IF (msg.obj = null && msg.obj the instanceof List) {
List <of LatLng> latLngList = (List <of LatLng>) msg.obj ;
moveCoarseTrack (latLngList);
}
}
}
};
// start loop thread
Looper.loop ();
}

void moveCoarseTrack Private (List <of LatLng> latLngList) {
IF (latLngList latLngList.size == null || () == 0 || latLngList.size () ==. 1) {
return;
}
Log.v (the TAG, "moveCoarseTrack start ................................................. ........ ");
Long the startTime = System.currentTimeMillis ();
Log.v (the TAG," the startTime: "the startTime +);
int = TrackMoveUtil.getStep STEP (latLngList); // the distance calculated track motion animation step number
Log.v (the TAG, "Move sTEP:" + sTEP);
a float TrackMoveUtil.getDistance Distance = (latLngList);
Log.v (the TAG, "Move Distance:" Distance +);
Double mTimeInterval = TrackMoveUtil. getMoveTime (distance, step); // the distance, the trajectory calculating animation interval
mTimeInterval = 10; // every step stop 10 ms
Log.v(TAG, "move mTimeInterval:" + mTimeInterval);

moveing = true;
for (int i = 0; i < latLngList.size() - 1; i++) {
// 暂停状态,线程停止了
if (pause) {
movePause();
}
if (stop) {
break;
}
moveing = true;
LatLng startLatLng = latLngList.get(i);
LatLng endLatLng = latLngList.get(i + 1);
MainActivity mainActivity = mActivityWeakReference.get();
moveCar(startLatLng, endLatLng, mainActivity);
moveLine(startLatLng, mainActivity);
moveCamera(startLatLng, mainActivity);

double slope = TrackMoveUtil.getSlope (startLatLng, endLatLng ); // calculate the slope between two
double intercept = TrackMoveUtil.getInterception (slope, startLatLng ); // The operator takes intercept point and slope
boolean isReverse = (startLatLng.latitude> endLatLng.latitude); // is not a positive mark (set up forward)
double xMoveDistance = isReverse TrackMoveUtil.getXMoveDistance (Slope): -1 * TrackMoveUtil.getXMoveDistance (Slope);?
// should latitude and longitude to handle
double = 0 SLEEP;
int Flag = 0;
for (Double J = startLatLng.latitude; ((J> = endLatLng.latitude) ^ isReverse);! J = J - xMoveDistance) {
// non-suspended state map was carried out following the move
if (PAUSE) {
movePause ();
}
IF (STOP) {
BREAK;
}
moveing = to true;
In Flag ++;
IF (Slope = Double.MAX_VALUE!) {
startLatLng of LatLng new new = (J, (J - Intercept) / Slope);
} the else {
startLatLng of LatLng new new = (J, startLatLng.longitude);
}
moveCar (startLatLng, MainActivity);
moveLine (startLatLng, MainActivity);
IF (100% In Flag == 0) {
moveCamera (startLatLng, MainActivity);
}
// If the interval is less than 1 millisecond, then skip this sleep, the sleep time is accumulated until one millisecond: loses precision
IF (mTimeInterval <. 1) {
SLEEP + = mTimeInterval;
IF (SLEEP> =. 1) {
Log.v (the TAG, "SLEEP:" + SLEEP);
SystemClock.sleep ((Long) SLEEP);
SLEEP = 0;
}
} {the else
SystemClock.sleep ((Long) mTimeInterval);
}

}
}
long endTime = System.currentTimeMillis();
moveing = false;
Log.v(TAG, "endTime:" + endTime);
Log.v(TAG, "run mTimeInterval:" + (endTime - startTime));
Log.v(TAG, "moveCoarseTrack end.........................................................");
}

void moveLine Private (startLatLng of LatLng, the MainActivity MainActivity) {
mainActivity.mLatLngList.add (startLatLng); // set point trajectory increases track
mainActivity.mMovePolyline.setPoints (mainActivity.mLatLngList); // draw a line trace start
}

private void moveCar(LatLng startLatLng, LatLng endLatLng, MainActivity mainActivity) {
moveCar(startLatLng,mainActivity);
if (mainActivity.mCarMarker != null) {
mainActivity.mCarMarker.setRotateAngle((float) TrackMoveUtil.getAngle(startLatLng, endLatLng));// 设置小车车头的方向
}
}
private void moveCar(LatLng startLatLng,MainActivity mainActivity) {
if (mainActivity.mCarMarker != null) {
mainActivity.mCarMarker.setPosition(startLatLng);// 小车移动
}
}
private void movePause() {
try {
lock.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

private void moveCamera(LatLng startLatLng, MainActivity mainActivity) {
Message message = Message.obtain();
message.what = MainActivity.EventType.MapMove;
message.obj = startLatLng;
mainActivity.mMainHandler.sendMessage(message);
}

}

Guess you like

Origin www.cnblogs.com/ly570/p/11369964.html