Android自定义View
来源:互联网 发布:烈火封神翅膀升级数据 编辑:程序博客网 时间:2024/05/24 06:15
Android自定义View
自定义View主要需要了解View的measure、layout和draw过程。其中measure用于测量View的宽和高,layout用来确定View在父容器中的放置位置,而draw负责将View绘制在屏幕上。
Measure过程
View的绘制过程是从ViewRoot的performTraversal方法开始的,它经过measure,layout,draw三个过程最终将一个View绘制出来。而performTraversal方法会依次调用performMeasure,performLayout和performDraw三个方法,这三个方法分别完成顶级View的measure,layout和draw这三个流程,其中performMeasure中会调用measure方法,在measure方法中又会调用onMeasure方法,在onMeasure方法中则会对所有的子元素进行measure过程,接着子元素重复父元素的measure过程,如此反复就完成了整个View树的遍历。
理解MeasureSpec
MeasureSpec是一个32位int值,高2位代表SpecMode,低30位代表SpecSize,SpecMode是指测量模式,而SpecSize是指在某种测量模式下的规格大小。
SpecMode有三类,每一类都有特殊的含义。
UNSPECIFIED: 父容器不对View有任何限制,要多大给多大。这种情况一般用于系统内部,表示一种测量状态.
EXACTLY: 父容器已经检测出View所需要的精确大小,这时候View的最终大小就是SpecSize所指定的值,他对应于match_parent和具体数值这两种模式。
AT_MOST: 父容器指定了一个可用大小即SpecSize,View的大小不能大于这个值,对应于wrap_content.
普通View的大小由自身的LayoutParams和父容器的MeasureSpec共同决定,MeasureSpec一旦确定后,onMeasure中就可以确定View的测量宽高了。
在activity中获得view的大小
无法保证activity执行了onCreate, onStart, onResume时某个View已经测量完毕了,以下有三个方法解决这个问题:
(1) Activity / View 的onWindowFocusChanged会被调用多次,当activity的窗口得到焦点和失去焦点时均会被调用一次。
public void onWindowFocusChanged(boolean hasFocus){ super.onWindowFocusChanged(hasFocus); if(hasFocus){ int width = view.getMeasuredWidth(); int height = view.getMeasuredHeight(); }}
(2) view.post(runnable),通过post可以将一个runnable投递到消息队列的队尾,然后等待Looper调用此runnable的时候,View也已经初始化好了。
protected void onStart(){ super.onStart(); view.post(new Runnable(){ @override public void run(){ int width = view.getMeasuredWidth(); int height = view.getMeasuredHeight(); } });}
(3) 使用ViewTreeObserver的众多回调可以完成这个功能,比如使用OnGlobalLayoutListener这个接口,当View树的状态发生改变或者View树内部的View的可见性发生改变时,onGlobalLayout方法将被回调,因此这是获得宽高的好时机。
protected void onStart(){ super.onStart(); ViewTreeObserver observer = view.getViewTreeObserver(); observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener(){ public void onGlobalLayout(){ view.getViewTreeObserver().removeGlobalOnLayoutListener(this); int width = view.getMeasuredWidth(); int height = view.getMeasuredHeight(); } });}
Layout过程
Layout的作用是ViewGroup用来确定子元素的位置,当ViewGroup的位置被确定后,它在onLayout中会遍历所有子元素并调用其layout方法,在layout方法中onLayout方法又会被调用。layout方法确定View本身的位置,而onLayout确定所有子元素的位置。在View中onMeasure确定了View的测量宽高,而layout则确定View的最终宽高。
public void layout(int l, int t, int r, int b){ super.layout(l, t, r, b);}
宽为r - l , 高为 b - t。即测量宽高可以不等于最终宽高,最终宽高是由layout函数决定的。
Draw过程
draw过程包含如下几步:
a. 绘制背景
b. 绘制自己
c. 绘制children
d. 绘制装饰
View的绘制过程传递是通过dispatchDraw来实现的,dispatchDraw会遍历调用所有元素的draw方法,如此draw事件就一层层地传递了下去。
自定义View
自定义View须知:
1.让View支持wrap_content
2.如果有必要,让View支持padding. 而margin则不需要,因为margin是父View处理的
3.View中如果有线程或者动画,需要及时停止,防止内存泄漏。在onDetachedFromWindow中处理
下面自定义一个简单的View为例:
public class CircleView extends View { private Paint mPaint; public CircleView(Context context) { this(context, null); } public CircleView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public CircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context, attrs, defStyleAttr); } private void init(Context context, AttributeSet attrs, int defStyleAttr) { //自定义属性 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleView, defStyleAttr, 0); int color = typedArray.getColor(R.styleable.CircleView_Circlecolor, 0x55ff6677); typedArray.recycle(); mPaint = new Paint(); mPaint.setColor(color); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { //处理wrap_content属性的大小 int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); if(widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST){ setMeasuredDimension(200, 200); }else if(widthSpecMode == MeasureSpec.AT_MOST){ setMeasuredDimension(200, heightSpecSize); }else if(heightSpecMode == MeasureSpec.AT_MOST){ setMeasuredDimension(heightSpecSize, 200); }else{ setMeasuredDimension(widthSpecSize, heightSpecSize); } } @Override protected void onDraw(Canvas canvas) { //在自定义View中处理padding int paddingLeft = getPaddingLeft(); int paddingTop = getPaddingTop(); int paddingRight = getPaddingRight(); int paddingBottom = getPaddingBottom(); int width = getWidth() - paddingLeft - paddingRight; int height = getHeight() - paddingTop - paddingBottom; int radiu = Math.min(width, height) / 2; canvas.drawCircle(width / 2 + paddingLeft, height / 2 + paddingTop , radiu, mPaint); }}
自定义属性文件attrs.xml内容:
<?xml version="1.0" encoding="utf-8"?><resources> <declare-styleable name="CircleView"> <attr name="Circlecolor" format="color"/> </declare-styleable></resources>
在布局文件中使用该自定义View:
<?xml version="1.0" encoding="utf-8"?><android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.xin.viewdemo.MainActivity"> <com.example.xin.viewdemo.CircleView android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingTop="20dp" app:Circlecolor="#895"/></android.support.constraint.ConstraintLayout>
自定义View使用时要是使用View的完整类名(包括包名),使用时要用自定义属性,需要加入
xmlns:app=”http://schemas.android.com/apk/res-auto”
- Android View---自定义View
- Android View---自定义View
- Android 自定义View 之 自定义View属性
- 【自定义View系列】android自定义View概述
- Android自定义view自定义属性
- Android自定义控件 -- 自定义View
- android自定义view(自定义数字键盘)
- Android自定义View-自定义属性
- Android自定义View-自定义属性
- Android 自定义View
- Android 自定义 View
- android自定义View
- Android 中自定义 view
- android 自定义view组件
- Android 自定义 View
- android 自定义view
- Android:如何自定义View
- android 自定义View
- 常用几个交换机命令
- 利用多线程写一个卖票程序
- fastjson 阿里巴巴json使用
- git常见问题之git pull时Automatic merge failed; fix conflicts and then commit the result.
- The animation state could not be played because it couldn't be found!
- Android自定义View
- 三种网络工作模式
- java遇到的问题总结
- 如何让imageView铺满屏幕?
- ls命令——目录基本操作
- linux下boost编译
- 服务器车牌云识别使用方案
- Centos 7 安装 Python3.6后yum无法使用的解决方法
- HBase全网最佳学习资料汇总