Android自定义ViewGroup实现受边界限制的滚动操作(3)

所属分类: 软件编程 / Android 阅读数: 26
收藏 0 赞 0 分享

上一篇文章《自定义viewgroup(2)》地址:https://www.jb51.net/article/100610.htm

代码

package com.example.libingyuan.horizontallistview.ScrollViewGroup;

import android.content.Context;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;

/**
 * 自定义ViewGroup
 * 在滚动的基础上,增加了边界限制
 */
public class ScrollViewGroup extends ViewGroup {
 //滚动计算辅助类
 private Scroller mScroller;
 //手指落点的X坐标
 private float mLastMotionX = 0;
 //屏幕宽度
 private int screenWidth;

 /**
  * 使用new关键字创建对象的时候调用
  * @param context 上下文
  */
 public ScrollViewGroup(Context context) {
  this(context, null);
 }
 /**
  * 在XML文件中使用的时候调用
  * @param context 上下文
  * @param attrs 属性:如 android:layout_width="wrap_content"
  */
 public ScrollViewGroup(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 /**
  * 在xml文件中调用,并且使用了自定义属性的时候调用
  * @param context 上下文
  * @param attrs 属性:如 android:layout_width="wrap_content"
  * @param defStyleAttr 自定义属性的id
  */
 public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  init(context);
 }

 /**
  * 初始化方法
  * 初始化滚动辅助类Scroller以及计算出屏幕宽度
  * @param context
  */
 private void init(Context context) {
  mScroller = new Scroller(context);
  WindowManager manager = (WindowManager) context
    .getSystemService(Context.WINDOW_SERVICE);
  DisplayMetrics outMetrics = new DisplayMetrics();
  manager.getDefaultDisplay().getMetrics(outMetrics);
  screenWidth = outMetrics.widthPixels;
 }

 /**
  * 滚动时需要重写的方法,用于控制滚动
  */
 @Override
 public void computeScroll() {
  //判断滚动时候停止
  if (mScroller.computeScrollOffset()) {
   //滚动到指定的位置
   scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
   //这句话必须写,否则不能实时刷新
   postInvalidate();
  }
 }

 /**
  * 手指触屏事件监听
  * @param event
  * @return
  */
 @Override
 public boolean onTouchEvent(MotionEvent event) {
  // TODO Auto-generated method stub
  int action = event.getAction();
  float x = event.getX();
  switch (action) {
   case MotionEvent.ACTION_DOWN:
    if (!mScroller.isFinished()) {
     mScroller.abortAnimation();
    }
    mLastMotionX = event.getX();
    break;
   case MotionEvent.ACTION_MOVE:
    float delt = mLastMotionX - x;
    mLastMotionX = x;
    scrollBy((int) delt, 0);
    break;
   case MotionEvent.ACTION_UP:
    View lastChild=getChildAt(getChildCount()-1);
    int finalyChild= (int) (lastChild.getX()+lastChild.getWidth()-screenWidth);
    if (getScrollX()<0){
     scrollTo(0,0);
    }
    if (getScrollX()>=finalyChild)
     scrollTo(finalyChild,0);
    invalidate();
    break;

   default:
    break;
  }

  return true;
 }

 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  //重新设置宽高
  this.setMeasuredDimension(measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec));
 }

  /**
  * 测量宽度
  */
 private int measureWidth(int widthMeasureSpec, int heightMeasureSpec) {
  // 宽度
  int sizeWidth = MeasureSpec.getSize(widthMeasureSpec);
  int modeWidth = MeasureSpec.getMode(widthMeasureSpec);
  //父控件的宽(wrap_content)
  int width = 0;
  int childCount = getChildCount();

  //重新测量子view的宽度,以及最大高度
  for (int i = 0; i < childCount; i++) {
   View child = getChildAt(i);
   measureChild(child, widthMeasureSpec, heightMeasureSpec);
   MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
   int childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
   width += childWidth;
  }
  return modeWidth == MeasureSpec.EXACTLY ? sizeWidth : width;
 }

 /**
  * 测量高度
  */
 private int measureHeight(int widthMeasureSpec, int heightMeasureSpec) {
  //高度
  int sizeHeight = MeasureSpec.getSize(heightMeasureSpec);
  int modeHeight = MeasureSpec.getMode(heightMeasureSpec);
  //父控件的高(wrap_content)
  int height = 0;
  int childCount = getChildCount();

  //重新测量子view的宽度,以及最大高度
  for (int i = 0; i < childCount; i++) {
   View child = getChildAt(i);
   measureChild(child, widthMeasureSpec, heightMeasureSpec);
   MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
   int childHeight = child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin;
   height += childHeight;
  }
  height = height / childCount;
  return modeHeight == MeasureSpec.EXACTLY ? sizeHeight : height;
 }

 /**
  * 给子布局设定位置
  */
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  int childLeft = 0;//子View左边的间距
  int childWidth;//子View的宽度
  int height = getHeight();//屏幕的宽度
  int childCount = getChildCount();//子View的数量
  for (int i = 0; i < childCount; i++) {
   View child = getChildAt(i);
   MarginLayoutParams lp = (MarginLayoutParams) child.getLayoutParams();
   childWidth = child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin;
   child.layout(childLeft, 0, childLeft + childWidth, height);
   childLeft += childWidth;
  }
 }

 @Override
 public LayoutParams generateLayoutParams(AttributeSet attrs) {
  return new MarginLayoutParams(getContext(), attrs);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android设计登录界面、找回密码、注册功能

这篇文章主要为大家详细介绍了Android设计登录界面的方法,Android实现找回密码、注册功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android通过手势实现答题器翻页效果

这篇文章主要为大家详细介绍了Android通过手势实现答题器翻页效果,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android采用双缓冲技术实现画板

这篇文章主要为大家详细介绍了Android采用双缓冲技术实现画板的相关资料,思路清晰,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android开发之毛玻璃效果实例代码

这篇文章主要给大家分享android开发之毛玻璃效果的实例代码,非常具有参考借鉴价值,感兴趣的朋友一起学习吧
收藏 0 赞 0 分享

Android实现桌面悬浮窗、蒙板效果实例代码

这篇文章主要介绍了Android实现桌面悬浮窗、蒙板效果实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

深入解读Android的Volley库的功能结构

这篇文章主要介绍了Android的Volley开发框架的功能结构,Volley是Android开发中网络部分的一大利器,包含很多HTTP协议通信的相关操作,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发中使用Volley库发送HTTP请求的实例教程

这篇文章主要介绍了Android开发中使用Volley库发送HTTP请求的实例教程,包括创建Volley单例的基本知识与取消Request请求的技巧等,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿QQ聊天撒花特效 很真实

本文写的这个特效,是关于聊天的,你肯定遇到过,就是你跟人家聊天的时候,比如发送应(么么哒),然后屏幕上全部就是表情了,今天我们就是做这个,撒花的特效,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android的HTTP操作库Volley的基本使用教程

这篇文章主要介绍了Android的HTTP操作库Volley的基本使用教程,包括JSON请求与图片加载等用法的实例,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿水波纹流量球进度条控制器

这篇文章主要介绍了Android仿水波纹流量球进度条控制器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享
查看更多