优化显示逻辑

This commit is contained in:
wangcongtao
2021-03-09 10:05:49 +08:00
parent 76f0095dd7
commit 731ffde5aa
12 changed files with 219 additions and 110 deletions

View File

@@ -1,12 +1,10 @@
package com.mogo.module.common.drawer;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import com.mogo.commons.AbsMogoApplication;
import com.mogo.map.MogoLatLng;
@@ -38,7 +36,7 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
private final Context mContext;
private Handler pointSettingHandler = null;
private Handler mPointSettingHandler = null;
public static final int MSG_POINTS_SETTING = 9;
public static final int MSG_SET_POINT = 10;
public static final int MSG_SET_SPEED = 11;
@@ -50,25 +48,21 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
}
private void initHandler() {
pointSettingHandler = new Handler( WorkThreadHandler.newInstance( "point-setting-thread" ).getLooper() ) {
mPointSettingHandler = new Handler( WorkThreadHandler.newInstance( "moving-points-thread" ).getLooper() ) {
@Override
public void handleMessage( Message msg ) {
super.handleMessage( msg );
if ( msg.what == MSG_POINTS_SETTING ) {
if ( msg.obj instanceof SettingData ) {
startSettingPointLooper( ( ( SettingData ) msg.obj ) );
if ( msg.obj instanceof MovingPoints ) {
startSettingPointLooper( ( ( MovingPoints ) msg.obj ) );
}
} else if ( msg.what == MSG_SET_POINT ) {
if ( msg.obj instanceof PointData ) {
moveMarker( ( ( PointData ) msg.obj ) );
if ( msg.obj instanceof MovingPoint ) {
moveMarker( ( ( MovingPoint ) msg.obj ) );
}
} else if( msg.what == MSG_SET_SPEED ){
if ( msg.obj instanceof SpeedData ) {
try {
( ( SpeedData ) msg.obj ).showSpeed();
} catch ( Exception e ) {
e.printStackTrace();
}
showSpeed( ( SpeedData ) msg.obj );
}
}
}
@@ -80,46 +74,33 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
*
* @param data
*/
private void startSettingPointLooper( SettingData data ) {
List< PointData > points = data.points;
private void startSettingPointLooper( MovingPoints data ) {
List< MovingPoint > points = data.points;
for ( int i = 0; i < points.size(); i++ ) {
Message msg = Message.obtain();
msg.what = MSG_SET_POINT;
msg.obj = points.get( i );
pointSettingHandler.sendMessageDelayed( msg, ( i + 1 ) * 10 );
mPointSettingHandler.sendMessageDelayed( msg, ( i + 1 ) * 10 );
}
}
private String markerRes;
/**
* 设置 marker 的点到新的位置
*
* @param pointData
*/
private void moveMarker( PointData pointData ) {
private void moveMarker( MovingPoint pointData ) {
if ( pointData == null ) {
return;
}
try {
pointData.move();
// MogoMarkerOptions options = new MogoMarkerOptions();
// options.gps( true )
// .position( pointData.point)
// .anchor( 0.5f, 0.5f )
// .rotate( pointData.angle);
// if ( TextUtils.isEmpty( markerRes ) ) {
// options.icon( BitmapFactory.decodeResource( mContext.getResources(), R.drawable.blue ) );
// }else {
// options.resName( markerRes );
// }
// IMogoMarker marker = MogoApisHandler.getInstance().getApis().getMapServiceApi().getMarkerManager( mContext ).addMarker( TAG, options );
// if ( TextUtils.isEmpty( markerRes ) ) {
// markerRes = marker.getMarkerResName();
// }
} catch ( Exception e ) {
e.printStackTrace();
pointData.move();
}
private void showSpeed(SpeedData data){
if ( data == null ) {
return;
}
data.showSpeed();
}
private final Map< String, ADASRecognizedResult > mLastPositions = new ConcurrentHashMap<>();
@@ -173,8 +154,13 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
mAdasRecognizedMarkersCaches = newAdasRecognizedMarkersCaches;
}
private final Map< String, Boolean > isMatchStatusCache = new ArrayMap<>();
private final Map< String, Boolean > mIsMatchStatusCache = new ArrayMap<>();
/**
* 绘制某个物体的一个数据
* @param recognizedListResult
* @param newAdasRecognizedMarkersCaches
*/
private void renderAdasOneFrame( ADASRecognizedResult recognizedListResult, Map< String, IMogoMarker > newAdasRecognizedMarkersCaches ) {
// 暂时只显示车辆
if ( !isCarType( recognizedListResult.type ) ) {
@@ -191,7 +177,7 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
recognizedListResult.heading,
true
);
Boolean isMatch = isMatchStatusCache.get( uniqueKey );
Boolean isMatch = mIsMatchStatusCache.get( uniqueKey );
if ( matchedPoint != null ) {
if ( ( isMatch == null || !isMatch ) ) {
if ( matchedPoint[2] < 0.5 ) {
@@ -205,7 +191,7 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
if ( isMatch == null ) {
isMatch = false;
}
isMatchStatusCache.put( uniqueKey, isMatch );
mIsMatchStatusCache.put( uniqueKey, isMatch );
if ( isMatch ) {
recognizedListResult.lon = matchedPoint[0];
@@ -229,22 +215,22 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
MogoLatLng lastPoint = new MogoLatLng( lastPosition.lat, lastPosition.lon );
lastPoint.setTime( lastPosition.satelliteTime );
endLatLon.setTime( recognizedListResult.satelliteTime );
PointData endPoint = new PointData();
MovingPoint endPoint = new MovingPoint();
endPoint.point = endLatLon;
endPoint.marker = marker;
endPoint.angle = ( float ) recognizedListResult.heading;
PointData startPoint = new PointData();
MovingPoint startPoint = new MovingPoint();
startPoint.point = lastPoint;
startPoint.marker = marker;
startPoint.angle = ( float ) lastPosition.heading;
List< PointData > points = interpolate( startPoint, endPoint, 30, interval );
List< MovingPoint > points = interpolate( startPoint, endPoint, 30, interval );
Message msg = new Message();
SettingData obj = new SettingData();
MovingPoints obj = new MovingPoints();
obj.points = points;
msg.obj = obj;
msg.what = MSG_POINTS_SETTING;
pointSettingHandler.sendMessage( msg );
mPointSettingHandler.sendMessage( msg );
// marker.startSmoothInMs( points, interval );
// marker.addDynamicAnchorPosition( endLatLon, interval );
Logger.d( TAG, "anim duration: %s, points size = %s", interval, points.size() );
@@ -259,7 +245,7 @@ class AdasRecognizedResultDrawer extends BaseDrawer {
obj.speed = recognizedListResult.speed;
msg.obj = obj;
msg.what = MSG_SET_SPEED;
pointSettingHandler.sendMessage( msg );
mPointSettingHandler.sendMessage( msg );
}
/**

View File

@@ -6,10 +6,10 @@ import android.graphics.Typeface;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.TextView;
import com.mogo.cloud.commons.utils.CoordinateUtils;
import com.mogo.commons.AbsMogoApplication;
import com.mogo.map.MogoLatLng;
import com.mogo.map.marker.IMogoMarker;
@@ -127,23 +127,29 @@ class BaseDrawer {
mogoMarker.hideInfoWindow();
return;
}
if ( speed > 0 ) {
int speedIntVal = ( int ) ( speed * 3.6 );
if ( speedIntVal <= 0 ) {
mogoMarker.hideInfoWindow();
return;
}
String speedVal = speedIntVal + "";
String infoResName = MogoApisHandler.getInstance().getApis().getMapServiceApi().getMapUIController().getMarkerInfoResName( speedVal );
if ( TextUtils.isEmpty( infoResName ) ) {
if ( mSpeedView == null ) {
mSpeedView = new TextView( context );
mSpeedView.setTextColor( Color.WHITE );
mSpeedView.setTypeface( Typeface.defaultFromStyle( Typeface.BOLD ) );
// mSpeedView.setShadowLayer( 10f, 5F, 5F, Color.BLACK );
mSpeedView.setLayoutParams( new ViewGroup.LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT ) );
}
mSpeedView.setText( String.valueOf( ( ( int ) ( speed * 3.6 ) ) ) );
mSpeedView.setText( speedVal );
mogoMarker.setInfoWindowOffset( 0, 20 );
mogoMarker.updateInfoWindowView( mSpeedView );
MogoApisHandler.getInstance().getApis().getMapServiceApi().getMapUIController().setMarkerInfoResName( speedVal, mogoMarker.getMarkerInfoResName() );
} else {
mogoMarker.hideInfoWindow();
mogoMarker.updateInfoWindowView( infoResName );
}
}
/**
* 移除markers
*
@@ -194,11 +200,14 @@ class BaseDrawer {
* @return
*/
public double[] matchRoad( double lon, double lat, double angle, boolean isRtk ) {
return MogoApisHandler.getInstance()
final long start = System.currentTimeMillis();
double[] matchRoad = MogoApisHandler.getInstance()
.getApis()
.getMapServiceApi()
.getMapUIController()
.matchRoad( lon, lat, angle, true, isRtk );
Log.i("timer-matchRoad", "cost " + (System.currentTimeMillis() - start) + "ms");
return matchRoad;
}
/**
@@ -228,12 +237,12 @@ class BaseDrawer {
* @param frameInterval
* @return
*/
public static List< PointData > interpolate( PointData start, PointData end, long frameInterval, long duration ) {
public static List< MovingPoint > interpolate( MovingPoint start, MovingPoint end, long frameInterval, long duration ) {
if ( start == null || end == null ) {
return null;
}
int interpolateFrame = ( int ) ( duration / frameInterval ) - 1;
List< PointData > arrayList = new ArrayList<>();
List< MovingPoint > arrayList = new ArrayList<>();
double _angle = ( end.angle + start.angle ) / 2;
if ( Math.abs( end.angle - start.angle ) > 5 ) {
_angle = Math.atan2( Math.abs( start.point.lon - end.point.lon ), Math.abs( start.point.lat - end.point.lat ) ) * ( 180 / PI );
@@ -245,7 +254,7 @@ class BaseDrawer {
for ( int i = 0; i < interpolateFrame; i++ ) {
double lon = start.point.lon + lonStep * ( i + 1 );
double lat = start.point.lat + latStep * ( i + 1 );
PointData pd = new PointData();
MovingPoint pd = new MovingPoint();
pd.point = new MogoLatLng( lat, lon );
pd.angle = ( float ) _angle;
pd.marker = start.marker;
@@ -259,24 +268,32 @@ class BaseDrawer {
/**
* marker 单个点移动的对象
*/
public static class PointData {
public static class MovingPoint {
public MogoLatLng point;
public float angle;
public IMogoMarker marker;
public void move() {
marker.setRotateAngle( angle );
marker.setPosition( point.lat, point.lon );
try {
marker.setRotateAngle( angle );
marker.setPosition( point.lat, point.lon );
} catch ( Exception e ) {
e.printStackTrace();
}
}
}
/**
* marker 平滑移动的对象
*/
public static class SettingData {
public List< PointData > points;
public static class MovingPoints {
public List< MovingPoint > points;
}
/**
* 速度显示对象
*/
public class SpeedData {
public IMogoMarker marker;

View File

@@ -4,7 +4,6 @@ import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.ArrayMap;
@@ -44,12 +43,12 @@ public class SimpleHandlerThreadPool {
public void handleMessage( Message msg ) {
super.handleMessage( msg );
if ( msg.what == MSG_POINTS_SETTING ) {
if ( msg.obj instanceof BaseDrawer.SettingData) {
startSettingPointLooper( ( (BaseDrawer.SettingData) msg.obj ) );
if ( msg.obj instanceof BaseDrawer.MovingPoints ) {
startSettingPointLooper( ( ( BaseDrawer.MovingPoints ) msg.obj ) );
}
} else if ( msg.what == MSG_SET_POINT ) {
if ( msg.obj instanceof BaseDrawer.PointData) {
moveMarker( ( (BaseDrawer.PointData) msg.obj ) );
if ( msg.obj instanceof BaseDrawer.MovingPoint ) {
moveMarker( ( ( BaseDrawer.MovingPoint ) msg.obj ) );
}
}
}
@@ -61,8 +60,8 @@ public class SimpleHandlerThreadPool {
*
* @param data
*/
private void startSettingPointLooper( BaseDrawer.SettingData data ) {
List<BaseDrawer.PointData> points = data.points;
private void startSettingPointLooper( BaseDrawer.MovingPoints data ) {
List< BaseDrawer.MovingPoint > points = data.points;
for ( int i = 0; i < points.size(); i++ ) {
Message msg = Message.obtain();
msg.what = MSG_SET_POINT;
@@ -76,7 +75,7 @@ public class SimpleHandlerThreadPool {
*
* @param pointData
*/
private void moveMarker( BaseDrawer.PointData pointData ) {
private void moveMarker( BaseDrawer.MovingPoint pointData ) {
if ( pointData == null ) {
return;
}
@@ -227,18 +226,18 @@ public class SimpleHandlerThreadPool {
MogoLatLng lastPoint = new MogoLatLng( lastPosition.getWgslat(), lastPosition.getWgslon() );
lastPoint.setTime( lastPosition.getSatelliteTime() );
endLatLon.setTime( cloudRoadData.getSatelliteTime() );
BaseDrawer.PointData endPoint = new BaseDrawer.PointData();
BaseDrawer.MovingPoint endPoint = new BaseDrawer.MovingPoint();
endPoint.point = endLatLon;
endPoint.marker = marker;
endPoint.angle = ( float ) cloudRoadData.getHeading();
BaseDrawer.PointData startPoint = new BaseDrawer.PointData();
BaseDrawer.MovingPoint startPoint = new BaseDrawer.MovingPoint();
startPoint.point = lastPoint;
startPoint.marker = marker;
startPoint.angle = ( float ) lastPosition.getHeading();
List<BaseDrawer.PointData> points = BaseDrawer.interpolate( startPoint, endPoint, 30, interval );
List< BaseDrawer.MovingPoint > points = BaseDrawer.interpolate( startPoint, endPoint, 30, interval );
Message msg = new Message();
BaseDrawer.SettingData obj = new BaseDrawer.SettingData();
BaseDrawer.MovingPoints obj = new BaseDrawer.MovingPoints();
obj.points = points;
msg.obj = obj;
msg.what = MSG_POINTS_SETTING;