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

作者:kururunga 时间:2021-07-12 12:00:04 

本文实例为大家分享了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);
 }
}

来源:https://blog.csdn.net/kururunga/article/details/84762888

标签:Android,View,轨迹图
0
投稿

猜你喜欢

  • java求100之内的素数(质数)简单示例

    2023-11-07 03:12:32
  • Java实现简单LRU缓存机制的方法

    2023-08-11 03:54:49
  • java递归实现拼装多个api的结果操作方法

    2023-11-24 23:44:35
  • 轻松掌握Java建造者模式

    2023-11-06 15:24:38
  • SpringCloud Feign参数问题及解决方法

    2022-08-08 08:00:35
  • Android 破解视频App去除广告功能详解及解决办法总结

    2022-07-16 23:28:50
  • android实现欢迎界面效果

    2021-11-05 14:44:52
  • Android布局之绝对布局AbsoluteLayout详解

    2023-07-26 07:44:27
  • android使用PullToRefresh框架实现ListView下拉刷新上拉加载更多

    2023-05-23 05:29:16
  • Unity Shader实现黑幕过场效果

    2022-01-13 00:18:10
  • Android 四种动画效果的调用实现代码

    2021-06-26 17:59:54
  • Android实现网易云音乐高仿版流程

    2023-05-13 22:08:50
  • Java编程复用类代码详解

    2021-09-13 10:06:34
  • 探讨C#中Dispose方法与Close方法的区别详解

    2023-01-24 03:59:55
  • java 引用传递的三种类型小结

    2023-09-03 03:59:47
  • C# 得到某一天的起始和截止时间的代码

    2021-11-13 10:57:54
  • 为什么Java volatile++不是原子性的详解

    2022-03-09 23:39:41
  • 简单谈谈java的异常处理(Try Catch Finally)

    2021-08-01 12:40:02
  • 详解Java中Optional类的使用方法

    2023-11-25 05:22:28
  • C# BitArray(点矩阵)转换成int和string的方法实现

    2023-06-18 07:33:44
  • asp之家 软件编程 m.aspxhome.com