Android学习自定义View(五)——自定义ViewGroup及其onMeasure()的理解

简介: MainActivity如下:package cc.testviewstudy5;import android.os.Bundle;import android.
MainActivity如下:
package cc.testviewstudy5;

import android.os.Bundle;
import android.app.Activity;
/**
 * Demo描述:
 * 自定义ViewGroup及其onMeasure()的理解
 * 
 * 参考资料:
 * 1 http://blog.csdn.net/guolin_blog/article/details/16330267
 * 2 http://blog.csdn.net/dawanganban/article/details/23953827
 *   Thank you very much
 */
public class MainActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new ViewGroupSubClass(this));
	}

}

ViewGroupSubClass如下:
package cc.testviewstudy5;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
/**
 * 当继承自ViewGroup时,如果在onMeasure()对于子View不调用
 * 1 measureChild(child, parentWidthMeasureSpec, parentHeightMeasureSpec)
 * 2 childView.measure(childWidthMeasureSpec, childHeightMeasureSpec)
 * 这两个方法的任何其中一个,那么:
 * childView.getMeasuredWidth()和childView.getMeasuredHeight()得到的值均为0
 * 
 * 这是为什么呢?
 * 因为ViewGroup继承自View,View就根本没有子View.所以:
 * 在ViewGroup的onMeasure()中的super.onMeasure(widthMeasureSpec,heightMeasureSpec)
 * 只是测量该ViewGroup本身的宽和高.而没有去测量其每个子View的宽和高.
 * 于是需要我们自己写代码去测量该ViewGroup的每个子View,或者让子View自己调用measure().这么操作以后,再用
 * childView.getMeasuredWidth()和childView.getMeasuredHeight()
 * 得到的值就不再是0了.
 * 
 * 假若不继承自ViewGroup而继承自XXXLayout,那么就不是必须要自己去测量每个子View的大小了.
 * 查看XXXLayout的源代码,可以看到在其onMeasure()中已经测量了子View的大小.
 *
 */
public class ViewGroupSubClass extends ViewGroup {

	public ViewGroupSubClass(Context context) {
		super(context);
		Button button1 = new Button(context);
		button1.setText("button1");
		this.addView(button1);

		Button button2 = new Button(context);
		button2.setText("button2");
		this.addView(button2);

		Button button3 = new Button(context);
		button3.setText("button3");
		this.addView(button3);

	}

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

	public ViewGroupSubClass(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// 获取系统自动测量的该ViewGroup的大小
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		System.out.println("获取系统自动测量的该ViewGroup的大小: widthSize="+widthSize+",heightSize="+heightSize);
		// 我们也可调用setMeasuredDimension()重新设置测量结果

		// 修改了系统自动测量的子View的大小
		int childCount = this.getChildCount();
		int childMeasuredWidth = 0;
		int childMeasuredHeight = 0;
		int childWidthMeasureSpec = 0;
		int childHeightMeasureSpec = 0;
		for (int i = 0; i < childCount; i++) {
			View childView = getChildAt(i);

			// 系统自动测量子View:
			measureChild(childView, widthMeasureSpec, heightMeasureSpec);

			// 如果不希望系统自动测量子View,我们用以下的方式:
			// childWidthMeasureSpec =
			// MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);
			// childHeightMeasureSpec =
			// MeasureSpec.makeMeasureSpec(100,MeasureSpec.EXACTLY);
			// childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);
		}

		// 获取每个子View测量所得的宽和高
		for (int i = 0; i < childCount; i++) {
			View childView = getChildAt(i);
			childMeasuredWidth = childView.getMeasuredWidth();
			childMeasuredHeight = childView.getMeasuredHeight();
			System.out.println("i=" + i+ ",获取系统自动测量的该子View的大小:" +
					          "childMeasuredWidth="+ childMeasuredWidth + "," +
					          "childMeasuredHeight="+ childMeasuredHeight);
		}
	}

	@Override
	protected void onLayout(boolean arg0, int l, int t, int r, int b) {
		System.out.println("该ViewGroup的布局:"+"l="+l+",t="+t+",r="+r+",b="+b);
		int childCount = getChildCount();
		int left = 0;
		int top = 10;
		int right = 0;
		int bottom = 0;
		for (int i = 0; i < childCount; i++) {
			View childView = getChildAt(i);
			right = left + childView.getMeasuredWidth();
			// 也可不用测量所得的宽而指定一个值
			// right=left+180;
			bottom = top + childView.getMeasuredHeight();
			// 也可不用测量所得的高而指定一个值
			// bottom=top+180;
			childView.layout(left, top, right, bottom);
			System.out.println("i=" + i + ",该子View的布局:" + "" +
					          "left="+left+",top="+top+",right="+right+",bottom="+bottom);
			top += 190;
		}

	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
	}

}

