自定义view中常用到哪些方法作用分别是什么

2024-09-08 08:20

本文主要是介绍自定义view中常用到哪些方法作用分别是什么,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

  • 构造函数
  • onMeasure(int widthMeasureSpec, int heightMeasureSpec)
  • onDraw(Canvas canvas)
  • onLayout(boolean changed, int left, int top, int right, int bottom)
  • onTouchEvent(MotionEvent event)
  • onSizeChanged(int w, int h, int oldw, int oldh)
  • onAttachedToWindow() 和 onDetachedFromWindow()
  • onSaveInstanceState() 和 onRestoreInstanceState(Parcelable state)

在 Android 开发中,自定义 View 是一种常用的技术,允许开发者创建具有独特外观和行为的 UI 组件。自定义 View 通常涉及重写一些关键的方法,以实现特定的绘制和交互逻辑。

构造函数

作用
构造函数用于初始化 View。通常会有三个重载的构造函数,用于不同的初始化场景。

public class CustomView extends View {public CustomView(Context context) {super(context);init(context, null);}public CustomView(Context context, AttributeSet attrs) {super(context, attrs);init(context, attrs);}public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);init(context, attrs);}private void init(Context context, AttributeSet attrs) {// 初始化代码}
}

onMeasure(int widthMeasureSpec, int heightMeasureSpec)

作用
该方法用于测量 View 的大小。开发者需要在这个方法中计算 View 的宽度和高度,并调用 setMeasuredDimension(int measuredWidth, int measuredHeight) 来设置测量结果。

Android之onMeasure的三种模式

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);int width = 0;int height = 0;// 根据测量模式和给定的尺寸来计算 View 的宽高if (widthMode == MeasureSpec.EXACTLY) {width = widthSize;} else if (widthMode == MeasureSpec.AT_MOST) {width = Math.min(200, widthSize); // 假设最大宽度为 200}if (heightMode == MeasureSpec.EXACTLY) {height = heightSize;} else if (heightMode == MeasureSpec.AT_MOST) {height = Math.min(200, heightSize); // 假设最大高度为 200}setMeasuredDimension(width, height);
}

onDraw(Canvas canvas)

作用
该方法用于绘制 View 的内容。开发者需要在这个方法中使用 Canvas 对象来绘制图形、文本或其他内容。

@Override
protected void onDraw(Canvas canvas) {super.onDraw(canvas);Paint paint = new Paint();paint.setColor(Color.RED);paint.setStyle(Paint.Style.FILL);// 绘制一个红色的圆canvas.drawCircle(getWidth() / 2, getHeight() / 2, Math.min(getWidth(), getHeight()) / 2, paint);
}

onLayout(boolean changed, int left, int top, int right, int bottom)

作用
该方法用于布局 View 的子视图。自定义 ViewGroup 通常需要重写这个方法,以确定每个子视图的位置和大小。

@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {int childCount = getChildCount();int currentTop = top;for (int i = 0; i < childCount; i++) {View child = getChildAt(i);int childHeight = child.getMeasuredHeight();child.layout(left, currentTop, right, currentTop + childHeight);currentTop += childHeight;}
}

onTouchEvent(MotionEvent event)

作用
该方法用于处理触摸事件。开发者可以在这个方法中实现自定义的触摸交互逻辑。

@Override
public boolean onTouchEvent(MotionEvent event) {switch (event.getAction()) {case MotionEvent.ACTION_DOWN:// 处理按下事件return true;case MotionEvent.ACTION_MOVE:// 处理移动事件return true;case MotionEvent.ACTION_UP:// 处理抬起事件return true;}return super.onTouchEvent(event);
}

onSizeChanged(int w, int h, int oldw, int oldh)

作用
该方法在 View 的大小发生变化时调用。开发者可以在这个方法中处理与尺寸变化相关的逻辑。

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);// 处理尺寸变化的逻辑
}

onAttachedToWindow() 和 onDetachedFromWindow()

作用
onAttachedToWindow() 在 View 被附加到窗口时调用,可以在这里进行一些资源的初始化。
onDetachedFromWindow() 在 View 从窗口分离时调用,可以在这里进行一些资源的释放

@Override
protected void onAttachedToWindow() {super.onAttachedToWindow();// 资源初始化
}@Override
protected void onDetachedFromWindow() {super.onDetachedFromWindow();// 资源释放
}

onSaveInstanceState() 和 onRestoreInstanceState(Parcelable state)

作用
用于保存和恢复 View 的状态,通常在需要处理配置变化(如屏幕旋转)时使用。

@Override
protected Parcelable onSaveInstanceState() {Parcelable superState = super.onSaveInstanceState();// 保存自定义状态return superState;
}@Override
protected void onRestoreInstanceState(Parcelable state) {super.onRestoreInstanceState(state);// 恢复自定义状态
}

这篇关于自定义view中常用到哪些方法作用分别是什么的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1147601

相关文章

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

SpringBoot 中 CommandLineRunner的作用示例详解

《SpringBoot中CommandLineRunner的作用示例详解》SpringBoot提供的一种简单的实现方案就是添加一个model并实现CommandLineRunner接口,实现功能的... 目录1、CommandLineRunnerSpringBoot中CommandLineRunner的作用

如何自定义一个log适配器starter

《如何自定义一个log适配器starter》:本文主要介绍如何自定义一个log适配器starter的问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求Starter 项目目录结构pom.XML 配置LogInitializer实现MDCInterceptor