关于getMeasuredWidth()和getWidth()方法 10C

package com.example.customview01.view;

import java.util.HashSet;
import java.util.Random;
import java.util.Set;

import android.R.integer;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;

import com.example.customview01.R;

public class CustomTitleView extends View
{
private String mTitleText;
private int mTitleTextColor;
private int mTitleTextSize;
private Rect mBound;
private Paint mPaint;
private View customvView;

public CustomTitleView(Context context, AttributeSet attrs)
{
    this(context, attrs, 0); 
}

public CustomTitleView(Context context)
{
    this(context, null);
}
public CustomTitleView(Context context, AttributeSet attrs, int defStyle)
{
    super(context, attrs, defStyle);
    /*LayoutInflater inflater = LayoutInflater.from(context);
    customvView = inflater.inflate(R.layout.activity_main, null);*/
    TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0);
    int n = a.getIndexCount();
    for (int i = 0; i < n; i++)
    {
        int attr = a.getIndex(i);
        switch (attr)
        {
        case R.styleable.CustomTitleView_titleText:
            mTitleText = a.getString(attr);
            break;
        case R.styleable.CustomTitleView_titleTextColor:
            mTitleTextColor = a.getColor(attr, Color.BLACK);
            break;
        case R.styleable.CustomTitleView_titleTextSize:
            mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(
                    TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics()));
            break;

        }

    }
    a.recycle();

    mPaint = new Paint();
    mPaint.setTextSize(mTitleTextSize);
    // mPaint.setColor(mTitleTextColor);
    mBound = new Rect();
    mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound);

    this.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            mTitleText = randomText();
            postInvalidate();
        }

    });
    System.out.println("45678duiehjwfgiywehbWI");

}
private String randomText()
{
    Random random = new Random();
    Set<Integer> set = new HashSet<Integer>();
    while (set.size() < 4)
    {
        int randomInt = random.nextInt(10);
        set.add(randomInt);
    }
    StringBuffer sb = new StringBuffer();
    for (Integer i : set)
    {
        sb.append("" + i);
    }

    return sb.toString();
}

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

    System.out.println("widthMeasureSpec=   "+widthMeasureSpec+"   heightMeasureSpec=   "+heightMeasureSpec);

    int width = 0;
    int height = 0;

    int specMode = MeasureSpec.getMode(widthMeasureSpec);
    int specSize = MeasureSpec.getSize(widthMeasureSpec);
    System.out.println(specMode+"           ---             "+specSize); 
    switch (specMode)
    { 
    case MeasureSpec.EXACTLY:
        width = getPaddingLeft() + getPaddingRight() + specSize;
        break;
    case MeasureSpec.AT_MOST: 
        int a=getPaddingLeft();
        int b=getPaddingRight();
        int c=mBound.width();
        width = a+b+c;
        break;
    }


    specMode = MeasureSpec.getMode(heightMeasureSpec);
    specSize = MeasureSpec.getSize(heightMeasureSpec);
    switch (specMode) 
    {
    case MeasureSpec.EXACTLY:
        height = getPaddingTop() + getPaddingBottom() + specSize;
        break;
    case MeasureSpec.AT_MOST: 
        int a=getPaddingTop();
        int b=getPaddingBottom();
        int c=mBound.height();
        height = a+b+c; 
        break;
    }
    int i=0;
    setMeasuredDimension(width, height);

}

@Override
protected void onDraw(Canvas canvas)
{
    mPaint.setColor(Color.YELLOW);
    System.out.println(getMeasuredHeight()+"************************"+getMeasuredWidth());
    System.out.println(getHeight()+"   ///////////////////////////   "+getWidth());
    canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint);

    mPaint.setColor(mTitleTextColor);
    canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint);
}

}
这是一个自定义View的代码,很简单,中间利用onMeasure()对View大小进行重绘,我的问题是,在onDraw()方法里面getMeasuredWidth()和getWidth()输出的值不一样,请问这两个方法有什么区别?

3个回答

一个是大小的绝对值,一个是度量值。

qq_29951983
谢耀眼 啥意思?
接近 5 年之前 回复

getMeasuredWidth是测量后的实际宽度, getWidth是你在布局中设置的宽度

比如你有一个textview,在页面里被滑了上去,就只能看到它的一半。那么getMeasuredWidth就是它的总高度, getWidth是显示出来的你看到的高度。望采纳。

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问