android 继承ViewGroup实现自定义布局,安卓移动开发基础案例教程

/**

  • @author :huangxianfeng on 2016/10/13.

  • 继承ViewGroup实现自定义布局

*/

public class HorizontalScrollViewEx extends ViewGroup{

private static final String TAG = “HorizontalScrollViewEx”;

private int mChildrenSize;

private int mChildWidth;

private int mChildIndex;

//分别记录上次滑动的坐标

private int mLastX = 0;

private int mLastY = 0;

//分别记录上次滑动的坐标(onInterceptTouchEvent)

private int mLastXIntercept = 0;

private int mLastYIntercept = 0;

private Scroller mScroller;

private VelocityTracker mVelocityTracker;

public HorizontalScrollViewEx(Context context) {

super(context);

init();

}

public HorizontalScrollViewEx(Context context, AttributeSet attrs) {

super(context, attrs);

init();

}

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

super(context, attrs, defStyleAttr);

init();

}

private void init() {

if (mScroller==null){

mScroller = new Scroller(getContext());

mVelocityTracker = VelocityTracker.obtain();

}

}

@Override

public boolean onInterceptTouchEvent(MotionEvent ev) {

boolean intercepted = false;

int x = (int)ev.getX();

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

switch (ev.getAction()){

case MotionEvent.ACTION_DOWN:

intercepted = false;

if (!mScroller.isFinished()){

mScroller.abortAnimation();

intercepted = true;

}

break;

case MotionEvent.ACTION_MOVE:

int detlaX = x-mLastXIntercept;

int detlaY = y-mLastYIntercept;

if (Math.abs(detlaX) > Math.abs(detlaY)){

intercepted = true;

}else{

intercepted = false;

}

break;

case MotionEvent.ACTION_UP:

intercepted = false;

break;

default:

break;

}

Log.d(TAG,"inter

《Android学习笔记总结+最新移动架构视频+大厂安卓面试真题+项目实战源码讲义》

【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 完整内容开源分享

cepted = "+intercepted);

mLastX = x;

mLastY = y;

mLastXIntercept = x;

mLastYIntercept = y;

return intercepted;

}

@Override

public boolean onTouchEvent(MotionEvent event) {

mVelocityTracker.addMovement(event);

int x = (int)getX();

int y = (int)getY();

switch (event.getAction()){

case MotionEvent.ACTION_DOWN:

if (!mScroller.isFinished()){

mScroller.abortAnimation();

}

break;

case MotionEvent.ACTION_MOVE:

int detalX = x-mLastX;

int detalY = y-mLastY;

scrollBy(-detalX,0);

break;

case MotionEvent.ACTION_UP:

int scrollX = getScrollX();

mVelocityTracker.computeCurrentVelocity(1000);

float xVelocity = mVelocityTracker.getXVelocity();

if (Math.abs(xVelocity) >= 50){

mChildIndex = xVelocity > 0 ? mChildIndex -1 : mChildIndex + 1;

}else{

mChildIndex = (scrollX + mChildWidth / 2) / mChildWidth;

}

mChildIndex = Math.max(0,Math.min(mChildIndex,mChildrenSize -1 ));

int dx = mChildIndex * mChildWidth - scrollX;

smoothScrollBy(dx, 0);

mVelocityTracker.clear();

break;

default:

break;

}

mLastX = x;

mLastY = y;

return true;

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

super.onMeasure(widthMeasureSpec, heightMeasureSpec);

int measuredWidth = 0;

int measuredHeight = 0;

//拿到自己含有子元素的个数

final int childCount = getChildCount();

measureChildren(widthMeasureSpec,heightMeasureSpec);

int widthSpaceSize = MeasureSpec.getSize(widthMeasureSpec);

int widthSpaceMode = MeasureSpec.getMode(widthMeasureSpec);

int heightSpaceSize = MeasureSpec.getSize(heightMeasureSpec);

int heightSpaceMode = MeasureSpec.getMode(heightMeasureSpec);

/**

  • 1.判断是否含有子元素,如果没有直接把自己的高度和宽度设置为0

  • 2.判断子元素的宽度和高度是否采用的warp_content,如果采用了则把所有的子元素的宽度之和给自己并且把

  • 第一个子元素的高度给自己。

  • 3.再进行分别判断高度和宽度那个用了warp_content,就对其分别设置不一样的值。

*/

if (childCount == 0){

setMeasuredDimension(0,0);

}else if (widthSpaceMode == MeasureSpec.AT_MOST && heightSpaceMode == MeasureSpec.AT_MOST){

final View childView = getChildAt(0);

measuredWidth = childView.getMeasuredWidth() * childCount;

measuredHeight = childView.getMeasuredHeight();

setMeasuredDimension(measuredWidth,measuredHeight);

}else if (heightSpaceMode == MeasureSpec.AT_MOST){

final View childView = getChildAt(0);

measuredHeight = childView.getMeasuredHeight();

setMeasuredDimension(widthSpaceSize,childView.getMeasuredHeight());

}else if (widthSpaceMode == MeasureSpec.AT_MOST){

final View childView = getChildAt(0);

measuredWidth = childView.getMeasuredWidth() * childCount;

setMeasuredDimension(measuredWidth,heightSpaceSize);

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值