欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义View实现公交成轨迹图

程序员文章站 2023-11-30 10:48:52
本文实例为大家分享了android自定义view实现公交成轨迹图的具体代码,供大家参考,具体内容如下 总体分析下:水平方向recyclewview,item包含定位点...

本文实例为大家分享了android自定义view实现公交成轨迹图的具体代码,供大家参考,具体内容如下

Android自定义View实现公交成轨迹图

总体分析下:水平方向recyclewview,item包含定位点,站台位置和站台名称。

下面看实现:

1.继承framelayout,实现构造方法:

public class busstopplateview extends framelayout {
...
 public busstopplateview(@nonnull context context) {
 super(context);
 initview(context);
 }

 public busstopplateview(@nonnull context context, @nullable attributeset attrs) {
 super(context, attrs);
 initview(context);
 }

 public busstopplateview(@nonnull context context, @nullable attributeset attrs, @attrres int defstyleattr) {
 super(context, attrs, defstyleattr);
 initview(context);
 }
 private void initview(context context) {
 ...
 //设置recycleview
 layoutinflater.from(context).inflate(r.layout.xxx, this, true);
 mrecyclerview = (recyclerview) findviewbyid(r.id.recycle);
 mrecyclerview.setlayoutmanager(new linearlayoutmanager(context, linearlayoutmanager.horizontal, false));
 mbusstopplateadapter = new busstopplateadapter(mstationlist);
 mrecyclerview.setadapter(mbusstopplateadapter);
 
 ...
}

...
}

