android进阶-自定义控件之量算

本文详细介绍了自定义View的关键步骤,包括布局、测量、绘制的过程,重点解析了MeasureSpec的作用及其实现方式,并提供了实例代码说明如何处理不同父布局下View的尺寸测量问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

知识储备:

  布局、测量、绘制;事件传递机制;动画;

           绘制: canvas、paint、matrix

          动画: property animation

 孙群: 点击打开链接

            量算、布局、绘图、触摸事件是自定义view的核心。


 测量: onMeasure

                  MeasureSpec : UNSPECIFIED \ AT_MOST \ EXACTLY

          继承自ImageView\TextView等已有的控件,其 onMeasure、onDraw方法已有实现,不用管onLayout,继承自viewGroup才需要实现onLayout。

         直接继承自 View的控件,其onMeasure已实现,但是其onDraw是空方法实现,其onLayout也是空实现,但是不用管onLayout。

 继承自view

      

public class MyView extends View {
    public MyView(Context context) {
        super(context);
    }

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        System.out.println("widthMeasureSpec = " + MeasureSpec.getSize(widthMeasureSpec));
        System.out.println("heightMeasureSpec = " + MeasureSpec.getSize(heightMeasureSpec));
        System.out.println("dendity = " + getContext().getResources().getDisplayMetrics().density);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.drawARGB(198, 255, 198, 100);
    }
}

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <com.adnonstop.bitmapshader.views.MyView
        android:layout_width="100dp"
        android:layout_height="100dp" />

</LinearLayout>

01-18 15:04:03.875 2648-2648/com.adnonstop.bitmapshader I/System.out: widthMeasure = 263
01-18 15:04:03.875 2648-2648/com.adnonstop.bitmapshader I/System.out: heightMeasure = 263
01-18 15:04:03.875 2648-2648/com.adnonstop.bitmapshader I/System.out: dendity = 2.625
01-18 15:04:04.005 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 l
01-18 15:04:04.005 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 t
01-18 15:04:04.005 2648-2648/com.adnonstop.bitmapshader I/System.out: 263 r 
01-18 15:04:04.005 2648-2648/com.adnonstop.bitmapshader I/System.out: 263 b

先走测量,-》布局,-》绘制。


父布局是ScrollView

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<com.adnonstop.bitmapshader.views.MyView
    android:layout_width="100dp"
    android:layout_height="100dp" />

</ScrollView>

打印结果:

 

1-18 15:05:54.385 2648-2648/com.adnonstop.bitmapshader I/System.out: widthMeasure = 263
01-18 15:05:54.385 2648-2648/com.adnonstop.bitmapshader I/System.out: heightMeasure = 0
01-18 15:05:54.385 2648-2648/com.adnonstop.bitmapshader I/System.out: dendity = 2.625
01-18 15:05:54.425 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 l
01-18 15:05:54.425 2648-2648/com.adnonstop.bitmapshader I/System.out: 0 t
01-18 15:05:54.425 2648-2648/com.adnonstop.bitmapshader I/System.out: 263 r
01-18 15:05:54.425 2648-2648/com.adnonstop.bitmapshader I/System.out: 0  b

高度未知;需要自己开发者帮助测量高度。

 

如果是HorizontalScrollView,则宽度未知

01-18 15:10:23.325 2648-2648/com.adnonstop.bitmapshader I/System.out: widthMeasure = 0
01-18 15:10:23.325 2648-2648/com.adnonstop.bitmapshader I/System.out: heightMeasure = 263
01-18 15:10:23.325 2648-2648/com.adnonstop.bitmapshader I/System.out: dendity = 2.625
01-18 15:10:23.385 2648-2648/com.adnonstop.bitmapshader I/System.out: l = 0
01-18 15:10:23.385 2648-2648/com.adnonstop.bitmapshader I/System.out: t = 0
01-18 15:10:23.385 2648-2648/com.adnonstop.bitmapshader I/System.out: r = 0
01-18 15:10:23.385 2648-2648/com.adnonstop.bitmapshader I/System.out: b = 263


 未指定就没办法了,你得手动指定了:

 

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        density = getContext().getResources().getDisplayMetrics().density;
        widthMeasureSpec = MeasureSpec.makeMeasureSpec((int) (density * 100), MeasureSpec.EXACTLY);
        heightMeasureSpec = MeasureSpec.makeMeasureSpec((int) (density * 100), MeasureSpec.EXACTLY);
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

