android自定义控件onMeasure方法

1、自定义控件首先定义一个类继承View

有时,Android系统控件无法满足我们的需求,因此有必要自定义View。具体方法参见官方开发文档:http://developer.android.com/guide/topics/ui/custom-components.html

 

一般来说,自定义控件都会去重写View的onMeasure方法,因为该方法指定该控件在屏幕上的大小。

protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec)

onMeasure传入的两个参数是由上一层控件传入的大小,有多种情况,重写该方法时需要对计算控件的实际大小,然后调用setMeasuredDimension(int, int)设置实际大小。

 

onMeasure传入的widthMeasureSpec和heightMeasureSpec不是一般的尺寸数值,而是将模式和尺寸组合在一起的数值。我们需要通过int mode = MeasureSpec.getMode(widthMeasureSpec)得到模式,用int size = MeasureSpec.getSize(widthMeasureSpec)得到尺寸。

 

mode共有三种情况,取值分别为MeasureSpec.UNSPECIFIED, MeasureSpec.EXACTLY, MeasureSpec.AT_MOST。

 

MeasureSpec.EXACTLY是精确尺寸,当我们将控件的layout_width或layout_height指定为具体数值时如andorid:layout_width="50dip",或者为FILL_PARENT是,都是控件大小已经确定的情况,都是精确尺寸。

 

MeasureSpec.AT_MOST是最大尺寸,当控件的layout_width或layout_height指定为WRAP_CONTENT时,控件大小一般随着控件的子空间或内容进行变化,此时控件尺寸只要不超过父控件允许的最大尺寸即可。因此,此时的mode是AT_MOST,size给出了父控件允许的最大尺寸。

 

MeasureSpec.UNSPECIFIED是未指定尺寸,这种情况不多,一般都是父控件是AdapterView,通过measure方法传入的模式。

我们来看下程序的代码:

xml布局文件:

复制代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="im.weiyuan.com.zidingyikongjian.MainActivity">

    <im.weiyuan.com.zidingyikongjian.MesureView
        android:layout_width="100px"
        android:layout_height="200px" />
</RelativeLayout>
复制代码

我们来看看类的代码,该代码继承自View类

复制代码
package im.weiyuan.com.zidingyikongjian;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by wei.yuan on 2017/6/2.
 */

public class MesureView extends View {
    /**
     *
    * new MesureView(context)的时候调用该构造函数
    * */
    public MesureView(Context context) {
        super(context);
    }

    /**
     * 在布局中引用
     *
     * */

    public MesureView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /**
     * 在布局中引用
     *
     * */
    public MesureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /**
     * 在布局中引用
     *
     * */

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

    /**
     *
     *
     * */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 第一步提取出具体对象的测量模式和大小
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int mode  = MeasureSpec.getMode(widthMeasureSpec);
        Log.d("123456",size+"");
        if(mode == MeasureSpec.EXACTLY){ //默认就是精确的模式
            Log.d("123456",mode+"");
        }

    }
}
复制代码

我们来看看日志打印的效果:

 MeasureSpec.getSize获得对应的控件的大小:单位是px,这里上面测量的是控件的宽度,在xml布局里面设置的是100px,这里打印就是100px
这里控件在xml指定的是具体的100px大小值,这里获得的mode就是精确的模式、
setMeasuredDimension这个方法,这个方法决定了当前View的大小
现在我们在xml中指定了控件的宽度是100px,高度是100px
现在我们要通过代码指定控件的宽度和高度可以使用函数
setMeasuredDimension
现在我们的首先的像素是1080*1920,我们想要这个控件填充我们的整个屏幕可以设置下面的代码:
复制代码
package im.weiyuan.com.zidingyikongjian;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created by wei.yuan on 2017/6/2.
 */

public class MesureView extends View {
    /**
     *
    * new MesureView(context)的时候调用该构造函数
    * */
    public MesureView(Context context) {
        super(context);
    }

    /**
     * 在布局中引用
     *
     * */

    public MesureView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    /**
     * 在布局中引用
     *
     * */
    public MesureView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    /**
     * 在布局中引用
     *
     * */

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

    /**
     *
     *
     * */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        // 第一步提取出具体对象的测量模式和大小
        int size = MeasureSpec.getSize(widthMeasureSpec);
        int mode  = MeasureSpec.getMode(widthMeasureSpec);
        Log.d("123456",size+"");
        if(mode == MeasureSpec.EXACTLY){ //默认就是精确的模式
            Log.d("123456",mode+"");
        }

      //设置控制的大小
        setMeasuredDimension(1080,1920);//单位是像素

    }
}
复制代码

 

posted on   luzhouxiaoshuai  阅读(261)  评论(0编辑  收藏  举报

编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

点击右上角即可分享
微信分享提示