这个电视菜单绘制方案这里讲一下demo的实现方案
①首先模拟菜单数据。在真实编程中,这里应该是存放在本地数据库或者其他位置的真实菜单数据,由于是demo,所以这里是模拟数据
②然后通过fastJson将json数据(就是上一步模拟的json字符串)转换成对象的列表,这里的对象指的是需要展示在电视上的『展示项目』,比如说图片、线、『鱼香肉丝』等
③遍历对象列表,通过调用MenuManager中的paint方法,根据传入绘制工厂的item的类型,调用不同的绘制方法实现绘图。
以上就是demo的实现方案。
如果要想商业化使用,需要实现以下几个点:
①菜单的切换,这一块主要是展示时菜单的清空和重绘
②菜单的读取,需要能够快速并且无误的完成读写操作
③菜单信息的自动刷新,后台运行service定时从服务器获取售罄菜品列表,和本地菜品列表进行对比,找到应该标为售罄和取消售罄的列表并实现绘制
1、MenuActivity 处理菜单的类
String demoData = "{ " + " "menu_type": 0, " + " "date": "", " + " "day": "", " + " "time_bucket_name": "午餐", " + " "time_bucket_id": "31", " + " "list": [ " + " { " + " "item_type": 0, " + " "item_id": 1, " + " "content": "干货", " + " "x": 10, " + " "y": 80.5, " + " "font_size": 30, " + " "font_type": 0, " + " "color": "#ffffff" " + " }, " + " { " + " "item_type": 1, " + " "item_id": 2, " + " "content": "肉夹馍", " + " "x": 100, " + " "y": 80.5, " + " "font_size": 30, " + " "font_type": 1, " + " "color": "#000000", " + " "chargeitem_id": 341, " + " "price": 12.5, " + " "show_price": 0, " + " "indent": 350, " + " "dynamic_price": 0, " + " "dynamic_content": 1 " + " }, " + " { " + " "item_type": 1, " + " "item_id": 3, " + " "content": "好吃的肉夹馍", " + " "x": 400, " + " "y": 180, " + " "font_size": 30, " + " "font_type": 0, " + " "color": "#000000", " + " "chargeitem_id": 341, " + " "price": 12, " + " "show_price": 1, " + " "indent": 400, " + " "dynamic_price": 1, " + " "dynamic_content": 1 " + " }, " + " { " + " "item_type": 2, " + " "item_id": 4, " + " "x": 0, " + " "y": 400, " + " "width": 2048 " + " }, " + " { " + " "item_type": 3, " + " "item_id": 5, " + " "x": 500, " + " "y": 20, " + " "height": 1000 " + " }, " + " { " + " "item_type": 4, " + " "item_id": 6, " + " "x": 100, " + " "y": 200, " + " "width": 300, " + " "height": 300, " + " "src": "http://7rflgy.com1.z0.glb.clouddn.com/image-14.jpg" " + " }, " + " { " + " "item_type": 5, " + " "item_id": 7, " + " "src": "http://7rflgy.com1.z0.glb.clouddn.com/blackboard.jpeg", " + " "laying_mode": 0 " + " } " + " ] " + "}";//假设这里是从服务器获取到的某天某个营业时间段的菜单数据 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
/** * 显示菜单,并更新本地的绑定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
/** * 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; } }