自定义宽高比例的view(轮播图,item等)
2016-07-18 09:35
239 查看
为解决轮播图、listview中的item,放在不通分辨率的手机上会出现拉伸变形。
比较简单 不多说 下边直接贴代码
关于下边方法中
在xml中使用的话如下,可直接设置ratia=" "
在values下的 attrs下
Android学习手册APP,手机上随时学习包括(android基础,android组件,用户界面,设备功能,数据储存,网络应用,游戏开发,多媒体,源码开发,高级,android面试题)
http://download.csdn.net/detail/mengshirui_/9576713
比较简单 不多说 下边直接贴代码
关于下边方法中
- getPaddingLeft() - getPaddingRight()后又加上是因为如果view中设置了padding 则会改变正确的比例值
+ 0.5f因float类型转换int类型是为了减少误差[(49.2+0.5f)=49 (49.7+0.5f)=50]
import android.content.Context; import android.content.res.TypedArray; import android.util.AttributeSet; import android.widget.FrameLayout; /** * 作者:Created by mengshirui on 2016-07-18 * 邮箱: * 描述:自定义宽高比例的view */ public class RatioLayout extends FrameLayout { // 宽和高的比例 private float ratio = 0.0f; public RatioLayout(Context context) { this(context, null); } public RatioLayout(Context context, AttributeSet attrs) { this(context, attrs, 0); } public RatioLayout(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); //从xml中获取比例值(需在values下的attrs下声明) TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RatioLayout); ratio = a.getFloat(R.styleable.RatioLayout_ratio, 0.0f); a.recycle(); } /** * 可代码设置,或者布局中设置 <span style="white-space:pre"> </span> * 设置宽高比例 * @param f */ public void setRatio(float f) { ratio = f; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int widthMode = MeasureSpec.getMode(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int width = MeasureSpec.getSize(widthMeasureSpec) - getPaddingLeft() - getPaddingRight(); int height = MeasureSpec.getSize(heightMeasureSpec) - getPaddingTop() - getPaddingBottom(); //MeasureSpec.EXACTLY精确模式 if (widthMode == MeasureSpec.EXACTLY && heightMode != MeasureSpec.EXACTLY && ratio != 0.0f) { height = (int) (width / ratio + 0.5f); heightMeasureSpec = MeasureSpec.makeMeasureSpec(height + getPaddingTop() + getPaddingBottom(), MeasureSpec.EXACTLY); } else if (widthMode != MeasureSpec.EXACTLY && heightMode == MeasureSpec.EXACTLY && ratio != 0.0f) { width = (int) (height * ratio + 0.5f); widthMeasureSpec = MeasureSpec.makeMeasureSpec(width + getPaddingLeft() + getPaddingRight(), MeasureSpec.EXACTLY); }else { new RuntimeException("设置比例值,宽高需要一个是精确模式"); } super.onMeasure(widthMeasureSpec, heightMeasureSpec); } }
在xml中使用的话如下,可直接设置ratia=" "
<com.ontim.googleplay.view.RatioLayout android:layout_width="match_parent" android:layout_height="wrap_content" app:ratio="2.43" > <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:scaleType="fitCenter" android:src="@mipmap/ic_ 4000 launcher"/> </com.ontim.googleplay.view.RatioLayout>
在values下的 attrs下
<declare-styleable name="RatioLayout">
<attr name="ratio" format="float" />
</declare-styleable>
Android学习手册APP,手机上随时学习包括(android基础,android组件,用户界面,设备功能,数据储存,网络应用,游戏开发,多媒体,源码开发,高级,android面试题)
http://download.csdn.net/detail/mengshirui_/9576713
相关文章推荐
- Notification与多线程
- CNN
- 在外部安装svn之后怎么用到vs上面
- 树形DP+DFS序+树状数组 HDOJ 5293 Tree chain problem(树链问题)
- Eclipse导入Android Studio 第三方库无法用add library
- Ubuntu14.04安装rabbitVCS_SVN
- Android事件分发与焦点争夺
- 源码解析之–YYAsyncLayer异步绘制
- 【记录】MOV与LDR
- 扩展欧几里德 noip 2012 day2 同余方程
- JS动态改变form表单里的action值属性的方法
- 【NOIP模拟】Angel Beats!
- ssm整合
- java设计模式之工厂模式
- 8086汇编语言自学经验分享 如何将一个段设置为代码段,数据段,栈段
- linux笔记:linux常用命令-目录和文件处理命令
- ExtJS-服务器代理
- 【记录】ldr
- CodeForces 131C--组合数
- 利用函数返回oracle对象表的三种方法