上一篇文章自訂viewgroup(3)地址:http://www.jb51.net/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 { //滾動計算輔助類 private Scroller mScroller; //手指落點的X座標 private float mLastMotionX = 0; //螢幕寬度 private int screenWidth; //手指加速度輔助類 private VelocityTracker mVelocityTracker; //每秒移動的最小dp private int mMinimumVelocity; //每秒移動的最大dp private int mMaximumVelocity; /** * 使用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; //擷取最小和最大的移動距離 final ViewConfiguration configuration = ViewConfiguration.get(context); mMinimumVelocity = configuration.getScaledMinimumFlingVelocity(); mMaximumVelocity = configuration.getScaledMaximumFlingVelocity(); } /** * 滾動時需要重寫的方法,用於控制滾動 */ @Override public void computeScroll() { //判斷滾動時候停止 if (mScroller.computeScrollOffset()) { //滾動到指定的位置 scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); //這句話必須寫,否則不能即時重新整理 postInvalidate(); } } /** * 手指觸屏事件監聽 */ @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub int action = event.getAction(); //擷取現在手指所在的位置的x座標 float x = event.getX(); //判斷觸發的時間 switch (action) { //按下事件 case MotionEvent.ACTION_DOWN: //初始化或服用加速度測試器 initOrResetVelocityTracker(); //測試器添加按下事件 mVelocityTracker.addMovement(event); //如果停止滾動則取消動畫(即手指按下就停止滾動) if (!mScroller.isFinished()) { mScroller.abortAnimation(); } //擷取現在的x座標 mLastMotionX = event.getX(); break; //移動事件 case MotionEvent.ACTION_MOVE: //測試器添加移動事件 if (mVelocityTracker != null) { mVelocityTracker.addMovement(event); } //計算移動的位移量 float delt = mLastMotionX - x; //重設手指位置 mLastMotionX = x; //滾動 scrollBy((int) delt, 0); break; //手指抬起事件 case MotionEvent.ACTION_UP: //測試器添加抬起事件 mVelocityTracker.addMovement(event); //添加加速度的測試時間,這裡是測量1000毫秒內的加速度 mVelocityTracker.computeCurrentVelocity(1000, mMaximumVelocity); //擷取x方向加速度 float pxsec = mVelocityTracker.getXVelocity(); //得到最後一個子View View lastChild = getChildAt(getChildCount() - 1); //擷取滑動的最大滑動距離(最後一個Child的右邊框的座標減去螢幕的寬度) int finalyChild = (int) (lastChild.getX() + lastChild.getWidth() - screenWidth); //如果x的加速度大於系統設定的最小移動距離,就可以慣性滑動 if (Math.abs(pxsec) > mMinimumVelocity) mScroller.fling(getScrollX(), 0, (int) -pxsec, 0, 0, finalyChild, 0, 0); //如果滑動的距離小於第一個控制項的最左邊(0)則回彈至(0,0)點 if (getScrollX() < 0) { scrollTo(0, 0); } //如果滑動的距離大於最大可滑動距離則滑動到最後一個子View if (getScrollX() >= finalyChild) scrollTo(finalyChild, 0); //重新整理介面 invalidate(); //清空測試器 recycleVelocityTracker(); break; default: break; } return true; } /** * 建立或複用加速度測試器 */ private void initOrResetVelocityTracker() { if (mVelocityTracker == null) { mVelocityTracker = VelocityTracker.obtain(); } else { mVelocityTracker.clear(); } } /** * 回收加速度測試器,防止記憶體流失 */ private void recycleVelocityTracker() { if (mVelocityTracker != null) { mVelocityTracker.recycle(); mVelocityTracker = null; } } @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); }}
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。