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);//单位是像素 }
}
android自定义控件onMeasure方法的更多相关文章
- [转]Android View.onMeasure方法的理解
转自:http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html Android View.onMeasure方法的理解 View在屏幕上显示出来要先经过 ...
- android自定义控件 onMeasure() 测量尺寸
上次讲的自定义控件刷新点屏幕的任意地方都会刷新,而且在xml里自定义控件下面放一个textview的话,这个TextView是显示不出来的,不只这个,以前的几个自定义控件都是 为什么呢?今天来讲下on ...
- Android的onMeasure方法
在Android开发中,当Android原生控件不能满足我们的需求的时候,就需要自定义View.View在屏幕上绘制出来先要经过measure(计算)和layout(布局). 什么时候调用onMeas ...
- Android View.onMeasure方法的理解
View在屏幕上显示出来要先经过measure(计算)和layout(布局).1.什么时候调用onMeasure方法? 当控件的父元素正要放置该控件时调用.父元素会问子控件一个问题,“你想要用多大地方 ...
- [转载]Android View.onMeasure方法的理解
2013-12-18 10:56:28 转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure( ...
- Android View.onMeasure方法的理解(转载)
一下内容转载自http://blog.sina.com.cn/s/blog_61fbf8d10100zzoy.html View在屏幕上显示出来要先经过measure(计算)和layout(布局).1 ...
- android自定义控件onLayout方法
onLayout设置子控件的位置,对应一些普通的控件例如Button.TextView等控件,不存在子控件,所以可以不用复写该方法. 向线性布局.相对布局等存在子控件,可以覆写该方法去控制子控件的位置 ...
- android自定义控件(6)-详解在onMeasure()方法中如何测量一个控件尺寸
今天的任务就是详细研究一下protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)方法.如果只是说要重写什么方法有什么 ...
- Android 自定义控件高度设置onMeasure方法
最近使用hellocharts需要表格横向显示,而activity需要竖屏显示,在旋转以后,默认宽度为不超过屏幕宽度,则一直无法显示全控件. 此时需要修改onMeasure方法,这个方法是用来控制控件 ...
随机推荐
- java类的方法的使用
类的方法:提供某种功能的实现: 实例:public void eat (){ } public String getName(){ } public void setName(String n){ ...
- 【图机器学习】cs224w Lecture 13 & 14 - 影响力最大化 & 爆发检测
目录 Influence Maximization Propagation Models Linear Threshold Model Independent Cascade Model Greedy ...
- python通用数据库操作工具 pydbclib
pydbclib是一个通用的python关系型数据库操作工具包,使用统一的接口操作各种关系型数据库(如 oracle.mysql.postgres.hive.impala等)进行增删改查,它是对各个p ...
- Jupyternotebook添加c++核心支持的配置过程
一.环境:虚拟机:(1)系统:centos7.5_1804(64bit)版本(2)软件环境:git.python3.5.3.Jupyter4.4.0二.下载安装脚本:资源及安装说明:https://g ...
- Docker容器同步主机时间
方法一: 查看本地是否有/etc/localtime文件 cat /etc/localtime 如果没有就新建文件 cp /usr/share/zoneinfo/Asia/Shanghai /et ...
- No grammar constraints (DTD or XML Schema) referenced in the document.的两种解决办法
方法一:常用方法 关闭XML验证 工具栏:windows => preferences => xml => xml files => validation => Indi ...
- 看不见远程新建git分支
再网页上新建了一个git分支.然后在本地跑git branch -r(查看远程分支)/ git branch -a(查看所有分支)两个命令,都没有看到新建的那个分支.这是为啥呢??? 原因是因为:gi ...
- Java实现埃拉托色尼筛选法
1 问题描述 Compute the Greatest Common Divisor of Two Integers using Sieve of Eratosthenes. 翻译:使用埃拉托色尼筛选 ...
- 性能测试中TPS上不去的原因
TPS(Transaction Per Second):每秒事务数,指服务器在单位时间内(秒)可以处理的事务数量,一般以request/second为单位. 压测中为什么TPS上不去的原因: .网络带 ...
- 09_EM算法
今天是2020年3月5日星期四.预计开学时间不会早于四月初,真是好消息,可以有大把的时间整理知识点(实际上发文章的时间都6月6号了,希望9月份能开学啊,不耽误找工作~).每次导师找,整个人会变的特别烦 ...