首页 文章 精选 留言 我的

精选列表

搜索[网站开发],共10000篇文章
优秀的个人博客,低调大师

[Android Pro] Android开发实践:自定义ViewGroup的onLayout()分析

reference to :http://www.linuxidc.com/Linux/2014-12/110165.htm 前一篇文章主要讲了自定义View为什么要重载onMeasure()方法(见http://www.linuxidc.com/Linux/2014-12/110164.htm),那么,自定义ViewGroup又都有哪些方法需要重载或者实现呢 ? Android开 发中,对于自定义View,分为两种,一种是自定义控件(继承View类),另一种是自定义布局容器(继承ViewGroup)。如果是自定义控件,则一 般需要重载两个方法,一个是onMeasure(),用来测量控件尺寸,另一个是onDraw(),用来绘制控件的UI。而自定义布局容器,则一般需要实 现/重载三个方法,一个是onMeasure(),也是用来测量尺寸;一个是onLayout(),用来布局子控件;还有一个是 dispatchDraw(),用来绘制UI。 本文主要分析自定义ViewGroup的onLayout()方法的实现。 ViewGroup类的onLayout()函数是abstract型,继承者必须实现,由于ViewGroup的定位就是一个容器,用来盛放子控 件的,所以就必须定义要以什么的方式来盛放,比如LinearLayout就是以横向或者纵向顺序存放,而RelativeLayout则以相对位置来摆 放子控件,同样,我们的自定义ViewGroup也必须给出我们期望的布局方式,而这个定义就通过onLayout()函数来实现。 我们通过实现一个水平优先布局的视图容器来更加深入地了解onLayout()的实现吧,效果如图所示(黑色方块为子控件,白色部分为自定义布局容器)。该容器的布局方式是,首先水平方向上摆放子控件,水平方向放不下了,则另起一行继续水平摆放。 1. 自定义ViewGroup的派生类 第一步,则是自定ViewGroup的派生类,继承默认的构造函数。 public class CustomViewGroup extends ViewGroup { public CustomViewGroup(Context context) { super(context); } public CustomViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } public CustomViewGroup(Context context, AttributeSet attrs, intdefStyle) { super(context, attrs, defStyle); } } 2. 重载onMeasure()方法 为什么要重载onMeasure()方法这里就不赘述了,上一篇文章已经讲过,这里需要注意的是,自定义ViewGroup的onMeasure()方法中,除了计算自身的尺寸外,还需要调用measureChildren()函数来计算子控件的尺寸。 onMeasure()的定义不是本文的讨论重点,因此这里我直接使用默认的onMeasure()定义,当然measureChildren()是必须得加的。 @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { measureChildren(widthMeasureSpec, heightMeasureSpec); super.onMeasure(widthMeasureSpec, heightMeasureSpec); } 3. 实现onLayout()方法 onLayout()函数的原型如下: //@paramchanged 该参数指出当前ViewGroup的尺寸或者位置是否发生了改变//@paramleft top right bottom 当前ViewGroup相对于其父控件的坐标位置 protected void onLayout(boolean changed,int left, int top, int right, int bottom); 由于我们希望优先横向布局子控件,那么,首先,我们知道总宽度是多少,这个值可以通过getMeasuredWidth()来得到,当然子控件的宽度也可以通过子控件对象的getMeasuredWidth()来得到。 这样,就不复杂了,具体的实现代码如下所示: protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int mViewGroupWidth = getMeasuredWidth(); //当前ViewGroup的总宽度 int mPainterPosX = left; //当前绘图光标横坐标位置 int mPainterPosY = top; //当前绘图光标纵坐标位置 int childCount = getChildCount(); for ( int i = 0; i < childCount; i++ ) { View childView = getChildAt(i); int width = childView.getMeasuredWidth(); int height = childView.getMeasuredHeight(); //如果剩余的空间不够,则移到下一行开始位置 if( mPainterPosX + width > mViewGroupWidth ) { mPainterPosX = left; mPainterPosY += height; } //执行ChildView的绘制 childView.layout(mPainterPosX,mPainterPosY,mPainterPosX+width, mPainterPosY+height); //记录当前已经绘制到的横坐标位置 mPainterPosX += width; } } 4. 布局文件测试 下面我们就尝试写一个简单的xml文件,来测试一下我们的自定义ViewGroup,我们把子View的背景颜色都设置为黑色,方便我们辨识。 <com.titcktick.customview.CustomViewGroup xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" android:background="@android:color/black"/> <View android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" android:background="@android:color/black"/> <View android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" android:background="@android:color/black"/> <View android:layout_width="100dp" android:layout_height="100dp" android:layout_margin="10dp" android:background="@android:color/black"/> </com.titcktick.customview.CustomViewGroup> 5. 添加layout_margin 为了让核心逻辑更加清晰,上面的onLayout()实现我隐去了margin的计算,这样就会导致子控件的layout_margin不起效果,所以上述效果是子控件一个个紧挨着排列,中间没有空隙。那么,下面我们来研究下如何添加margin效果。 其实,如果要自定义ViewGroup支持子控件的layout_margin参数,则自定义的ViewGroup类必须重载 generateLayoutParams()函数,并且在该函数中返回一个ViewGroup.MarginLayoutParams派生类对象,这样 才能使用margin参数。 ViewGroup.MarginLayoutParams的定义关键部分如下,它记录了子控件的layout_margin值: public static class MarginLayoutParams extends ViewGroup.LayoutParams { public int leftMargin; public int topMargin; public int rightMargin; public int bottomMargin; } 你可以跟踪源码看看,其实XML文件中View的layout_xxx参数都是被传递到了各种自定义ViewGroup.LayoutParams派生类对象中。例如LinearLayout的LayoutParams定义的关键部分如下: public class LinearLayout extends ViewGroup { public static class LayoutParams extends ViewGroup.MarginLayoutParams { public float weight; public int gravity = -1; public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); TypedArray a = c.obtainStyledAttributes(attrs, com.android.internal.R.styleable.LinearLayout_Layout); weight = a.getFloat(com.android.internal.R.styleable.LinearLayout_Layout_layout_weight, 0); gravity = a.getInt(com.android.internal.R.styleable.LinearLayout_Layout_layout_gravity, -1); a.recycle(); } } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new LinearLayout.LayoutParams(getContext(), attrs); } } 这样你大概就可以理解为什么LinearLayout的子控件支持weight和gravity的设置了吧,当然我们也可以这样自定义一些属于我们 ViewGroup特有的params,这里就不详细讨论了,我们只继承MarginLayoutParams来获取子控件的margin值。 public class CustomViewGroup extends ViewGroup { public static class LayoutParams extends ViewGroup.MarginLayoutParams { public LayoutParams(Context c, AttributeSet attrs) { super(c, attrs); } } @Override public LayoutParams generateLayoutParams(AttributeSet attrs) { return new CustomViewGroup.LayoutParams(getContext(), attrs); } } 这样修改之后,我们就可以在onLayout()函数中获取子控件的layout_margin值了,添加了layout_margin的onLayout()函数实现如下所示: @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { int mViewGroupWidth = getMeasuredWidth(); //当前ViewGroup的总宽度 int mViewGroupHeight = getMeasuredHeight(); //当前ViewGroup的总高度 int mPainterPosX = left; //当前绘图光标横坐标位置 int mPainterPosY = top; //当前绘图光标纵坐标位置 int childCount = getChildCount(); for ( int i = 0; i < childCount; i++ ) { View childView = getChildAt(i); int width = childView.getMeasuredWidth(); int height = childView.getMeasuredHeight(); CustomViewGroup.LayoutParams margins = (CustomViewGroup.LayoutParams)(childView.getLayoutParams()); //ChildView占用的width = width+leftMargin+rightMargin //ChildView占用的height = height+topMargin+bottomMargin //如果剩余的空间不够,则移到下一行开始位置 if( mPainterPosX + width + margins.leftMargin + margins.rightMargin > mViewGroupWidth ) { mPainterPosX = left; mPainterPosY += height + margins.topMargin + margins.bottomMargin; } //执行ChildView的绘制 childView.layout(mPainterPosX+margins.leftMargin, mPainterPosY+margins.topMargin,mPainterPosX+margins.leftMargin+width, mPainterPosY+margins.topMargin+height); mPainterPosX += width + margins.leftMargin + margins.rightMargin; } } 6. 总结 费了好大劲,终于算是把自定义ViewGroup的onLayout()相关知识点讲清楚了,如果有任何疑问欢迎留言或者来信lujun.hust@gmail.com交流. 分类:Android Pro 本文转自demoblog博客园博客,原文链接http://www.cnblogs.com/0616--ataozhijia/p/6135794.html如需转载请自行联系原作者 demoblog

