android 自定义viewgroup onmeasure,自定义ViewGroup

1460000006760480

文章来自:Android程序员日记

作者:贤榆的鱼

测试阅读时间:5min 12s

前言

好像有一段时间没有更新技术篇了,所以赶紧发一篇出来提醒一下我一个android开发,怕你们忘了!

之前我分享了一下关于自定义view的三种类型!其中谈到了关于组合式的自定义View,那我们都知道它是继承自ViewGroup的子类的,如LinearLayout和RelativeLayout。这样我们借助这样的框架布局将一些我们需要的控件放进去就实现了组合的自定View。

那么其实我们直接集成viewGroup也是可以的,只是这种方法我们通常并不用它来实现自定义View,而主要是用于实现自定义的布局。类似LinearLayout等系统布局!那么还是继承我一贯的分享风格,下面我们实现一个简单的带有粘性效果的ScrollView来作为本文的例子

正文

Step 0:看效果图

嘘!别说话,静静的观赏!

1460000006760481

Step 1:创建一个类继承ViewGroup

public class CustomScrollView extends ViewGroup {

//下面的这些全局变量在后面用到的时候在创建

int mScreenHeight;

private int mStartY;

private int mEnd;

private Scroller mScroller;

private int mLastY;

private int childCount;

private int realChildCount;

public CustomScrollView(Context context) {

this(context,null);

}

public CustomScrollView(Context context, AttributeSet attrs) {

this(context, attrs,0);

}

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

super(context, attrs, defStyleAttr);

init();

}

//初始化一些必要变量并创建一些必要的对象

private void init() {

WindowManager wm= (WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE);

mScreenHeight=wm.getDefaultDisplay().getHeight();

mScroller = new Scroller(getContext());

}

}

注:在自定义View之总结中,我简单总结过自定义View的三个构造方法会在xml中使用时和用代码动态添加时被调用。我们在这里用this方法调用本类中多参数的构造,最后在三个参数的构造方法中实现初始化操作是一种简便的写法!

Step 2:在onMeasure中测量子view大小

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int childCount = getChildCount();

for (int i=0;i

View childView = getChildAt(i);

measureChild(childView,widthMeasureSpec,heightMeasureSpec);

}

}

Step 3:在onLayout中绘制子view的位置

@Override

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

realChildCount = 0;

childCount = getChildCount();

//set the ViewGroup's height

MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();

lp.height=mScreenHeight* childCount;

setLayoutParams(lp);

//绘制子view的位置

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

View childView = getChildAt(i);

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

realChildCount++;

childView.layout(l,i*mScreenHeight,r,(i+1)*mScreenHeight);

}

}

}

Step 4:在onTouchEvent中相应触摸及滚动事件

@Override

public boolean onTouchEvent(MotionEvent event) {

//在这个触摸事件中,需要判断两个距离,一个是手指移动的距离一个是view滚动的距离

//这就是随着手指的移动会发送改变的量

int y = (int) event.getY();

switch (event.getAction()){

case MotionEvent.ACTION_DOWN:

mLastY = y;

//这是view滚动的距离

mStartY = getScrollY();

break;

case MotionEvent.ACTION_MOVE:

//当我们再次触碰屏幕时,如果之前的滚动动画还没有停止,我们也让他立即停止

if(!mScroller.isFinished()){

mScroller.abortAnimation();

}

int dY= mLastY -y;

//当滚动时触及到上边缘时,我们增加一个回弹的效果

if(getScrollY()<0){

dY/=3;

}

//如果是下边缘,我们不让它继续向下滚动

if(getScrollY()>mScreenHeight*realChildCount-mScreenHeight){

dY=0;

}

//让我们的view滚动相应的dy距离

scrollBy(0,dY);

mLastY=y;

break;

case MotionEvent.ACTION_UP:

mEnd = getScrollY();

int dScrollY = mEnd - mStartY;

if(dScrollY>0){//向上滚动的情况

if (dScrollY

mScroller.startScroll(0,getScrollY(),0,-dScrollY);

}else{

mScroller.startScroll(0,getScrollY(),0,mScreenHeight-dScrollY);

}

}else{//向下滚动的情况

if(-dScrollY

mScroller.startScroll(0,getScrollY(),0,-dScrollY);

}else{

mScroller.startScroll(0,getScrollY(),0,-mScreenHeight-dScrollY);

}

}

break;

}

//必须加该方法,否则上面的代码都会无效果

postInvalidate();

return true;

}

Step 5:应用

创建一个activiy在其对应的xml文件中添加我们的控件:

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/main"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context="com.timen4.ronnny.customscrollview.MainActivity">

android:id="@+id/csv"

android:layout_height="match_parent"

android:layout_width="match_parent"

android:background="@drawable/img4"

/>

在activity类中:

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

initView();

}

//添加相应的子控件并填充模拟数据

private void initView() {

mainView = (RelativeLayout) findViewById(R.id.main);

CustomScrollView customScrollView = (CustomScrollView) findViewById(R.id.csv);

WindowManager wm= (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);

int mScreenHeight = wm.getDefaultDisplay().getHeight();

ViewGroup.LayoutParams layoutParams=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,mScreenHeight);

ViewGroup.LayoutParams layoutParams2=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,mScreenHeight);

ViewGroup.LayoutParams layoutParams3=new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,mScreenHeight);

ImageView imageView = new ImageView(this);

ImageView imageView2 = new ImageView(this);

ImageView imageView3 = new ImageView(this);

imageView.setLayoutParams(layoutParams);

imageView2.setLayoutParams(layoutParams2);

imageView3.setLayoutParams(layoutParams3);

imageView.setBackgroundResource(R.drawable.img1);

imageView2.setBackgroundResource(R.drawable.img2);

imageView3.setBackgroundResource(R.drawable.img3);

customScrollView.addView(imageView);

customScrollView.addView(imageView2);

customScrollView.addView(imageView3);

}

简单总结

对于实现我们的ViewGroup核心大致可以分为三步:

重写onMeasure()方法来对子View进行测量

重写onLayout方法来确定子view的位置

重写onTouchEvent()来增加响应事件

注:这里只是一个简单的例子,真正可用的布局我们需要考虑到的细节更多,并且对wrap_content的支持也是需要在onMeasure当中自己对其进行支持的!当然我们也可以利用ScrollView的源码来进行学习!

后记

大家看我git上面的提交记录,viewGroup初稿的提交时间和最后完成的提交时间中间相隔了一个18天,差不多三周的时间!我在这里,就不一一交代这中间都有那些事情让我这篇文章和项目代码搁置了!因为我觉无论是自责的说我懒,还是自我开拓的说这段时间我很忙、我很累,都没有太大的意义!毕竟这个项目也好这篇文章也罢,都只是为了成为更好的自己而必须经历的一个过程!所以在这个过程当中出现的任何事情,我都尽量不用道德的眼光(也就是简单的用好和坏去自评)。有人说自责可以鞭笞自己,而自我欣赏也可以对自己有积极正面的鼓励!而事实并不像我们想想的那么好!在《自控力》一书中,就表示往往我们的自我责备更容易带来一种自怨自艾、自爆自弃!而“认为自己已经很好了”往往也会蒙蔽我们对最终目标前进的心!

所以最后我想说(也是对我自己说):客观的认识我们达成目标前的整个过程,平常心对待它的各种变化!时刻提醒自己那个最终的目标!

1460000006760482

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值