main.xml如下:
<RelativeLayout 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"
     >
    
    <cc.testviewstudy5.ButtonSubClass
        android:id="@+id/button"
        android:layout_width="100dip"
        android:layout_height="200dip"
        android:text="Button"/>

</RelativeLayout>

PS:
main.mxl没有任何用处
相关文章
|
10月前
|
Android开发 UED 计算机视觉
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
本文介绍了一款受游戏“金铲铲之战”启发的Android自定义View——线条等待动画的实现过程。通过将布局分为10份,利用`onSizeChanged`测量最小长度,并借助画笔绘制动态线条,实现渐变伸缩效果。动画逻辑通过四个变量控制线条的增长与回退,最终形成流畅的等待动画。代码中详细展示了画笔初始化、线条绘制及动画更新的核心步骤,并提供完整源码供参考。此动画适用于加载场景,提升用户体验。
656 5
Android自定义view之线条等待动画(灵感来源:金铲铲之战)
|
10月前
|
XML Java Android开发
Android自定义view之网易云推荐歌单界面
本文详细介绍了如何通过自定义View实现网易云音乐推荐歌单界面的效果。首先,作者自定义了一个圆角图片控件`MellowImageView`,用于绘制圆角矩形图片。接着,通过将布局放入`HorizontalScrollView`中,实现了左右滑动功能,并使用`ViewFlipper`添加图片切换动画效果。文章提供了完整的代码示例,包括XML布局、动画文件和Java代码,最终展示了实现效果。此教程适合想了解自定义View和动画效果的开发者。
439 65
Android自定义view之网易云推荐歌单界面
|
10月前
|
XML 前端开发 Android开发
一篇文章带你走近Android自定义view
这是一篇关于Android自定义View的全面教程,涵盖从基础到进阶的知识点。文章首先讲解了自定义View的必要性及简单实现(如通过三个构造函数解决焦点问题),接着深入探讨Canvas绘图、自定义属性设置、动画实现等内容。还提供了具体案例,如跑马灯、折线图、太极图等。此外,文章详细解析了View绘制流程(measure、layout、draw)和事件分发机制。最后延伸至SurfaceView、GLSurfaceView、SVG动画等高级主题,并附带GitHub案例供实践。适合希望深入理解Android自定义View的开发者学习参考。
809 84
|
10月前
|
前端开发 Android开发 UED
讲讲Android为自定义view提供的SurfaceView
本文详细介绍了Android中自定义View时使用SurfaceView的必要性和实现方式。首先分析了在复杂绘制逻辑和高频界面更新场景下,传统View可能引发卡顿的问题,进而引出SurfaceView作为解决方案。文章通过Android官方Demo展示了SurfaceView的基本用法,包括实现`SurfaceHolder.Callback2`接口、与Activity生命周期绑定、子线程中使用`lockCanvas()`和`unlockCanvasAndPost()`方法完成绘图操作。
292 3
|
Android开发
Android 自定义View 测量控件宽高、自定义viewgroup测量
Android 自定义View 测量控件宽高、自定义viewgroup测量
797 0
|
XML Android开发 数据格式
Android 中自定义ViewGroup实现流式布局的效果
Android 中自定义ViewGroup实现流式布局的效果
237 0
|
XML 前端开发 Android开发
Android自定义View-入门(明白自定义View和自定义ViewGroup)
为什么要自定义View? 主要是Andorid系统内置的View 无法实现我们的 需求,我们需要针对我们的业务需求定制我们想要的 View.
223 0
Android自定义View-入门(明白自定义View和自定义ViewGroup)
|
XML Android开发 数据格式
android自定义View&自定义ViewGroup(下)
本篇来看看自定义ViewGroup
282 0
|
XML 前端开发 Android开发
android自定义View&自定义ViewGroup(上)
自定义View&自定义ViewGroup
243 0
|
XML Android开发 数据格式
Android自定义控件(十一)——自定义ViewGroup实现LinearLayout
Android自定义控件(十一)——自定义ViewGroup实现LinearLayout
694 0