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

 /**
     * 显示菜单,并更新本地的绑定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;
    }
}

 

你可能感兴趣的:(70、电视菜单绘制方案)