优秀的个人博客,低调大师

苏宁中兴战略携手 将联合开发智能家居

日前,苏宁和中兴通讯在北京签署战略合作协议。未来,苏宁、中兴将在线上线下O2O、物联网、大数据、智能家居等领域全方位整合资源,力求打造新的渠道、营销和生态模式。“中兴与苏宁的战略合作,不仅是两个企业深度合作的开端,同时也是中国零售业和高科技业紧密融合的开始。”苏宁控股集团董事长张近东表示,这是两个对商业本质有清晰认知和精准把握的企业,在互联网时代,不断锻造核心能力,输出核心能力,是开放共享的一次合作典范。 作为苏宁与中兴通讯全球战略合作的第一步,2015年12月31日,苏宁旗下苏宁润东以19.3亿元投资努比亚。就在发布会现场,双方公布了努比亚手机未来3年在苏宁渠道1000万台的销售目标。努比亚品牌创始人、努比亚技术有限公司总裁里强表示:“苏宁对于努比亚品牌理念和发展战略上的高度认同是促成此次合作非常重要的原因。苏宁的入股将为努比亚产品研发、设计等方面提供强力资源补充,在产品层面将会注入更多新科技。借助苏宁用户大数据方面的优势,我们想像力转化成产品的周期也将大大缩减。另外,有了PPTV强大的优势资源互补,努比亚也将构建更完善的互联网生态。”与此同时,苏宁旗下PPTV也与努比亚签订了合作协议,双方将整合优势资源,联手打造定制手机,并在VR等领域展开合作探索。 业内人士分析,中兴作为国内政企网、智慧城市的领先厂商,将为苏宁搭建更加强健的云桌面、大数据中心、分布式数据库等云端基础设施;双方可以在大数据方面深入合作,挖掘零售数据价值,拓展O2O合作空间。双方联合定制的努比亚可以成为苏宁支付工具等互联网应用的一个重要入口,未来成为万物连接的载体。同时,中兴是全球领先的IPTV/OTT基础技术提供商,苏宁有优秀的文娱内容和渠道优势,双方在智能电视、大视频内容方面将有巨大合作空间。未来双方还将有望在互联网金融、虚拟运营商、IPTV等领域展开全面密切合作。 本文转自d1net(转载)

