城镇建设周刊网站,建设网app下载安装,网站建设合同付款比例,企业年报入口官网查询系统前言 本文是自定义view中最简单的使用方法#xff0c;分别进行 ‘onMeasure’、‘onDraw’、‘自定义样式’、‘lifecycle’的简单使用#xff0c;了解自定义view的使用。
通过lifecycle来控制 动画的状态 一、onMeasure做了什么#xff1f;
在onMeasure中获取view 的宽和…前言 本文是自定义view中最简单的使用方法分别进行 ‘onMeasure’、‘onDraw’、‘自定义样式’、‘lifecycle’的简单使用了解自定义view的使用。
通过lifecycle来控制 动画的状态 一、onMeasure做了什么
在onMeasure中获取view 的宽和高 是 ‘0’ 测量View的宽 / 高
在某些情况下需要多次测量measure才能确定View最终的宽/高该情况下measure过程后得到的宽 / 高可能不准确此处建议在layout过程中onLayout()去获取最终的宽 / 高 必须要了解 MeasureSpec 作用
测量规格(MeasureSpec)是由测量模式(mode)和测量大小(size)组成共32位(int类型)其中
测量模式(mode)占测量规格(MeasureSpec)的高2位测量大小(size)占测量规格(MeasureSpec)的低30位。
MeasureSpec类用一个变量封装了测量模式(mode)和测量大小(size)通过使用二进制将测量模式mode)和测量大小(size打包成一个int值并提供了打包和解包的方法这样的做法是为了减少对象内存分配和提高存取效率。具体使用如下所示 override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)val widthModel MeasureSpec.getMode(widthMeasureSpec)val widthSize MeasureSpec.getSize(widthMeasureSpec)val heightModel MeasureSpec.getMode(heightMeasureSpec)val heightSize MeasureSpec.getSize(heightMeasureSpec)
// TODO 在 onMeasure 中获取view的 宽高 获取到是 0Log.e(TAG, onMeasure: ${widthSize}-${width}__${heightSize}__${height})val defWidth 400val defHeight 400
// TODO MeasureSpec.AT_MOST:wrap_content ; MeasureSpec.EXACTLY:match_parent ;if (widthModel MeasureSpec.AT_MOST heightModel MeasureSpec.AT_MOST) {setMeasuredDimension(defWidth, defHeight)} else if (widthModel MeasureSpec.AT_MOST) {setMeasuredDimension(defWidth, heightSize)} else if (heightModel MeasureSpec.AT_MOST) {setMeasuredDimension(widthSize, defHeight)}} 2、onLayout 做了什么
计算位置里面包含子view 的情况下才会用到这个函数
一般继承自viewGroup或者重新写layout布局 3、onDraw 做了什么
绘制View自身设置padding 时要在onDraw中计算 1. 绘制view背景
2. 绘制view内容
3. 绘制子View
4. 绘制装饰渐变框滑动条等等 override fun onDraw(canvas: Canvas?) {super.onDraw(canvas)canvas?.let {val pL paddingLeftval pR paddingRightval pT paddingTopval pB paddingBottomvar mHeight height - pT - pBvar mWidth width - pL - pRval cy pT.plus(pB).div(2) mHeight.div(2).toFloat()val cx pL.plus(pR).div(2) mWidth.div(2).toFloat()val cc Math.min(mHeight, mWidth).div(2).toFloat()it.drawCircle(cx,cy,cc,mPaint)}}
4、lifecycle控制动画的状态
自定义view 继承 DefaultLifecycleObserver 类 然后实现 生命周期中的方法override fun onStart(owner: LifecycleOwner) {super.onStart(owner)animSetColor.start()}override fun onDestroy(owner: LifecycleOwner) {super.onDestroy(owner)animSetColor.cancel()}override fun onPause(owner: LifecycleOwner) {super.onPause(owner)animSetColor.pause()}override fun onResume(owner: LifecycleOwner) {super.onResume(owner)animSetColor.resume()}在Act中 进行生命周期监听的绑定lifecycle.addObserver(customView)
5、代码示例
自定义View代码 /*** TODO 自定义view***/
class MyView(context: Context?, attrs: AttributeSet?) :View(context, attrs), DefaultLifecycleObserver {private val mPaint by lazy { Paint() }private val TAG MyViewprivate var i 0// TODO 动画实现改变颜色 然后 通过 lifecycle 控制动画的状态开始、暂停、恢复、取消private val animSetColor by lazy {ValueAnimator.ofInt(0, 100).apply {addListener(object : AnimatorListener {override fun onAnimationStart(animation: Animator) {}override fun onAnimationEnd(animation: Animator) {}override fun onAnimationCancel(animation: Animator) {}override fun onAnimationRepeat(animation: Animator) {iif (i % 2 0) {mPaint.color android.graphics.Color.BLUE}mPaint.color when (i % 5) {0 - android.graphics.Color.BLUE1 - android.graphics.Color.YELLOW2 - android.graphics.Color.CYAN3 - android.graphics.Color.MAGENTA4 - android.graphics.Color.LTGRAYelse - android.graphics.Color.TRANSPARENT}
// TODO 每次设置颜色后 调用postInvalidate 重新绘制ViewpostInvalidate()}})
// 动画无线循环执行repeatCount ValueAnimator.INFINITE
// 间隔一秒执行一次duration 1000}}init {mPaint.color Color.Blue.hashCode()mPaint.style Paint.Style.FILLmPaint.strokeWidth 20fcontext?.obtainStyledAttributes(attrs, R.styleable.MyView)?.apply {mPaint.color getColor(R.styleable.MyView_circlr_color, android.graphics.Color.GREEN)recycle()}}override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {super.onMeasure(widthMeasureSpec, heightMeasureSpec)val widthModel MeasureSpec.getMode(widthMeasureSpec)val widthSize MeasureSpec.getSize(widthMeasureSpec)val heightModel MeasureSpec.getMode(heightMeasureSpec)val heightSize MeasureSpec.getSize(heightMeasureSpec)
// TODO 在 onMeasure 中获取view的 宽高 获取到是 0Log.e(TAG, onMeasure: ${widthSize}-${width}__${heightSize}__${height})val defWidth 400val defHeight 400
// TODO MeasureSpec.AT_MOST:wrap_content ; MeasureSpec.EXACTLY:match_parent ;if (widthModel MeasureSpec.AT_MOST heightModel MeasureSpec.AT_MOST) {setMeasuredDimension(defWidth, defHeight)} else if (widthModel MeasureSpec.AT_MOST) {setMeasuredDimension(defWidth, heightSize)} else if (heightModel MeasureSpec.AT_MOST) {setMeasuredDimension(widthSize, defHeight)}}
//挂在到Act上时
// override fun onAttachedToWindow() {
// super.onAttachedToWindow()
// Log.e(TAG, onAttachedToWindow: )
// anim.start()
// }//在Act 销毁时
// override fun onDetachedFromWindow() {
// super.onDetachedFromWindow()
// Log.e(TAG, onDetachedFromWindow: )
// anim.cancel()
//
// }override fun onStart(owner: LifecycleOwner) {super.onStart(owner)animSetColor.start()}override fun onDestroy(owner: LifecycleOwner) {super.onDestroy(owner)animSetColor.cancel()}override fun onPause(owner: LifecycleOwner) {super.onPause(owner)animSetColor.pause()}override fun onResume(owner: LifecycleOwner) {super.onResume(owner)animSetColor.resume()}override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {super.onLayout(changed, left, top, right, bottom)Log.e(TAG, onLayout: )}/*** 作用根据给定的 Canvas 自动渲染View包括其所有子 View。* 绘制过程* 1. 绘制view背景* 2. 绘制view内容* 3. 绘制子View* 4. 绘制装饰渐变框滑动条等等* 注* a. 在调用该方法之前必须要完成 layout 过程* b. 所有的视图最终都是调用 View 的 draw()绘制视图 ViewGroup 没有复写此方法* c. 在自定义View时不应该复写该方法而是复写 onDraw(Canvas) 方法进行绘制* d. 若自定义的视图确实要复写该方法那么需先调用 super.draw(canvas)完成系统的绘制然后再进行自定义的绘制*/override fun onDraw(canvas: Canvas?) {super.onDraw(canvas)canvas?.let {val pL paddingLeftval pR paddingRightval pT paddingTopval pB paddingBottomvar mHeight height - pT - pBvar mWidth width - pL - pRval cy pT.plus(pB).div(2) mHeight.div(2).toFloat()val cx pL.plus(pR).div(2) mWidth.div(2).toFloat()val cc Math.min(mHeight, mWidth).div(2).toFloat()it.drawCircle(cx,cy,cc,mPaint)}}
}
自定义View的xml样式文件
?xml version1.0 encodingutf-8?
resourcesdeclare-styleable nameMyViewattr namecirclr_color formatcolor//declare-styleable
/resources
layout布局文件
?xml version1.0 encodingutf-8?
androidx.constraintlayout.widget.ConstraintLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:apphttp://schemas.android.com/apk/res-autoxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthmatch_parentandroid:layout_heightmatch_parentandroid:background#11008811tools:context.CustomViewActivitycom.andriod.police.view.MyViewandroid:idid/customViewandroid:layout_widthwrap_contentandroid:layout_height130dpandroid:background#11f08811app:circlr_colorcolor/cardview_light_backgroundandroid:padding20dpapp:layout_constraintStart_toStartOfparentapp:layout_constraintTop_toTopOfparent //androidx.constraintlayout.widget.ConstraintLayout
Act
class CustomViewActivity : AppCompatActivity() {private val customView: MyView by lazy { findViewById(R.id.customView) }override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_custom_view)
// TODO 通过 lifecycle 控制动画的状态开始、暂停、恢复、取消lifecycle.addObserver(customView)}
} 总结 在自定义View中了解在 onMeasure中进行view 的测量在onLayout中进行对view位置的控制在onDraw中进行view的绘制。
通过 lifecycle控制view的生命周期防止出现内存泄露问题如在相应的生命周期中操作动画的执行状态