谢耀眼 2015-10-06 03:08 采纳率: 12.5%
浏览 1466
已结题

关于getMeasuredWidth()和getWidth()方法

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()输出的值不一样,请问这两个方法有什么区别?

  • 写回答

2条回答 默认 最新

  • threenewbee 2015-10-06 03:34
    关注

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

    评论

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择