如题,时一个仿滴滴的轨迹动画处理,自己在网上结合资料自己做的,欢迎各位使用
思路:1.根据百度地图提供的的api获取LatLng点所对应的屏幕坐标Point
Projection projection =mMap.getProjection();
Point oldp1 = (projection.toScreenLocation(new LatLng(0.0,0.0)));
2.将Point集合转换为Path路径,
for(int i =0;i<pts.size();i++){
if(i>=1) {
Point oldp1 = (projection.toScreenLocation(pts.get(i-1)));
Point newp1 = (projection.toScreenLocation(pts.get(i)));
Point oldp =newPoint((oldp1.x- w),(oldp1.y- h));
Point newp =newPoint((newp1.x- w),(newp1.y- h));
Path path2 =newPath();
path2.moveTo(oldp.x,oldp.y);
path2.lineTo(newp.x,newp.y);
path.addPath(path2);
path2.close();
}
}
3.使用ObjectAnimator完成属性动画
ObjectAnimator animx= ObjectAnimator.ofFloat(iv_car,"x","y",path);
animx.setDuration(3000);
animx.start();
4.主要思路就是这样,接下来就是上整个方法了,我注释尽量多写点
private voidreBack2() {
animx=newObjectAnimator();
Path path =newPath();//总路径
Projection projection =mMap.getProjection();//百度地图获取屏幕坐标转换的工具
inth =iv_car.getHeight();//计算要移动控件的高度
intw =iv_car.getWidth() /2;//计算要移动控件的宽度并找到中点
floatlen =0;//路径的总长度
PathMeasure pm =newPathMeasure();//路径获取的帮助器
for(inti =0;i
if(i>=1) {
Point oldp1 = (projection.toScreenLocation(pts.get(i-1)));//开始位置坐标
Point newp1 = (projection.toScreenLocation(pts.get(i)));//结束位置
Point oldp =newPoint((oldp1.x- w),(oldp1.y- h));//纠正控件显示坐标
Point newp =newPoint((newp1.x- w),(newp1.y- h));//纠正控件显示坐标
Path path2 =newPath();
path2.moveTo(oldp.x,oldp.y);//设置起点
path2.lineTo(newp.x,newp.y);//设置中点
path.addPath(path2);//将一段路径添加到总路径中
path2.close();//关闭
pm.setPath(path2,true);//关联帮助器
len = pm.getLength()+len;//添加到总路径中
}
}
path.close();//关闭
if(android.os.Build.VERSION.SDK_INT>= android.os.Build.VERSION_CODES.LOLLIPOP) {//ObjectAnimator设置路径的方法只有在api21之后可用
animx= ObjectAnimator.ofFloat(iv_car,"x","y",path);//设置动画路径以及控件,参数2和参数3不用动
String length = MathExtendUtils.qianweifenge(len);//把长度换算为1,000.00的形式
longl =1000;
if(length.indexOf(",")>-1){
String a[] = length.split(",");
l = Long.parseLong((a[0]+"000"));
}
animx.setDuration(l);//根据路径长度动态设置整个动画的所需时间
iv_car.setVisibility(View.VISIBLE);
animx.start();
animx.addListener(newAnimator.AnimatorListener() {//监听
@Override
public voidonAnimationStart(Animator animation) {
iv_car.setVisibility(View.VISIBLE);
}
@Override
public voidonAnimationEnd(Animator animation) {
//要设置地图的手势动作,不然会出现错位
UiSettings settings =mMap.getUiSettings();
canClick=true;
settings.setAllGesturesEnabled(true);//关闭一切手势操作
settings.setZoomGesturesEnabled(true);//获取是否允许缩放手势返回:是否允许缩放手势
iv_car.setVisibility(View.INVISIBLE);
}
@Override
public voidonAnimationCancel(Animator animation) {
}
@Override
public voidonAnimationRepeat(Animator animation) {
}
});
}
}
注意:一定要在移动前禁用百度地图的所有手势动作
UiSettings settings =mMap.getUiSettings();
settings.setAllGesturesEnabled(false);//关闭一切手势操作
settings.setZoomGesturesEnabled(false);//获取是否允许缩放手势返回:是否允许缩放手势