关闭

平板中UI 模拟餐馆真实布局

416人阅读 评论(0) 收藏 举报
分类:

最近项目有个需求是实现模拟餐馆真实的餐桌布局,后台给出的数据实体是:

1.

private int height;     //参照的基准高度
private int width;      //参照的基准宽度
private int id;
private String name;
private String roomImg;        //房间的图片
private TablesModel[] tables;  //餐桌实体
2.

private int coordinateX;    //餐桌的X坐标
private int coordinateY;    //餐桌的Y坐标
private int height;         //餐桌的高度
private int width;          //餐桌的宽度
private int id;
private String name;
private int roomId;
private String tableImg;    //餐桌的图片

实现代码:

 private void showRoomsUI(ViewGroup group, RestRoomsModel roomsModel) {
        group.removeAllViews();
        referenceWidth = roomsModel.getWidth();
        referenceHeight = roomsModel.getHeight();
        final TablesModel[] tablesModels = roomsModel.getTables();
        for (int i = 0; i < tablesModels.length; i++) {
            View view = LayoutInflater.from(this)
                    .inflate(R.layout.table_layout /* resource id */, group /* parent */, false);
            setLayout(view, getScaleX(tablesModels[i]), getScaleY(tablesModels[i]),
                    getScaleW(tablesModels[i]), getScaleH(tablesModels[i]));

            ImageView table_img = (ImageView) view.findViewById(R.id.iv_table_img);
            final TextView table_name = (TextView) view.findViewById(R.id.tv_table_name);
            table_name.setText(tablesModels[i].getName());
            AsyncImageLoader.displayImage(tablesModels[i].getTableImg(), table_img);

            group.addView(view);
          
        }
    }

   /**
     * 得到餐桌的宽和高应该缩放的比例
     * @return
     */
    private double getScale() {
        double sh, sw;
        if (WidgetController.getScreenHeight() > WidgetController.getScreenWidth()) {
            sh = WidgetController.getScreenWidth();
            sw = WidgetController.getScreenHeight();
        } else {
            sh = WidgetController.getScreenHeight();
            sw = WidgetController.getScreenWidth();
        }

        // 同比例放大时 检测是高度先充满还是宽带先充满
        if (sw / referenceWidth > sh / referenceHeight) { //高度先充满
            return sh / referenceHeight;
        } else {    //宽度先充满
            return sw / referenceWidth;
        }
    }

    /**
     * 得到餐桌 X 坐标应该缩放的比例
     * @return
     */
    private double getXScale() {
        double sw;
        if (WidgetController.getScreenHeight() > WidgetController.getScreenWidth()) {
            sw = WidgetController.getScreenHeight();
        } else {
            sw = WidgetController.getScreenWidth();
        }

        return sw / referenceWidth;
    }

    /**
     * 得到餐桌 Y 坐标应该缩放的比例
     * @return
     */
    private double getYScale() {
        double sh;
        if (WidgetController.getScreenHeight() > WidgetController.getScreenWidth()) {
            sh = WidgetController.getScreenWidth();
        } else {
            sh = WidgetController.getScreenHeight();
        }

        return sh / referenceHeight;
    }

    /**
     * 得到餐桌的高度
     * @param tablesModel
     * @return
     */
    private int getScaleH(TablesModel tablesModel) {
        return (int) (tablesModel.getHeight() * getScale());
    }

    /**
     * 得到餐桌的宽度
     * @param tablesModel
     * @return
     */
    private int getScaleW(TablesModel tablesModel) {
        return (int) (tablesModel.getWidth() * getScale());
    }

    /**
     * 得到餐桌相对于自己设备的 X 坐标
     * @param tablesModel
     * @return
     */
    private int getScaleX(TablesModel tablesModel) {
        return (int) (tablesModel.getCoordinateX() * getXScale());
    }

    /**
     * 得到餐桌相对于自己设备的 Y 坐标
     * @param tablesModel
     * @return
     */
    private int getScaleY(TablesModel tablesModel) {
        return (int) (tablesModel.getCoordinateY() * getYScale()) + UIUtility.dp2px(48, this);
    }

    /**
     * 动态设置View位置
     *
     * @param view   View
     * @param x      X坐标
     * @param y      Y坐标
     * @param width  按照比例缩放后的宽度
     * @param height 按照比例缩放后的高度
     */
    public void setLayout(View view, int x, int y, int width, int height) {
        ViewGroup.MarginLayoutParams margin = new ViewGroup.MarginLayoutParams(view.getLayoutParams());
        margin.leftMargin = x;
        margin.topMargin = y;
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(margin);
        layoutParams.height = height;
        layoutParams.width = width;
        view.setLayoutParams(layoutParams);
    }


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:462422次
    • 积分:4636
    • 等级:
    • 排名:第6272名
    • 原创:42篇
    • 转载:223篇
    • 译文:0篇
    • 评论:68条
    最新评论