MainActivity如下:
package cc.testviewstudy2;
import android.os.Bundle;
import android.app.Activity;
/**
* Demo描述:
* 关于自定义View的学习(二)
*
* View的绘制流程:onMeasure()-->onLayout()-->onDraw()
*
* 学习资料:
* http://blog.csdn.net/guolin_blog/article/details/16330267
* Thank you very much
*
*/
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
LinearLayoutTest如下:
package cc.testviewstudy2;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
/**
* 注意:
* 1 继承自XXXXLayout不要继承自ViewGroup
* 假如继承自ViewGroup那么xml的cc.testviewstudy2.ViewGroupLayout中
* 设置layout_width和layout_height不论是wrap_content还是fill_parent
* 这个父视图总是充满屏幕的.
* 现象是如此,原因暂不明.
*
* 2 在本LinearLayoutTest我们只放入一个子View作为测试
*/
public class LinearLayoutTest extends LinearLayout {
private Paint mPaint;
public LinearLayoutTest(Context context) {
super(context);
}
public LinearLayoutTest(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (getChildCount()>0) {
View childView=getChildAt(0);
//这里的widthMeasureSpec和heightMeasureSpec都是父视图的
//这两个值都由Size和Mode组成,所以要是如下输出:
//System.out.println("widthMeasureSpec="+widthMeasureSpec+",heightMeasureSpec="+heightMeasureSpec);
//会得到两个很大的值.这个值就是Size和Mode的组合.
//所以我们应该按照下面的方式来
//分别获取widthMeasureSpec和heightMeasureSpec的Size和Mode
int widthSize=MeasureSpec.getSize(widthMeasureSpec);
int widthMode=MeasureSpec.getMode(widthMeasureSpec);
int heightSize=MeasureSpec.getSize(heightMeasureSpec);
int heightMode=MeasureSpec.getMode(heightMeasureSpec);
System.out.println("父视图widthSize="+widthSize+",父视图widthMode="+widthMode+
",父视图heightSize="+heightSize+",父视图heightMode="+heightMode);
//测量子View
measureChild(childView, widthMeasureSpec, heightMeasureSpec);
}
}
@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
if (getChildCount()>0) {
View childView=getChildAt(0);
System.out.println("子视图childView.getMeasuredWidth()="+childView.getMeasuredWidth()+
",子视图childView.getMeasuredHeight()="+ childView.getMeasuredHeight());
//摆放子View
childView.layout(0, 0, childView.getMeasuredWidth(), childView.getMeasuredHeight());
//X和Y均平移50
//在此犯错写成了:childView.layout(50, 50, childView.getMeasuredWidth(), childView.getMeasuredHeight());
//导致图片显示出来很小,正确的方式如下:
//childView.layout(50, 50, childView.getMeasuredWidth()+50, childView.getMeasuredHeight()+50);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mPaint = new Paint();
mPaint.setColor(Color.YELLOW);
mPaint.setTextSize(20);
String content = "Hello World";
canvas.drawText(content, 150, 100, mPaint);
}
}
main.xml如下:
<cc.testviewstudy2.LinearLayoutTest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff0033"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
</cc.testviewstudy2.LinearLayoutTest>原文:http://blog.csdn.net/lfdfhl/article/details/18913421