{"id":21880540,"url":"https://github.com/brucewind/fixheiview","last_synced_at":"2026-05-17T08:37:16.338Z","repository":{"id":92555918,"uuid":"44587981","full_name":"BruceWind/FixHeiview","owner":"BruceWind","description":"始终保持宽高比的自定义控件。（用于分辨率适配）","archived":false,"fork":false,"pushed_at":"2015-10-28T06:43:19.000Z","size":300,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-26T18:47:05.451Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"http://crm345.com","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/BruceWind.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-20T06:56:24.000Z","updated_at":"2016-12-04T15:46:03.000Z","dependencies_parsed_at":"2023-03-03T13:00:40.760Z","dependency_job_id":null,"html_url":"https://github.com/BruceWind/FixHeiview","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BruceWind%2FFixHeiview","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BruceWind%2FFixHeiview/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BruceWind%2FFixHeiview/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/BruceWind%2FFixHeiview/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/BruceWind","download_url":"https://codeload.github.com/BruceWind/FixHeiview/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244890100,"owners_count":20527031,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-28T09:14:21.558Z","updated_at":"2026-05-17T08:37:16.310Z","avatar_url":"https://github.com/BruceWind.png","language":"Java","funding_links":[],"categories":[],"sub_categories":[],"readme":"## fixheiview这个控件就是高度依据宽高比来自动变化的控件\n\n\n由于最近梯子大多都挂了 由于最近梯子大多都挂了，所以没有使用gradle构建，防止有些人需要重新下载gradle的一些jar包，而没有网络。\n代码已经传到github：[请点击我跳转](https://github.com/weizongwei5/fixheiview)\n\n\n\t做这个控件的目的 就是之前有轮播图，然后轮播图的宽度肯定是要fill_parent，高度肯定以宽度为基准和一个约定的宽高比来计算得出的。\n因为如果服务器图片宽高比有问题的话，我这边界面就会很难看，所以我不能以服务器图片的宽高比来重绘。\n\n我的需求：\n\u003e - 宽度是 fill_parent.\n\u003e - 高度是宽度 × 约定的宽高比.\n\n这样子才是我的需求。\n虽然可以再viewpager添加数据源的时候，通过set高度的方法来控制，只是我觉得这样子手动的操作，写多次会导致代码的赘余。\n仔细思考下我发现还有其他的优点：\n\n\u003e - set高度的方式，从代码设计模式的角度来说不是很完美。如果其他地方用到类似的控件这样子就会导致代码的赘余很厉害。\n\u003e - 如果在listview，gridview的item的布局中也每次都set高度的话，一方面高度难以计算。\n\u003e - 另外一方面这样子的话，会导致计算高度和布局又重新计算一次，而不单单是这个控件重新计算一次，可能父控件也重新计算一次，导致重绘次数过多，不必要的性能损耗。\n\n\n**综上：做到自定义控件里面，合适的重写父类的方法才是最好的选择。**\n\n我这里这个例子配置的是1.0的比例，上截图：\n\n![](http://leanote.com/api/file/getImage?fileId=5626f6d538f4110c8f000181)\n\n“哆啦a梦！你在那？”  哈哈哈！\n\n\n好了，废话说完了。上代码：\n\n\n### 1.自定义属性：\n\n\n``` xml\n  \u003cdeclare-styleable name=\"FixHeiImageView\"\u003e\n        \u003cattr name=\"whratio\" format=\"float\" /\u003e\n    \u003c/declare-styleable\u003e \n\n```\n\n### 2.控件\n\n``` java\n\n\npublic class FixHeiImageView extends ImageView\n{\n\tprivate double wh_ratio=0.0;\n\n\tpublic FixHeiImageView(Context context)\n\t{\n\t\tsuper(context);\n\t\twh_ratio = 2.0;\n\t}\n\t\n\tpublic FixHeiImageView(Context context, double mWh_ratio)\n\t{\n\t\tsuper(context);\n\n\t\tthis.wh_ratio = mWh_ratio;\n\t}\n\n\tpublic FixHeiImageView(Context context, AttributeSet attrs)\n\t{\n\t\tsuper(context, attrs);\n\n\t\tTypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FixHeiImageView);\n\t\twh_ratio = typedArray.getFloat(R.styleable.FixHeiImageView_whratio, (float) 1.0);\n\t\ttypedArray.recycle();\n\t}\n\t\n\tpublic FixHeiImageView(Context context, AttributeSet attrs, int defStyle) {\n\t\tsuper(context, attrs, defStyle);\n\n\t\tTypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.FixHeiImageView);\n\t\twh_ratio = typedArray.getFloat(R.styleable.FixHeiImageView_whratio, (float) 1.0);\n\t\ttypedArray.recycle();\n\t}\n\t\n\t@Override\n\tprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)\n\t{\n\t\t// 父容器传过来的宽度方向上的模式\n        int widthMode = MeasureSpec.getMode(widthMeasureSpec);\n        // 父容器传过来的高度方向上的模式\n        int heightMode = MeasureSpec.getMode(heightMeasureSpec);\n\n        // 父容器传过来的宽度的值\n        int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft()\n                - getPaddingRight();\n        // 父容器传过来的高度的值\n        int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingBottom()\n                - getPaddingTop();\n\n            height = (int) (width / wh_ratio + 0.5f);\n            heightMeasureSpec = MeasureSpec.makeMeasureSpec(height,\n                    MeasureSpec.EXACTLY);\n            super.onMeasure(widthMeasureSpec, heightMeasureSpec);\n\t}\n}\n\n```\n这里代码的逻辑很简单，也有了注释， 我就不再详细解释了。这里是把高度的计算放到onMeasure中了，减少了多次计算和重绘的性能浪费。\n\n\n\u003e - 我这里只是重写了ImageView，如果还有别的需求还可以再改下这个类，为你所用。\n\u003e - 比如你需要重写Linearlayout，重写其他layout之类的。\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrucewind%2Ffixheiview","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrucewind%2Ffixheiview","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrucewind%2Ffixheiview/lists"}