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. JavaScript之对原生JavaScript对象及其原型扩展初探

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

  2. [C++]线性链表之顺序表<二>

    /*   @content 线性链表之顺序表   @date 2017-3-21 1:06   @author Johnny Zen  */ /* 线性表     顺序表     链式表[带头指针/不 ...

  3. luogu P1593 因子和

    不要吐槽博主总做这些数论氵题 首先我们看到这种因数问题,果断质因数分解 所以当前数\(a=p_1^{k_1}*p_2^{k_2}...*p_m^{k_m}\) 可得\(a^b=p_1^{k_1*b}* ...

  4. “微信小程序商城构建全栈应用”开发小记

    注意事项: 1.application\api\extra下的wx.php记得填写小程序的app_id.app_secret: 2.API测试小工具需要APPID:

  5. 第18月第22天 机器学习first

    1.网易公开课 机器学习   http://open.163.com/special/opencourse/machinelearning.html https://github.com/search ...

  6. jQuery - ajaxUpLoad.js

    ajaxFileUpload是一个异步上传文件的jQuery插件 语法:$.ajaxFileUpload([options]) options参数说明: 参数 作用 url 上传处理程序地址 file ...

  7. SpringMVC集成MongoDb

    (1)pom添加相关依赖 <dependency> <groupId>org.springframework.data</groupId> <artifact ...

  8. 【C++】解决vs2015经常卡顿的办法

    VS2015经常性的卡顿,参考了zhihu里问答的办法,编译和使用的时候的确快多了 为什么vs2015经常卡顿? https://www.zhihu.com/question/34911426 感谢z ...

  9. C# 关于用7zip压缩文件提示win32exception 系统找不到文件解决方案(win7 x64)

    网上已经很多这方面的资料了,我就简单的说下好了 为了方便以后的查看 --------------------- 1.需要下载7zSharp:http://7zsharp.codeplex.com/re ...

  10. 『转载』hadoop2.x常用端口、定义方法及默认端口

    『转载』hadoop2.x常用端口.定义方法及默认端口 1.问题导读 DataNode的http服务的端口.ipc服务的端口分别是哪个? NameNode的http服务的端口.ipc服务的端口分别是哪 ...