Android自定義ViewGroup實(shí)現(xiàn)受邊界限制的滾動(dòng)操作(3)
更新時(shí)間:2016年12月20日 14:40:08 作者:封魔之殤
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup實(shí)現(xiàn)受邊界限制的滾動(dòng)操作,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
上一篇文章《自定義viewgroup(2)》地址:http://www.dbjr.com.cn/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 * 在滾動(dòng)的基礎(chǔ)上,增加了邊界限制 */ public class ScrollViewGroup extends ViewGroup { //滾動(dòng)計(jì)算輔助類 private Scroller mScroller; //手指落點(diǎn)的X坐標(biāo) private float mLastMotionX = 0; //屏幕寬度 private int screenWidth; /** * 使用new關(guān)鍵字創(chuàng)建對(duì)象的時(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); } /** * 初始化方法 * 初始化滾動(dòng)輔助類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; } /** * 滾動(dòng)時(shí)需要重寫的方法,用于控制滾動(dòng) */ @Override public void computeScroll() { //判斷滾動(dòng)時(shí)候停止 if (mScroller.computeScrollOffset()) { //滾動(dòng)到指定的位置 scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); //這句話必須寫,否則不能實(shí)時(shí)刷新 postInvalidate(); } } /** * 手指觸屏事件監(jiān)聽 * @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) { //重新設(shè)置寬高 this.setMeasuredDimension(measureWidth(widthMeasureSpec, heightMeasureSpec), measureHeight(widthMeasureSpec, heightMeasureSpec)); } /** * 測(cè)量寬度 */ 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(); //重新測(cè)量子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; } /** * 測(cè)量高度 */ 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(); //重新測(cè)量子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); } }
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
- Android自定義ViewGroup實(shí)現(xiàn)堆疊頭像的點(diǎn)贊Layout
- Android自定義ViewGroup實(shí)現(xiàn)標(biāo)簽浮動(dòng)效果
- Android自定義ViewGroup之實(shí)現(xiàn)FlowLayout流式布局
- Android App開發(fā)中自定義View和ViewGroup的實(shí)例教程
- 一篇文章弄懂Android自定義viewgroup的相關(guān)難點(diǎn)
- Android應(yīng)用開發(fā)中自定義ViewGroup的究極攻略
- Android動(dòng)畫效果之自定義ViewGroup添加布局動(dòng)畫(五)
- Android自定義ViewGroup的實(shí)現(xiàn)方法
- Android自定義ViewGroup實(shí)現(xiàn)朋友圈九宮格控件
相關(guān)文章
Android淺析viewBinding和DataBinding
這篇文章主要介紹了Android淺析viewBinding和DataBinding,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Android模擬美團(tuán)客戶端進(jìn)度提示框
這篇文章主要為大家詳細(xì)介紹了Android模擬美團(tuán)客戶端進(jìn)度提示框的實(shí)現(xiàn)過程,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2015-09-09Android中Glide加載圖片并實(shí)現(xiàn)圖片緩存
本篇文章主要介紹了Android中Glide加載圖片并實(shí)現(xiàn)圖片緩存,這里和大家簡(jiǎn)單的分享一下Glide的使用方法以及緩存 ,有興趣的可以了解一下。2017-03-03SQLite數(shù)據(jù)庫在Android中的使用小結(jié)
SQLIte是一款輕型的數(shù)據(jù)庫,占用資源非常低,在嵌入式設(shè)備中,可能只需幾百k的內(nèi)存,這篇文章主要介紹了SQLite數(shù)據(jù)庫在Android中的使用,需要的朋友可以參考下2024-07-07Android控件FlowLikeView實(shí)現(xiàn)點(diǎn)贊動(dòng)畫
這篇文章主要為大家詳細(xì)介紹了一個(gè)點(diǎn)贊動(dòng)畫的優(yōu)雅控件FlowLikeView,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-06-06Android實(shí)現(xiàn)音樂播放器歌詞顯示效果
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)音樂播放器歌詞顯示效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-06-06