自定义控件(View) (B)

一、“半自动”自定义view。这种方法比较简单,可以满足大部分需求。

核心代码

 LayoutInflater.from(context).inflate(R.layout.me_recyclerview, this, true);

重点是方法inflate的第二个参数,把自定义的布局添加到父类中

LayoutInflater的from()方法可以构造出一个LayoutInflater对象,然后调用inflate()方法就可以动态加载一个布局文件,inflate()方法接收两个参宿,第一个参数是要加载的布局文件的id,这里我们传入R.layout.title,第二个参数是给加载好的布局再添加一个父布局,这里直接传入this

// 继承 LinearLayout ,通过LayoutInflater.from(context).inflate 第二个参数,
// 把自定义的布局加入到 LinearLayout中
// 也可以通过addView,把自定义的view 加入到父类
public class MeRecyclerView extends LinearLayout  {
   ...
    public MeRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }
    ...
    private void init(Context context) {
        this.mContext = context;
        LayoutInflater.from(context).inflate(R.layout.me_recyclerview, this, true);
        ...
    }
}

例子:CloudEyes_DG:com.xxx.DaniuPlayView 和 com.xxx.StandardViewController 

“半自动”自定义view 两种方法,1、动态增加布局。2,通过layout增加布局。

(1)先继承一个view类,无论哪种都要实现下面的方法,类view的三个构造方法:

extends FrameLayout //(一个view类)
    
    public StandardViewController(@NonNull Context context) {
        this(context, null);
    }

    public StandardViewController(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public StandardViewController(@NonNull Context context, @Nullable AttributeSet attrs, @AttrRes int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

(2.1)动态增加布局 com.xxx.DaniuPlayView 

com.xxx.DaniuPlayView

/**
     * 设置控制器
     */
    public void setVideoController(@Nullable BaseViewController mediaController) {
        mPlayerContainer.removeView(mVideoController);
        mVideoController = mediaController;
        if (mediaController != null) {
            mediaController.setMediaPlayer(this);
            LayoutParams params = new LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT);
            mPlayerContainer.addView(mVideoController, params);
        }
    }

(2.2)通过layout增加布局  com.xxx.StandardViewController 

 mControllerView = LayoutInflater.from(getContext()).inflate(R.layout.layout_standard_controller, this);
        mFullScreenButton = mControllerView.findViewById(R.id.fullscreen);
        mMultiRate= mControllerView.findViewById(R.id.tv_multi_rate);
        mMultiRate.setVisibility(View.GONE);
        mFullScreenButton.setOnClickListener(this);
        mMultiRate.setOnClickListener(this);

二、完全手动自定义view,核心是学会使用系统提供的工具集,把view “画” 出来。

1、继承 view 就可以拿到工具集,常用的工具有画布Canvas和画笔Paint

2、把你想要的东西画出来 ,onDraw。

3、了解View框架,这样可以更好地使用自定义view。

 View框架的工作流程为:测量每个View大小(measure)-->把每个View放置到相应的位置(layout)-->绘制每个View(draw)

4、入门核心看两文章:

(1)自定义控件其实很简单1/12  。这文章是AigeStudio系列文章的首章。一步一步教你学习自定义view

(2)Android自定义View之Canvas。入门级实战例子,教你怎样用画布Canvas和画笔Paint画一个VIEW,看完这个后,会有一个大概的认识。再接着看 AigeStudio的一系列文章。

(3)canvas之save、restore、restoreToCount详解 ,入门级例子

------------------------------------------------------------------------、

一、学习自定义view

作者:转载请注明:From AigeStudio(http://blog.csdn.net/aigestudio)Power by Aige 侵权必究!

1、自定义控件其实很简单1/12  主要内容:自定义控件的入门,简单介绍画布Canvas和画笔Paint

开篇,这是一个系列的文章,每个文章最后,有下一篇文章的入口

两个常用方法// 重绘 invalidate (重绘其实就是刷新UI)和//跳转到主线程 重绘 postInvalidate

这文章解释了为什么自定义view一般要加上三个调用父类的构造方法,例如:

public PinnedHeaderRecyclerView(Context context) {
	super(context);
}

public PinnedHeaderRecyclerView(Context context, @Nullable AttributeSet attrs) {
	super(context, attrs);
}

public PinnedHeaderRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
	super(context, attrs, defStyle);
}

文章备份 自定义控件 1 (入门)

2、自定义控件其实很简单1/6 主要内容:详细介绍画布Canvas和画笔Paint

重点:Xfermode和ColorFilter

3、自定义控件其实很简单1/4

Android中和字体相关的很重要的类

4、自定义控件其实很简单1/3 。主要内容:Paint方法学习还略带地说了下Matri的简单用法

Matrix ,里面有很多关于动画,图形处理,坐标系的知识。

5、自定义控件其实很简单5/12 。主要内容 :Canvas

6、自定义控件其实很简单7/12 。主要内容:重点是控件的测量

未完...

二、基础知识 :

1. Android View框架的measure(测量)机制。文章备份:备份

摘要:

 1.1 Android中View框架的工作机制中,主要有三个过程:

                1.1.1 View树的测量(measure)Android View框架的measure机制

                1.1.2 View树的布局(layout) Android View框架的layout机制

                1.1.3 View树的绘制(draw)Android View框架的draw机制

   View框架的工作流程为:测量每个View大小(measure)-->把每个View放置到相应的位置(layout)-->绘制每个View(draw)。

  1.2  整棵View树的根节点是DecorView,它是一个FrameLayout,所以它是一个ViewGroup,所以整棵View树的测量是从一个ViewGroup对象的measure方法开始的。实践文章:DecorView是window中的最顶层view (可实现全屏)

  1.3 MeasureSpec类。该类贯穿于整个measure过程,用来传递父布局对子View尺寸测量的约束信息

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

零散的与view相关的文章

1、RecyclerView分组悬浮列表(在RecyclerView上添加固定的view)。涉及到自定义view的绘制,测量

2、改变View的大小,layout方法和LayoutParams方法的差异

3、Android画布剪裁函数clipRect详解

4、Android中View自定义XML属性详解以及R.attr与R.styleable的区别

5、Toolbar的详细介绍和自定义Toolbar

6、MeasureUtil 测量工具类

小知识点:

Paint的measureText()方法取得字符串显示的宽度值

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值