Android自定义View系列

  • Android自定义View之Paint绘制文字和线
  • Android自定义View之图像的色彩处理
  • Android自定义View之Canvas
  • Android自定义View之轻松实现圆角和圆形图片
  • Android自定义View之双缓冲机制和SurfaceView
  • Android自定义View之Window、ViewRootImpl和View的三大流程
  • Android自定义View之事件分发机制总结
  • Android自定义View之requestLayout方法和invalidate方法

自定义View的分类

  • 继承View重写onDraw方法

主要用于实现不规则的效果,即这种效果不方便通过布局的组合方式来实现。相当于就是得自己“画”了。采用这种方式需要自己支持wrap_content,padding也需要自己处理

  • 继承ViewGroup派生特殊的Layout

主要用于实现自定义的布局,看起来很像几种View组合在一起的时候,可以使用这种方式。这种方式需要合适地处理ViewGroup的测量和布局,并同时处理子元素的测量和布局过程。比如自定义一个自动换行的LinerLayout等。

  • 继承特定的View,比如TextView

这种方法主要是用于扩展某种已有的View,增加一些特定的功能。这种方法比较简单,也不需要自己支持wrap_content和padding。

  • 继承特定的ViewGroup,比如LinearLayout

这种方式也比较常见,和上面的第2种方法比较类似,第2种方法更佳接近View的底层。

自定义View有多种方式,需要根据实际需要选择一种简单低成本的方式来实现

自定义View需要注意的地方

  • 让View支持wrap_content

直接继承View和ViewGroup的控件需要在onMeasure方法中处理wrap_content的方法。处理方法是在wrap_content的情况下设置一个固定的尺寸

//处理wrap_content的套路
@Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);//处理WAP_CONTENTint widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) {setMeasuredDimension(200,200);}else if (widthSpecMode == MeasureSpec.AT_MOST) {setMeasuredDimension(200, heightSize);}else if (heightSpecMode == MeasureSpec.AT_MOST) {setMeasuredDimension(widthSize, 200);}
}
  • 让View支持padding

直接继承View的控件需要在onDraw方法中处理padding,否则用户设置padding属性就不会起作用。直接继承ViewGroup的控件需要在onMeasure和onLayout中考虑padding和子元素的margin对其造成的影响,不然将导致padding和子元素的margin失效。

@Override
public void onDraw(Canvas canvas) {super.onDraw(canvas);//获取padding,然后根据实际情况处理就好mPaddingLeft = getPaddingLeft();mPaddingRight = getPaddingRight();mPaddingTop = getPaddingTop();mPaddingBottom = getPaddingBottom();mWidth = getWidth() - mPaddingLeft - mPaddingRight;mHeight = getHeight() - mPaddingTop - mPaddingBottom;
}
  • 尽量不要在View中使用Handler

View中已经提供了post系列方法,完全可以替代Handler的作用。

@UiThread
public class View implements Drawable.Callback, KeyEvent.Callback,AccessibilityEventSource {...public boolean post(Runnable action) {final AttachInfo attachInfo = mAttachInfo;if (attachInfo != null) {return attachInfo.mHandler.post(action);}// Postpone the runnable until we know on which thread it needs to run.// Assume that the runnable will be successfully placed after attach.getRunQueue().post(action);return true;}public boolean postDelayed(Runnable action, long delayMillis) {final AttachInfo attachInfo = mAttachInfo;if (attachInfo != null) {return attachInfo.mHandler.postDelayed(action, delayMillis);}// Postpone the runnable until we know on which thread it needs to run.// Assume that the runnable will be successfully placed after attach.getRunQueue().postDelayed(action, delayMillis);return true;} ...
}
  • View中如果有线程或者动画,需要及时停止

在View的onDetachedFromWindow方法可以停止线程和动画,因为当View被remove或是包含此View的Activity退出时,就会调用View的onDetachedFromWindow方法。如果不处理的话很可能会导致内存泄漏

  • View带有滑动嵌套时,需要处理好滑动冲突问题

  • 在View的onDraw方法中不要创建太多的临时对象,也就是new出来的对象。因为onDraw方法会被频繁调用,如果有大量的临时对象,就会引起内存抖动,影响View的效果



今天你进步了嘛?欢迎关注我的微信公众号,和我一起每天进步一点点!

Android自定义View注意事项相关推荐

