70、电视菜单绘制方案
这个电视菜单绘制方案这里讲一下demo的实现方案
①首先模拟菜单数据。在真实编程中,这里应该是存放在本地数据库或者其他位置的真实菜单数据,由于是demo,所以这里是模拟数据
②然后通过fastJson将json数据(就是上一步模拟的json字符串)转换成对象的列表,这里的对象指的是需要展示在电视上的『展示项目』,比如说图片、线、『鱼香肉丝』等
③遍历对象列表,通过调用MenuManager中的paint方法,根据传入绘制工厂的item的类型,调用不同的绘制方法实现绘图。
以上就是demo的实现方案。
如果要想商业化使用,需要实现以下几个点:
①菜单的切换,这一块主要是展示时菜单的清空和重绘
②菜单的读取,需要能够快速并且无误的完成读写操作
③菜单信息的自动刷新,后台运行service定时从服务器获取售罄菜品列表,和本地菜品列表进行对比,找到应该标为售罄和取消售罄的列表并实现绘制
1、MenuActivity 处理菜单的类
String demoData = "{\n" + " \"menu_type\": 0,\n" + " \"date\": \"\",\n" + " \"day\": \"\",\n" + " \"time_bucket_name\": \"午餐\",\n" + " \"time_bucket_id\": \"31\",\n" + " \"list\": [\n" + " {\n" + " \"item_type\": 0,\n" + " \"item_id\": 1,\n" + " \"content\": \"干货\",\n" + " \"x\": 10,\n" + " \"y\": 80.5,\n" + " \"font_size\": 30,\n" + " \"font_type\": 0,\n" + " \"color\": \"#ffffff\"\n" + " },\n" + " {\n" + " \"item_type\": 1,\n" + " \"item_id\": 2,\n" + " \"content\": \"肉夹馍\",\n" + " \"x\": 100,\n" + " \"y\": 80.5,\n" + " \"font_size\": 30,\n" + " \"font_type\": 1,\n" + " \"color\": \"#000000\",\n" + " \"chargeitem_id\": 341,\n" + " \"price\": 12.5,\n" + " \"show_price\": 0,\n" + " \"indent\": 350,\n" + " \"dynamic_price\": 0,\n" + " \"dynamic_content\": 1\n" + " },\n" + " {\n" + " \"item_type\": 1,\n" + " \"item_id\": 3,\n" + " \"content\": \"好吃的肉夹馍\",\n" + " \"x\": 400,\n" + " \"y\": 180,\n" + " \"font_size\": 30,\n" + " \"font_type\": 0,\n" + " \"color\": \"#000000\",\n" + " \"chargeitem_id\": 341,\n" + " \"price\": 12,\n" + " \"show_price\": 1,\n" + " \"indent\": 400,\n" + " \"dynamic_price\": 1,\n" + " \"dynamic_content\": 1\n" + " },\n" + " {\n" + " \"item_type\": 2,\n" + " \"item_id\": 4,\n" + " \"x\": 0,\n" + " \"y\": 400,\n" + " \"width\": 2048\n" + " },\n" + " {\n" + " \"item_type\": 3,\n" + " \"item_id\": 5,\n" + " \"x\": 500,\n" + " \"y\": 20,\n" + " \"height\": 1000\n" + " },\n" + " {\n" + " \"item_type\": 4,\n" + " \"item_id\": 6,\n" + " \"x\": 100,\n" + " \"y\": 200,\n" + " \"width\": 300,\n" + " \"height\": 300,\n" + " \"src\": \"http://7rflgy.com1.z0.glb.clouddn.com/image-14.jpg\"\n" + " },\n" + " {\n" + " \"item_type\": 5,\n" + " \"item_id\": 7,\n" + " \"src\": \"http://7rflgy.com1.z0.glb.clouddn.com/blackboard.jpeg\",\n" + " \"laying_mode\": 0\n" + " }\n" + " ]\n" + "}";//假设这里是从服务器获取到的某天某个营业时间段的菜单数据 int length = demoData.getBytes().length*4; JSONObject objDemo = JSON.parseObject(demoData); com.alibaba.fastjson.JSONArray arrayDemo = objDemo.getJSONArray("list"); List<DisplayMenuItem> list = com.alibaba.fastjson.JSONArray.parseArray(arrayDemo.toJSONString(), DisplayMenuItem.class);//通过fastJson解析数组中的每个元素,得到的是某个菜单的所有『展示项目』的一个集合 MenuManager.getInstance().showMenu(list);
2、 MenuManager
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /** * 显示菜单,并更新本地的绑定id列表 * * @param displayMenuItemList */ public void showMenu(List<DisplayMenuItem> displayMenuItemList) { mapServerWidgetMatchId.clear(); for (DisplayMenuItem item : displayMenuItemList) { PaintFactory.getPaint(item).paint(rlMain, ivMain); //绘制工厂类根据item的类型在rlMain上进行绘制 if (item.item_type == MenuActivity.ItemTypeMenu) { if (item.chargeitem_id != - 1 && item.chargeitem_id != 0 ) { mapServerWidgetMatchId.put(item.chargeitem_id, item.item_id); //如果是被关联到服务器上的,记录在Map中,用来动态售罄信息 } } } CommonUtils.LogWuwei( "" , "size is " +mapServerWidgetMatchId.keySet().size()); } |
3、绘制工厂类PaintFactory
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | /** * author: Created by zzl on 16/1/8. */ public class PaintFactory { public static Context ctxt; public static int fenbianlv[]= new int [ 2 ]; public static double x_fraction; public static double y_fraction; public static Typeface fontFaceChalkBoard; public static void init() { ctxt = MainApplication.getContext(); fenbianlv = CommonUtils.getScreenWidthAndHeight(MainApplication.getmActivity()); x_fraction = fenbianlv[ 0 ] / 1920.0 ; y_fraction = fenbianlv[ 1 ] / 1080.0 ; fontFaceChalkBoard = Typeface.createFromAsset(MainApplication.getmActivity().getAssets(), "fonts/pianpina.ttf" ); } public static BasePaint getPaint(DisplayMenuItem item) { switch (item.item_type) //根据item的类型调用不同的方法完成绘制 { case MenuActivity.ItemTypeText: return new PaintText(item); case MenuActivity.ItemTypeMenu: return new PaintMenuItem(item); case MenuActivity.ItemTypeHorizontalLine: return new PaintHorizontalLine(item); case MenuActivity.ItemTypeVerticalLine: return new PaintVerticalLine(item); case MenuActivity.ItemTypePicture: return new PaintPic(item); case MenuActivity.ItemTypeBackgroundPic: return new PaintBackgroundPic(item); } return null ; } } |
分类:
android应用开发
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?