打印结果:

 

01-18 15:44:35.665 15142-15142/? I/System.out: widthMeasure = 262
01-18 15:44:35.665 15142-15142/? I/System.out: heightMeasure = 262
01-18 15:44:35.665 15142-15142/? I/System.out: dendity = 2.625
01-18 15:44:35.795 15142-15142/? I/System.out: l = 0
01-18 15:44:35.795 15142-15142/? I/System.out: t = 0
01-18 15:44:35.795 15142-15142/? I/System.out: r = 262
01-18 15:44:35.795 15142-15142/? I/System.out: b = 262

继承自已有的TextView,父布局是scrollView的话,只会根据内容的高度分配高度,而不会根据xml里 layout_heighet = 100dp 设置高度。


 



内容概要:该PPT详细介绍了企业架构设计的方法论,涵盖业务架构、数据架构、应用架构和技术架构四大核心模块。首先分析了企业架构现状,包括业务、数据、应用和技术四大架构的内容和关系,明确了企业架构设计的重要性。接着,阐述了新版企业架构总体框架(CSG-EAF 2.0)的形成过程,强调其融合了传统架构设计(TOGAF)和领域驱动设计(DDD)的优势,以适应数字化转型需求。业务架构部分通过梳理企业级和专业级价值流,细化业务能力、流程和对象,确保业务战略的有效落地。数据架构部分则遵循五大原则,确保数据的准确、一致和高效使用。应用架构方面,提出了分层解耦和服务化的设计原则,以提高灵活性和响应速度。最后,技术架构部分围绕技术框架、组件、平台和部署节点进行了详细设计,确保技术架构的稳定性和扩展性。 适合人群:适用于具有一定企业架构设计经验的IT架构师、项目经理和业务分析师,特别是那些希望深入了解如何将企业架构设计与数字化转型相结合的专业人士。 使用场景及目标:①帮助企业和组织梳理业务流程,优化业务能力,实现战略目标;②指导数据管理和应用开发,确保数据的一致性和应用的高效性;③为技术选型和系统部署提供科学依据,确保技术架构的稳定性和扩展性。 阅读建议:此资源内容详尽,涵盖企业架构设计的各个方面。建议读者在学习过程中,结合实际案例进行理解和实践,重点关注各架构模块之间的关联和协同,以便更好地应用于实际工作中。
资 源 简 介 独立分分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的一种盲信号分离方法。它是一种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的一个研究热点。本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA法以及它们之间的内在联系, 详 情 说 明 独立分分析(Independent Component Analysis,简称ICA)是近二十年来逐渐发展起来的一种盲信号分离方法。它是一种统计方法,其目的是从由传感器收集到的混合信号中分离相互独立的源信号,使得这些分离出来的源信号之间尽可能独立。它在语音识别、电信和医学信号处理等信号处理方面有着广泛的应用,目前已成为盲信号处理,人工神经网络等研究领域中的一个研究热点。 本文简要的阐述了ICA的发展、应用和现状,详细地论述了ICA的原理及实现过程,系统地介绍了目前几种主要ICA法以及它们之间的内在联系,在此基础上重点分析了一种快速ICA实现法一FastICA。物质的非线性荧光谱信号可以看成是由多个相互独立的源信号组合成的混合信号,而这些独立的源信号可以看成是光谱的特征信号。为了更好的了解光谱信号的特征,本文利用独立分分析的思想和方法,提出了利用FastICA法提取光谱信号的特征的方案,并进行了详细的仿真实验。 此外,我们还进行了进一步的研究,探索了其他可能的ICA应用领域,如音乐信号处理、图像处理以及金融数据分析等。通过在这些领域中的实验和应用,我们发现ICA在提取信号特征、降噪和信号分离等方面具有广泛的潜力和应用前景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值