您好,登錄后才能下訂單哦!
本篇文章為大家展示了怎么在Android中通過自定義View實現公交成軌跡圖,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
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); } }
Android是一種基于Linux內核的自由及開放源代碼的操作系統,主要使用于移動設備,如智能手機和平板電腦,由美國Google公司和開放手機聯盟領導及開發。
上述內容就是怎么在Android中通過自定義View實現公交成軌跡圖,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。