android高级ui01-自定义view-

学习笔记,整理中

摘要

1、layoutparams:xml的属性转换为java用的格式

2、面必问:measurespec

------------------------------------

主要讲 自定义viewgroup,viewpager  

1、所有的xml解析 都在 layoutinflater里面

2、自定义view至少3个构造函数,java new view调用;xml 反射调用;不同主题调用;自定义属性用(这个很少用)

4、source insite

5、jianshu.com/p/0723ff4123e1 flexboxlayout 流式布局

----------------------------------

实现UI: 自定义VIew

java 语言

自定义View android 基础

自定义View 包含什么?

布局: onlayout onmeausre/ Layout:viewGroup

显示: onDraw          :view: canvas paint matrix clip rect animation path(贝塞尔) line  text绘制

      frameWork:

交互: onTouchEvent    :组合的viewGroup

大量的实战,实践/

jett:xml解析原理,view层次结构,插件化换肤

2000平

序列化

自定义序列化: IOT

协议

物联网:蓝牙:传递的数据 串口 协议:

自己  ;viewPager

都可以:算法

先孩子再自己

android:layout_width="wrap_content"  -》xxx dp  dip

                android:layout_height="wrap_content"

很多知识:算法

MeasureSpec :

size 值

  0~10000

  int 32:

  30



int  11000000000000000000000

mode:高两位:UNSPECIFIED, EXACTLY, AT_MOST

01 00 11

算法是怎样的?

SDK一个包:严谨性,可拓展性

内容







----------------------------------------------------------------------------------------------------------------------------------------------------------------------------

public class FlowLayoutextends ViewGroup {

private static final StringTAG ="FlowLayout";

    private int mHorizontalSpacing =dp2px(16); //每个item横向间距

    private int mVerticalSpacing =dp2px(8); //每个item横向间距

    private List>allLines =new ArrayList<>(); // 记录所有的行,一行一行的存储,用于layout

    ListlineHeights =new ArrayList<>(); // 记录每一行的行高,用于layout

    public FlowLayout(Context context) {

super(context);

//        initMeasureParams();

    }

//反射

    public FlowLayout(Context context, AttributeSet attrs) {

super(context, attrs);

//        initMeasureParams();

    }

//主题style

    public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

//        initMeasureParams();

    }

//四个参数 自定义属性

    private void clearMeasureParams() {

allLines.clear();

        lineHeights.clear();

    }

//度量

    @Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

clearMeasureParams();//内存 抖动

        //先度量孩子

        int childCount = getChildCount();

        int paddingLeft = getPaddingLeft();

        int paddingRight = getPaddingRight();

        int paddingTop = getPaddingTop();

        int paddingBottom = getPaddingBottom();

        int selfWidth = MeasureSpec.getSize(widthMeasureSpec);  //ViewGroup解析的父亲给我的宽度

        int selfHeight = MeasureSpec.getSize(heightMeasureSpec); // ViewGroup解析的父亲给我的高度

        List lineViews =new ArrayList<>(); //保存一行中的所有的view

        int lineWidthUsed =0; //记录这行已经使用了多宽的size

        int lineHeight =0; // 一行的行高

        int parentNeededWidth =0;  // measure过程中,子View要求的父ViewGroup的宽

        int parentNeededHeight =0; // measure过程中,子View要求的父ViewGroup的高

        for (int i =0; i < childCount; i++) {

View childView = getChildAt(i);

            LayoutParams childLP = childView.getLayoutParams();

            if (childView.getVisibility() != View.GONE) {

//将layoutParams转变成为measureSpec

                int childWidthMeasureSpec = getChildMeasureSpec(widthMeasureSpec, paddingLeft + paddingRight,

                        childLP.width);

                int childHeightMeasureSpec = getChildMeasureSpec(heightMeasureSpec, paddingTop + paddingBottom,

                        childLP.height);

                childView.measure(childWidthMeasureSpec, childHeightMeasureSpec);

                //获取子view的度量宽高

                int childMesauredWidth = childView.getMeasuredWidth();

                int childMeasuredHeight = childView.getMeasuredHeight();

                //如果需要换行

                if (childMesauredWidth + lineWidthUsed +mHorizontalSpacing > selfWidth) {

//一旦换行,我们就可以判断当前行需要的宽和高了,所以此时要记录下来

                    allLines.add(lineViews);

                    lineHeights.add(lineHeight);

                    parentNeededHeight = parentNeededHeight + lineHeight +mVerticalSpacing;

                    parentNeededWidth = Math.max(parentNeededWidth, lineWidthUsed +mHorizontalSpacing);

                    lineViews =new ArrayList<>();

                    lineWidthUsed =0;

                    lineHeight =0;

                }

// view 是分行layout的,所以要记录每一行有哪些view,这样可以方便layout布局

                lineViews.add(childView);

                //每行都会有自己的宽和高

                lineWidthUsed = lineWidthUsed + childMesauredWidth +mHorizontalSpacing;

                lineHeight = Math.max(lineHeight, childMeasuredHeight);

                //处理最后一行数据

                if (i == childCount -1) {

allLines.add(lineViews);

                    lineHeights.add(lineHeight);

                    parentNeededHeight = parentNeededHeight + lineHeight +mVerticalSpacing;

                    parentNeededWidth = Math.max(parentNeededWidth, lineWidthUsed +mHorizontalSpacing);

                }

}

}

//再度量自己,保存

        //根据子View的度量结果,来重新度量自己ViewGroup

        // 作为一个ViewGroup,它自己也是一个View,它的大小也需要根据它的父亲给它提供的宽高来度量

        int widthMode = MeasureSpec.getMode(widthMeasureSpec);

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);

        int realWidth = (widthMode == MeasureSpec.EXACTLY) ? selfWidth: parentNeededWidth;

        int realHeight = (heightMode == MeasureSpec.EXACTLY) ?selfHeight: parentNeededHeight;

        setMeasuredDimension(realWidth, realHeight);

    }

//布局

    @Override

protected void onLayout(boolean changed, int l, int t, int r, int b) {

int lineCount =allLines.size();

        int curL = getPaddingLeft();

        int curT = getPaddingTop();

        for (int i =0; i < lineCount; i++){

List lineViews =allLines.get(i);

            int lineHeight =lineHeights.get(i);

            for (int j =0; j < lineViews.size(); j++){

View view = lineViews.get(j);

                int left = curL;

                int top =  curT;

//                int right = left + view.getWidth();

//                int bottom = top + view.getHeight();

                int right = left + view.getMeasuredWidth();

                int bottom = top + view.getMeasuredHeight();

                view.layout(left,top,right,bottom);

                curL = right +mHorizontalSpacing;

            }

curT = curT + lineHeight +mVerticalSpacing;

            curL = getPaddingLeft();

        }

}

//    @Override

//    protected void onDraw(Canvas canvas) {

//        super.onDraw(canvas);

//    }

    public static int dp2px(int dp) {

return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, Resources.getSystem().getDisplayMetrics());

    }

}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 228,156评论 6 531
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 98,401评论 3 415
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 176,069评论 0 373
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 62,873评论 1 309
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 71,635评论 6 408
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 55,128评论 1 323
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 43,203评论 3 441
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 42,365评论 0 288
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 48,881评论 1 334
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 40,733评论 3 354
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 42,935评论 1 369
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 38,475评论 5 358
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 44,172评论 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 34,582评论 0 26
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 35,821评论 1 282
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 51,595评论 3 390
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 47,908评论 2 372

推荐阅读更多精彩内容