No1:

View的滑动

1)layout()方法的

public class CustomView extends View{
private int lastX;
private int lastY;
public CustomView(Context context,AttributeSet attrs,int defStyleAttr){
super(context,attrs,defStyleAttr);
}
public CustomView(Context context,AttributeSet attrs){
super(context,attrs);
}
public CustomView(Context context){
super(context);
} public boolean onTouchEvent(MotionEvent event){
//获取手指摸点的横坐标和纵坐标
int x = (int)event.getX();
int y = (int)event.getY(); switch(event.getAction()){
case MotionEvent.ACTION_DOWN:
lastX = x;
lastY = y;
break;
case MotionEvent.ACTION_MOVE:
//计算移动的距离
int offsetX = x - lastX;
int offsetY = y - lastY;
//调用layout方法来重新放置它的位置
layout(getLeft()+offsetX,getTop()+offsetY,getRight()+offsetX,getBottom()+offsetY);
break;
}
return true;
}
}

2)offsetLeftAndRight()与offsetTopAndBottom()

对上面代码进行修改

case MotionEvent.ACTION_MOVE:
//计算移动的距离
int offsetX = x - lastX;
int offsetY = y - lastY;
//对left和right进行偏移
offsetLeftAndRight(offsetX);
//对top和bottom进行偏移
offsetTopAndBottom(offsetY);
break;

3)LayoutParams(改变布局参数)

同样对上面代码进行修改

case MotionEvent.ACTION_MOVE:
ViewGroup.MarginLayoutParams layoutParams = (ViewGroup.MarginLayoutParams)getLayoutParams();
layoutParams.leftMargin = getLeft() + offsetX;
layoutParams.topMargin = getTop() + offsetY;
setLayoutParams(layoutParams);
break;

4)动画

5)scrollTo与scrollBy

scrollTo(x,y)表示移动到一个具体的坐标点,而scrollBy(x,y)表示移动的增量为dx、dy。其中scrollBy最终也是要调用scrollTo的。

View.java的scrollBy和scrollTo源码

public void scrollTo(int x,int y){
if(mScrollX!=x || mScrollY!=y){
int oldX = mScrollX;
int oldY = mScrollY;
mScrollX = x;
mScrollY = y;
invalidateParentCaches();
onScrollChanged(mScrollX,mScrollY,oldX,oldY);
if(!awakenScrollBars()){
postInvalidateOnAnimation();
}
}
} public void scrollBy(int x,int y){
scrollTo(mScrollX+x,mScrollY+y);
}

6)Scroller

public CustomView(Context context,AttributeSet attrs){
super(context,attrs);
mScroller = new Scroller(context);
} @Override
public void computeScroll(){
super.computeScroll();
if(mScroller.computeScrollOffset()){
((View)getParent()).scrollTo(mScroller.getCurrX(),mScroller.getCurrY());
invalidate();
}
} public void smoothScrollTo(int destX,int destY){
int scrollX = getScrollX();
int delta = destX-scrollX;
mScroller.startScroll(scrollX,0,delta,0,2000);
invalidate();
} //最后调用
mCustomView.smoothScrollTo(-400,0);

No2:

View的measure流程,ViewGroup中定义了measureChildren方法

View和ViewGroup中均没有实现onLayout方法

No3:

View的draw流程

1)绘制背景:View.drawBackground()

2)绘制View的内容:重写View.onDraw()

3)绘制子View:ViewGroup.dispatchDraw()对子View进行遍历->ViewGroup.drawChild()->View.draw()

4)绘制装饰:View.onDrawForeground()

No4:

自定义View

1)继承系统控件的自定义View:重写onDraw()

2)继承View的自定义View:重写onDraw()、考虑warp_content属性以及padding属性设置、或者自定义属性、考虑是否重写onTouchEvent()

3)自定义组合控件

4)自定义ViewGroup:重写onLayout()、处理warp_content属性、处理滑动冲突、弹性滑动到其他页面、快速滑动到其他页面、再次触摸屏幕阻止页面继续滑动

