canvas教程

13问13答全面学习Android View绘制(4)

字号+ 作者:H5之家 来源:H5之家 2016-05-07 17:00 我要评论( )

package com.example.administrator.motioneventtest;import android.content.Context;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.view.ViewGroup;/** *

package com.example.administrator.motioneventtest; import android.content.Context; import android.util.AttributeSet; import android.util.Log; import android.view.View; import android.view.ViewGroup; /** * Created by Administrator on 2016/2/4. */ //这里我们只处理了padding的状态 没有处理margin的状态,子view的margin 对measure和layout的影响 //就留给读者自己完成了 public class CustomHorizontalLayout extends ViewGroup { //设置默认的控件最小是多少 这里不提供自定义属性了 写死在代码里 你们可以自行拓展 final int minHeight = 0; final int minWidth = 0; public CustomHorizontalLayout(Context context) { super(context); } public CustomHorizontalLayout(Context context, AttributeSet attrs) { super(context, attrs); } public CustomHorizontalLayout(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int measureWidth = 0; int measureHeight = 0; final int childCount = getChildCount(); measureChildren(widthMeasureSpec, heightMeasureSpec); int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec); int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec); int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec); int heightSpecSize = MeasureSpec.getSize(heightMeasureSpec); final View childView = getChildAt(0); final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); final int paddingTop = getPaddingTop(); final int paddingBottom = getPaddingBottom(); //没有子控件 时 我们的宽高要作特殊处理 if (childCount == 0) { //当没有子控件时,如果长宽有一个为wrap 那么就让这个控件以最小的形式展现 //这里我们最小设置为0 if (widthSpecMode == MeasureSpec.AT_MOST || heightSpecMode == MeasureSpec.AT_MOST) { setMeasuredDimension(minWidth, minHeight); } else { //否则根据我们的layout属性来 setMeasuredDimension(getLayoutParams().width, getLayoutParams().height); } } else if (widthSpecMode == MeasureSpec.AT_MOST && heightSpecMode == MeasureSpec.AT_MOST) { measureWidth = childView.getMeasuredWidth() * childCount; measureHeight = childView.getMeasuredHeight(); setMeasuredDimension(paddingLeft + measureWidth + paddingRight, paddingTop + measureHeight + paddingBottom); } else if (heightSpecMode == MeasureSpec.AT_MOST) { measureHeight = childView.getMeasuredHeight(); setMeasuredDimension(paddingLeft + paddingRight + widthSpecSize, paddingTop + paddingBottom + measureHeight); } else if (widthSpecMode == MeasureSpec.AT_MOST) { measureWidth = childView.getMeasuredWidth() * childCount; setMeasuredDimension(paddingLeft + paddingRight + measureWidth, paddingTop + paddingBottom + heightSpecSize); } } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { final int paddingLeft = getPaddingLeft(); final int paddingRight = getPaddingRight(); final int paddingTop = getPaddingTop(); final int paddingBottom = getPaddingBottom(); //左边初始位置为0 int childLeft = 0 + paddingLeft; final int childCount = getChildCount(); for (int i = 0; i < childCount; i++) { final View childView = getChildAt(i); if (childView.getVisibility() != View.GONE) { final int childWidth = childView.getMeasuredWidth(); childView.layout(childLeft, 0 + paddingTop, childLeft + childWidth, paddingTop + childView.getMeasuredHeight()); childLeft += childWidth; } } } }

以上就是本文的全部内容,希望对大家的学习有所帮助。

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • Android游戏开发学习②焰火绽放效果实现方法

    Android游戏开发学习②焰火绽放效果实现方法

    2016-05-08 10:00

  • Android动画之渐变动画(Tween Animation)详解 (渐变、缩放、位移

    Android动画之渐变动画(Tween Animation)详解 (渐变、缩放、位移

    2016-04-30 14:00

网友点评
<