2.recycleview适配器:初始化的时候设置起点设置终点设置车道设置当前车位置的下标

 /**
 * 设置车道
 */
 private void setdriveway(baseviewholder helper, busstopplatestationinfo item) {
 if (helper.getadapterposition() <= admincurrentindex) {
  helper.getview(r.id.v_daolu).setselected(true);
  helper.getview(r.id.iv_jiantou).setselected(true);
 } else {
  helper.getview(r.id.v_daolu).setselected(false);
  helper.getview(r.id.iv_jiantou).setselected(false);
 }
 }

 /**
 * 设置起点
 */
 private void setstartstation(baseviewholder helper, busstopplatestationinfo item) {
 helper.setvisible(r.id.v_daolu, false)
  .setbackgroundres(r.id.iv_jiantou, r.drawable.bg_busstop_vdaolu_start);
 }

 /**
 * 设置终点
 */
 private void setendstation(baseviewholder helper, busstopplatestationinfo item) {
 helper.setbackgroundres(r.id.iv_jiantou, r.drawable.bg_busstop_vdaolu_end)
  .setbackgroundres(r.id.v_daolu, r.drawable.bg_busstop_vdaolu_end)
  .setvisible(r.id.v_zhanwei, true)
  .setvisible(r.id.v_daoli_zhanwei, false);
 }

 /**
 * 设置当前所在站点
 */
 private void setcurrentstation(baseviewholder helper, busstopplatestationinfo item) {
 mcurrentview = helper.getconvertview();
 helper.setvisible(r.id.bus_stop_reach, true)
  .setvisible(r.id.iv_bus_stop_current, false)
  .setvisible(r.id.tv_bus_stop_current_num, false)
  .setvisible(r.id.iv_current_point, true)
  .setvisible(r.id.iv_admin_index, true)
  // 显示占位符,用于显示一半的灰色
  .setbackgroundres(r.id.v_daoli_zhanwei, r.drawable.bg_busstop_vdaolu)
  .setvisible(r.id.v_daoli_zhanwei, true);
//  .settextcolor(r.id.tv_bus_station_name, color.parsecolor("#3d93fd"));

 glide.with(mcontext)
  .load(r.drawable.bus_icon_fangxiang_current)
  .crossfade()
  .into((imageview) helper.getview(r.id.iv_current_point));

 list<alivebusinfo> alivebusinfos = item.getalivebusinfos();
 if (alivebusinfos != null && alivebusinfos.size() != 0) {
  alivebusinfo alivebusinfo = alivebusinfos.get(0);
  if ("1".equals(alivebusinfo.getststatus()) && alivebusinfo.getstname().equals(item.getstname())) {
  helper.setvisible(r.id.iv_admin_index, false)
   .setvisible(r.id.iv_bus_stop_current, true)
   .setimageresource(r.id.iv_bus_stop_current, r.drawable.bus_stop_current);
  }
 } else {
  glide.with(mcontext)
   .load(r.drawable.icon_admin_current_station)
   .crossfade()
   .into((imageview) helper.getview(r.id.iv_admin_index));
 }

 }

 /**
 * 设置公交所在站点
 */
 private void setbusstation(baseviewholder helper, busstopplatestationinfo item) {
 list<alivebusinfo> alivebusinfos = item.getalivebusinfos();
 if (alivebusinfos != null && alivebusinfos.size() != 0) {
  alivebusinfo alivebusinfo = alivebusinfos.get(0);
  if ("0".equals(alivebusinfo.getststatus())) {
  // 在车道上
  helper.setvisible(r.id.bus_stop_not_to, true)
   .setvisible(r.id.bus_stop_reach, false)
   .settext(r.id.tv_stop_not_to_num, string.valueof(alivebusinfos.size()))
   // 显示在过道中的车
   .setvisible(r.id.iv_stop_not_to, alivebusinfos.size() != 0)
   // 是否显示数字
   .setvisible(r.id.tv_stop_not_to_num, alivebusinfos.size() > 1);
  // 如果已经过站 显示灰色图标
  if (alivebusinfo.getstcount() < 0) {
   glideutils.loadimageview(mcontext, r.drawable.bus_stop_over_station_min, helper.getview(r.id.iv_stop_not_to));
  } else {
   glideutils.loadimageview(mcontext, r.drawable.bus_stop_not_to, helper.getview(r.id.iv_stop_not_to));
  }

  } else if ("1".equals(alivebusinfo.getststatus())) {
  // 到站
  helper.setvisible(r.id.bus_stop_not_to, false)
   .setvisible(r.id.bus_stop_reach, true)
   .setvisible(r.id.iv_admin_index, true)
   .setvisible(r.id.iv_bus_stop_current, false)
   .setvisible(r.id.tv_bus_stop_current_num, alivebusinfo.getstcount() > 1)
   .settext(r.id.tv_bus_stop_current_num, string.valueof(alivebusinfos.size()));
  // 如果已经过站 显示灰色图标
  if (alivebusinfo.getstcount() < 0) {
   glideutils.loadimageview(mcontext, r.drawable.bus_stop_over_station, helper.getview(r.id.iv_admin_index));
  } else {
   glideutils.loadimageview(mcontext, r.drawable.bus_stop_not_to, helper.getview(r.id.iv_admin_index));
  }
  }
 } else {
  // 隐藏公交车
  helper.setvisible(r.id.bus_stop_not_to, false)
   .setvisible(r.id.bus_stop_reach, false);
 }
 }

3.外部activity的点击事件:点击文字的时候将当前位置对象刷新到选择的位置,刷新recycleview

mbusstopplateview.setonbusstopplateviewitemclick(new busstopplateview.onbusstopplateviewevent() {
  @override
  public void onitemclick(busstopplatestationinfo station) {
  stationid = station.getstid();
  stationname = station.getstname();
  exportstationinfo(mbusstopplateview.getstationlist());
  alivebusrefresh();

  //当上车提醒保存的信息与当前候车站点信息不一致时恢复为上车提醒,
  // 并在点击上车提醒是判断是否更新上车提醒的站点
  busremind remind = spkeyconfig.getonremind();
  if (remind != null) {
   if (remind.getstationid().equals(stationid) &&
    remind.getlineid().equals(mlineid)) {
   tvonremind.settext("取消提醒");
   ivonremind.setimageresource(r.drawable.bus_icon_onremind_on);
   } else {
   tvonremind.settext("上车提醒");
   ivonremind.setimageresource(r.drawable.bus_icon_onremind_off);
   }
  }
  }

  @override
  public void oncurrentviewposition(int x, int y, boolean isvisibility) {
  mivpoint.settranslationx(x - mivpoint.getwidth() / 2 + 6);
  mivpoint.setvisibility(isvisibility ? view.visible : view.invisible);
  }
 }

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。