《Android进阶之光》--View体系与自定义View的更多相关文章

  1. Android查缺补漏(View篇)--自定义 View 的基本流程

    View是Android很重要的一部分,常用的View有Button.TextView.EditView.ListView.GridView.各种layout等等,开发者通过对这些View的各种组合以 ...

  2. Android绘图机制(三)——自定义View的实现方式以及半弧圆新控件

    Android绘图机制(三)--自定义View的三种实现方式以及实战项目操作 在Android绘图机制(一)--自定义View的基础属性和方法 里说过,实现自定义View有三种方式,分别是 1.对现有 ...

  3. Android绘图机制(二)——自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和图片的坐标讲解

    Android绘图机制(二)--自定义View绘制形, 圆形, 三角形, 扇形, 椭圆, 曲线,文字和图片的坐标讲解 我们要想画好一些炫酷的View,首先我们得知道怎么去画一些基础的图案,比如矩形,圆 ...

  4. Android绘图机制(一)——自定义View的基础属性和方法

    Android绘图机制(一)--自定义View的基础属性和方法 自定义View看起来,确实看起来高深莫测,很多Android开发都不是特别在行这一块,这里面的逻辑以及一些绘画都是有一点难的,说一下我目 ...

  5. Android view相关与自定义View

    一.关于view的机制的问答 1.gesturedetector和ontouchevent的区别 gesturedetector指的是手势检测器,根据动态手势的运动特性,提出了速率边沿检测算法来分割手 ...

  6. Android开发之制作圆形头像自定义View,直接引用工具类,加快开发速度。带有源代码学习

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing ...

  7. [置顶] 曙光到来,我的新书《Android进阶之光》已出版

    独立博客版本请点击这里 由来 2016年我开始建立了自己的知识体系,所有的文章都是围绕着这个体系来写,随着这个体系的慢慢成长,开始有很多出版社联系我写书,因为比较看好电子工业出版社,就顺理成章的开始了 ...

  8. 《Android进阶之光》--Material Design

    接上篇<Android进阶之光>--Android新特性 No1: 组件: 1)底部工作条-Bottom Sheets 2)卡片-Cards 3)提示框-Dialogs 4)菜单-Menu ...

  9. 《Android进阶之光》--注解与依赖注入框架

    No1: 标准注解: 1)@Override:覆写 2)@Deprecated:过时 3)@SuppressWarnings:取消警告 4)@SafeVarargs:申明使用了可变长度参数的方法 No ...

随机推荐

  1. C语言入门教程-(6)运算符

    1.运算符概述 运算符是一种编译器执行特定的数学或逻辑操作的符号.C语言提供了以下类型的运算符: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 条件运算符 其他运算符 2.算术运算符 算术 ...

  2. JavaScript之对原生JavaScript对象及其原型扩展初探

    Object对象: //扩展:对JavaScript原生对象的扩展 //原理:原型对象 Object.prototype.keys = function(){ var keys = []; for(v ...

  3. ServiceMesh了解一下

    http://www.servicemesh.cn/?/article/70 https://zhuanlan.zhihu.com/p/33196550

  4. 虚拟树研究-CheckBox初步判断只能在第一列

    //虚拟树研究-CheckBox初步判断只能在第一列 procedure TWindowsXPForm.XPTreeInitNode(Sender: TBaseVirtualTree; ParentN ...

  5. TypeError: 'range' object does not support item assignment

    TypeError: 'range' object does not support item assignment I was looking at some python 2.x code and ...

  6. dblink 退出 session

    以dblink的表现为例,我一直认为dblink的远程连接session仅在操作(select,dml)发生时短期存在,在操作完成后依据一定条件保留或退出. 而事实并非如此,随便使用一个远程查询语句如 ...

  7. spring boot 中的热部署

    <plugin>    <groupId>org.springframework.boot</groupId>    <artifactId>sprin ...

  8. 使用 ButterKnife 操作无效(不报错、点击后没效果)------代码编写错误

    错误写法 ButterKnife.bind(this, inflater.inflate(R.layout.buju, container, false)); return inflater.infl ...

  9. 输入一个数,求1到他 的和(for循环)

  10. IE6下select被这罩住

    在我们做弹出遮罩层时经常遇到这种问题,就是select被这罩住不兼容IE6,其实解决这种问题并不难,只要掌握住原理就挺简单的. 首先就是当遮罩层出现时select要暂时隐藏,但是不能用display: ...