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


猜你喜欢
- 一、进程线和程的概念线程: 一个线程是一个独立的执行流,每个线程之间都可以按照顺讯执行自己的代码. 多个线程之间 “同时
- 双向循环链表定义相比于单链表,有两个指针,next指针指向下一个结点,prior指针指向上一个结点,最后一个结点的next指针指向头结点,头
- 首先我先声明一点,本文单纯就是技术探讨,要从实际应用中来说的话,我并不建议这样去玩分布式事务、也不建议这样去玩多数据源,毕竟分布式事务主要还
- 最近有很多小伙伴给我留言,分布式系统时代,线程并发,资源抢占,"锁" 慢慢变得很重要。那么常见的锁都有哪些?今天Tom哥
- 关于DocumentCompleted事件,MSDN给出的解释是在文档加载完毕后执行,但是在我的程序中DocumentCompleted却被
- Android动态修改ToolBar的Menu菜单效果图实现实现很简单,就是一个具有3个Action的Menu,在我们滑动到不同状态的时候,
- 这是个很简单的问题,但每次隔一段时间后使用起来总是会出点乱子。这里记录下Logcat的步骤:1,在Activity里申明tag变量(名字其实
- 这里主要利用API函数Animate Window实现窗体左右,上下,扩展,淡入滑动或滚动动画效果,步骤如下:1.新建窗体,使用2个Grou
- 在前一期中,我们做了悬浮头部的两个tab切换和下拉刷新效果,后来项目中要求改成三个tab,当时就能估量了一下,如果从之前的改,也不是不可以,
- 前台处理首先前台先要获取所有的要删除数据的ID,并将ID拼接成字符串 例如: 2,3,4,5,然后通过GET请求返送到后台。后台处理控制器接
- 机器跑了一晚上,发现有崩溃现象,由于页面内有动态绘图功能,我怀疑是绘图原因,但是今天上午有人提醒我才想到,是不是间隔调用时DWR产生了内存泄
- 意义:由于每个应用进程都有自己的独立进程空间,在android平台上,一个进程通常不能访问另一个进程的内存空间,而我们经常需要夸进程传递对象
- java -version 命令大家都用过,大部分就是看下jdk版本或检查下环境变量的设置,但最后一行的信息也挺重要,如下图所示:Serve
- 本文实例讲述了Android编程解析XML方法。分享给大家供大家参考,具体如下:XML在各种开发中都广泛应用,Android也不例外。作为承
- 本文实例讲述了C#画笔使用复合数组绘制单个矩形的方法。分享给大家供大家参考。具体实现方法如下:using System;using Syst
- 这里直接给出C#类成员一般初始化顺序:子类静态字段子类静态构造子类实例字段父类静态字段父类静态构造父类实例字段父类实例构造子类实例构造为什么
- 相同点:二者都是Java的虚拟机,用来执行Java程序区别:javaw.exe运行程序时不会输出控制台信息,如果是双击打开jar文件的话(假
- 本文实例讲述了C#事件用法。分享给大家供大家参考。具体分析如下:EventHandler<TEventArgs>的定义如下pub
- 本文实例为大家分享了C语言实现通讯录小项目的具体代码,供大家参考,具体内容如下编写程序实现通讯录的基本功能,可以做到增,删,查,改,打印通讯
- 前言以多个客户端和一个服务端的socket通信为例,服务端启动时创建一个固定大小的线程池。服务端每接收到一个连接请求后(通信任务),交给线程