首页 技术 正文
技术 2022年11月18日
0 收藏 928 点赞 2,869 浏览 2131 个字

最近在学习自定义view  一遍看一别学顺便记录一下

1.View的测量——–>onMeasure()

首先,当我们要画一个图形的时候,必须知道三个数据:位置,长度,宽度   才能确定应该在哪个位置画多大的图形。这就是onMeasure方法存在的意义

测量的模式有三种

1.EXACTLY:             精确模式                    当我们把布局的宽度高度设为指定数值,或者match_parent时系统指定的是这种模式

2.AT MOST:            最大值模式                  当我们将控件的宽高设为warp_content时,会跟随子控件的大小而变化,系统指定的是这种模式

3.UNSPECIFIED:      不指定大小测量模式       想多大就多大, 一般用于自定义view

【view 默认的模式是EXACTLY,当自定义view时,要是不重写onMeasure()方法,控件只会响应设置的具体的宽高或者march_parent,要想使用warp_content,就必须重写onMeasure(),指定warp_content大小】

2.andoird提供了一个类用来测量view————>MeasureSpec

     首先:从MeasureSpec对象中获取具体的测量模式和大小:

int mode = MeasureSpec.getMode(heightMeasureSpec);

int size = MeasureSpec.getSize(heightMeasureSpec);

然后:通过判断测量模式给出不同的测量值:

当mode为EXACTLY时使用制定的size即可,

当为其他两种模式的时候需要给它一个默认值,如果指定warp_content属性时,则需要取出我们指定的大小与size中最小的一个来作为测量值

demo:

if(mode == MeasureSpec.EXACTLY){
            height = size;
        }else{
            height = 400;
            if(mode == MeasureSpec.AT_MOST){
                height = Math.min(height,size);
            }
        }

举例说明:

1.自定义一个view

public class TouchView extends View {

public TouchView(Context context) {super(context);}

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

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

@Override
           protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { setMeasuredDimension(measureWidth(widthMeasureSpec),measureHeight(heightMeasureSpec));}

private int measureHeight(int heightMeasureSpec) {

int height = 0;

int mode = MeasureSpec.getMode(heightMeasureSpec);

int size = MeasureSpec.getSize(heightMeasureSpec);

if(mode == MeasureSpec.EXACTLY){  height = size;

}else{  height = 400;

if(mode == MeasureSpec.AT_MOST){ height = Math.min(height,size); }
             }
                  return height;
           }

private int measureWidth(int widthMeasureSpec) {

int widith = 0;

int mode = MeasureSpec.getMode(widthMeasureSpec);

int size = MeasureSpec.getSize(widthMeasureSpec);

if(mode == MeasureSpec.EXACTLY){widith = size; }else{  widith = 200;

if(mode == MeasureSpec.AT_MOST){ widith = Math.min(widith,size);}

return widith;         
    }
}

2.在xml中使用:

<com.view.TouchView
        android:layout_height=”wrap_content”
        android:background=”#f00″
        android:layout_width=”wrap_content”
        />

【可以尝试变换高和宽的属性值来感受下具体的不同】

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898