欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義viewgroup快速滑動(4)

 更新時(shí)間:2016年12月20日 15:43:44   作者:封魔之殤  
這篇文章主要為大家詳細(xì)介紹了Android自定義viewgroup快速滑動的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

上一篇文章自定義viewgroup(3)地址:http://www.dbjr.com.cn/article/100618.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.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Scroller;

/**
 * 自定義ViewGroup
 * 增加了加速度滑動
 */
public class ScrollViewGroup extends ViewGroup {
  //滾動計(jì)算輔助類
  private Scroller mScroller;
  //手指落點(diǎn)的X坐標(biāo)
  private float mLastMotionX = 0;
  //屏幕寬度
  private int screenWidth;
  //手指加速度輔助類
  private VelocityTracker mVelocityTracker;
  //每秒移動的最小dp
  private int mMinimumVelocity;
  //每秒移動的最大dp
  private int mMaximumVelocity;

  /**
   * 使用new關(guān)鍵字創(chuàng)建對象的時(shí)候調(diào)用
   *
   * @param context 上下文
   */
  public ScrollViewGroup(Context context) {
    this(context, null);
  }

  /**
   * 在XML文件中使用的時(shí)候調(diào)用
   *
   * @param context 上下文
   * @param attrs  屬性:如 android:layout_width="wrap_content"
   */
  public ScrollViewGroup(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
  }

  /**
   * 在xml文件中調(diào)用,并且使用了自定義屬性的時(shí)候調(diào)用
   *
   * @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以及計(jì)算出屏幕寬度
   *
   * @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;
    //獲取最小和最大的移動距離
    final ViewConfiguration configuration = ViewConfiguration.get(context);
    mMinimumVelocity = configuration.getScaledMinimumFlingVelocity();
    mMaximumVelocity = configuration.getScaledMaximumFlingVelocity();
  }

  /**
   * 滾動時(shí)需要重寫的方法,用于控制滾動
   */
  @Override
  public void computeScroll() {
    //判斷滾動時(shí)候停止
    if (mScroller.computeScrollOffset()) {
      //滾動到指定的位置
      scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
      //這句話必須寫,否則不能實(shí)時(shí)刷新
      postInvalidate();
    }
  }

  /**
   * 手指觸屏事件監(jiān)聽
   */
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    // TODO Auto-generated method stub
    int action = event.getAction();
    //獲取現(xiàn)在手指所在的位置的x坐標(biāo)
    float x = event.getX();
    //判斷觸發(fā)的時(shí)間
    switch (action) {
      //按下事件
      case MotionEvent.ACTION_DOWN:
        //初始化或服用加速度測試器
        initOrResetVelocityTracker();
        //測試器添加按下事件
        mVelocityTracker.addMovement(event);
        //如果停止?jié)L動則取消動畫(即手指按下就停止?jié)L動)
        if (!mScroller.isFinished()) {
          mScroller.abortAnimation();
        }
        //獲取現(xiàn)在的x坐標(biāo)
        mLastMotionX = event.getX();
        break;
      //移動事件
      case MotionEvent.ACTION_MOVE:
        //測試器添加移動事件
        if (mVelocityTracker != null) {
          mVelocityTracker.addMovement(event);
        }
        //計(jì)算移動的偏移量
        float delt = mLastMotionX - x;
        //重置手指位置
        mLastMotionX = x;
        //滾動
        scrollBy((int) delt, 0);
        break;
      //手指抬起事件
      case MotionEvent.ACTION_UP:
        //測試器添加抬起事件
        mVelocityTracker.addMovement(event);
        //添加加速度的測試時(shí)間,這里是測量1000毫秒內(nèi)的加速度
        mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity);
        //獲取x方向加速度
        float pxsec = mVelocityTracker.getXVelocity();
        //得到最后一個(gè)子View
        View lastChild = getChildAt(getChildCount() - 1);
        //獲取滑動的最大滑動距離(最后一個(gè)Child的右邊框的坐標(biāo)減去屏幕的寬度)
        int finalyChild = (int) (lastChild.getX() + lastChild.getWidth() - screenWidth);
        //如果x的加速度大于系統(tǒng)設(shè)定的最小移動距離,就可以慣性滑動
        if (Math.abs(pxsec) > mMinimumVelocity)
          mScroller.fling(getScrollX(), 0, (int) -pxsec, 0, 0, finalyChild, 0, 0);
        //如果滑動的距離小于第一個(gè)控件的最左邊(0)則回彈至(0,0)點(diǎn)
        if (getScrollX() < 0) {
          scrollTo(0, 0);
        }
        //如果滑動的距離大于最大可滑動距離則滑動到最后一個(gè)子View
        if (getScrollX() >= finalyChild)
          scrollTo(finalyChild, 0);
        //刷新界面
        invalidate();
        //清空測試器
        recycleVelocityTracker();
        break;
      default:
        break;
    }

    return true;
  }

  /**
   * 創(chuàng)建或復(fù)用加速度測試器
   */
  private void initOrResetVelocityTracker() {
    if (mVelocityTracker == null) {
      mVelocityTracker = VelocityTracker.obtain();
    } else {
      mVelocityTracker.clear();
    }
  }

  /**
   * 回收加速度測試器,防止內(nèi)存泄漏
   */
  private void recycleVelocityTracker() {
    if (mVelocityTracker != null) {
      mVelocityTracker.recycle();
      mVelocityTracker = null;
    }
  }

  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    //重新設(shè)置寬高
    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;
  }

  /**
   * 給子布局設(shè)定位置
   */
  @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的數(shù)量
    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);
  }

}

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論