  1. Android自定义View之Paint绘制文字和线

    Android自定义View系列 Android自定义View注意事项 Android自定义View之图像的色彩处理 Android自定义View之Canvas Android自定义View之轻松实现 ...

  2. Android自定义view之事件传递机制

    Android自定义view之事件传递机制 在上一篇文章<Android自定义view之measure.layout.draw三大流程>中,我们探讨了一下view的显示过程.不太熟悉的同学 ...

  3. Android自定义View —— TypedArray

    在上一篇中Android 自定义View Canvas -- Bitmap写到了TypedArray 这个属性 下面也简单的说一下TypedArray的使用 TypedArray 的作用: 用于从该结 ...

  4. Android 自定义View —— Canvas

    上一篇在android 自定义view Paint 里面 说了几种常见的Point 属性 绘制图形的时候下面总有一个canvas ,Canvas 是是画布 上面可以绘制点,线,正方形,圆,等等,需要和 ...

  5. android自定义view获取控件,android 自定义控件View在Activity中使用findByViewId得到结果为null...

    转载:http://blog.csdn.net/xiabing082/article/details/48781489 1.  大家常常自定义view,,然后在xml 中添加该view 组件..如果在 ...

  6. Android自定义View:ViewGroup(三)

    自定义ViewGroup本质是什么? 自定义ViewGroup本质上就干一件事--layout. layout 我们知道ViewGroup是一个组合View,它与普通的基本View(只要不是ViewG ...

  7. android 自定义图形,Android自定义View之图形图像(模仿360的刷新球自定

    概述: 360安全卫士的那个刷新球(姑且叫它刷新球,因为真的不知道叫什么好,不是dota里的刷新球!!),里面像住了水一样,生动可爱,看似简单,写起来不太简单,本例程只是实现了它的部分功能而已,说实话 ...

  8. android代码实现手机加速功能,Android自定义View实现内存清理加速球效果

    Android自定义View实现内存清理加速球效果 发布时间:2020-09-21 22:21:57 来源:脚本之家 阅读:105 作者:程序员的自我反思 前言 用过猎豹清理大师或者相类似的安全软件, ...

  9. android中仿qq最新版抽屉,Android 自定义View实现抽屉效果

    Android 自定义View实现抽屉效果 说明 这个自定义View,没有处理好多点触摸问题 View跟着手指移动,没有采用传统的scrollBy方法,而是通过不停地重新布局子View的方式,来使得子 ...

最新文章

  1. 周一02.3运行python程序的两种方式
  2. 联想拯救者y7000怎么配置Java环境_联想拯救者y7000重装系统教程
  3. 每日一皮:举一反三,这么聪明的客户可不多见了...
  4. Swift之深入解析闭包Closures的使用和捕获变量的原理
  5. POJ1015-Jury Compromise【01背包,dp】
  6. java核心技术-jvm基础知识
  7. linux下chkConfig的用法,mysqld开机自启动
  8. kvm启动报错Could not access KVM kernel module: Permission denied
  9. VB 实现UTF-8 与GB2312互转
  10. 从华为“流程与IT管理部”看IT部门定位
  11. 委托应用及泛型委托和多播委托
  12. python程序输入两个整数、实现加减乘除_加减乘除
  13. 小说阅读APP开发定制搭建方案
  14. 微信小程序–二维码生成器
  15. 深入理解Symbol
  16. 【编程题】【Scratch一级】2019.12 小狗长大记
  17. java开发环境变量的配置
  18. sklearn模块之朴素贝叶斯:(二)伯努利模型的实现
  19. 汽车车灯注塑件三维尺寸公差检测
  20. 女神青涩时纤毫毕现,腾讯 AI 模型 GFPGAN 火上 GitHub 热榜第一,Demo 在线可玩

热门文章

  1. revit链接文件操作
  2. 每一个账号对应所有密码,再每一个密码对应所有账号暴力破解代码怎么写?...
  3. 【Linux】Linux 中的UID
  4. Mac 软件出现「意外退出」及「崩溃」修复方法
  5. linux设备驱动开发之环境搭建(基于exynos4412)
  6. 科学计算机 app,万能科学计算器
  7. integer java关键字_JAVA关键字及作用详解
  8. 编译 OpenJDK
  9. abaqus质量缩放系数取值_后处理时的变形缩放系数
  10. pytorch Relu