优秀的个人博客,低调大师

《企业级ios应用开发实战》一2.2 iOS框架介绍

2.2 iOS框架介绍 iOS衍生自Mac OS X的成熟内核,但iOS操作系统更紧凑和高效,支持iPhone和iPod Touch的硬件。iOS继承了Mac OS X的风格,包括:统一的OS X 内核,针对网络的BSD套接字,以及Objective-C和C/C++编译器。iOS框架分为Cocoa Touch、Media、Core Service、Core OS四个层次,如图2-1所示。 这4个层次从上到下排列,位置越高说明层次越抽象,距离硬件底层越远,其特点如下:层次最高的是Cocoa Touch框架,是我们使用得最多的框架,每个iOS应用都要使用,其中包括:UIKit和Foundation(NS),下一节会更详细地介绍这一层。Media框架是对iPhone音频和视频协议的封装,例如,OpenGL ES、EAGL、Quartz、Core Animation、Core Audio、Open Audio Library和Media Player。Core Services框架提供了一些核心框架,诸如Address Book和Core Foundation,后者包含了基本的数据类型定义,如数组和集合。Core OS框架包含系统内核级服务,如线程、文件、I/O、内存和网络。

资源下载

更多资源
腾讯云软件源

腾讯云软件源

为解决软件依赖安装时官方源访问速度慢的问题,腾讯云为一些软件搭建了缓存服务。您可以通过使用腾讯云软件源站来提升依赖包的安装速度。为了方便用户自由搭建服务架构,目前腾讯云软件源站支持公网访问和内网访问。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册