Android自定義ViewGroup實(shí)現(xiàn)可滾動(dòng)的橫向布局(2)
上一篇文章自定義viewgroup(1)地址:http://www.dbjr.com.cn/article/100608.htm
這里直接代碼:
package com.example.libingyuan.horizontallistview.ScrollViewGroup; import android.content.Context; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; import android.widget.Scroller; /** * 自定義ViewGroup * 在橫向布局的基礎(chǔ)上,增加啦滾動(dòng)效果,但是沒(méi)有邊界限制 */ public class ScrollViewGroup extends ViewGroup { private Scroller mScroller; private float mLastMotionX = 0; public ScrollViewGroup(Context context) { this(context, null); } public ScrollViewGroup(Context context, AttributeSet attrs) { this(context, attrs, 0); } public ScrollViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } private void init(Context context) { mScroller = new Scroller(context); } @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(); 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: 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; } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int childLeft = 0; int childWidth; int height = getHeight(); int childCount = getChildCount(); 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開(kāi)發(fā)之橫向滾動(dòng)/豎向滾動(dòng)的ListView(固定列頭)
- Android程序開(kāi)發(fā)之ListView實(shí)現(xiàn)橫向滾動(dòng)(帶表頭與固定列)
- 詳解Android使GridView橫向水平滾動(dòng)的實(shí)現(xiàn)方式
- Android使用GridView實(shí)現(xiàn)橫向滾動(dòng)效果
- Android開(kāi)發(fā)實(shí)現(xiàn)橫向列表GridView橫向滾動(dòng)的方法【附源碼下載】
- Android GridView實(shí)現(xiàn)橫向列表水平滾動(dòng)
- Android實(shí)現(xiàn)自定義的彈幕效果
- 實(shí)例解析如何在Android應(yīng)用中實(shí)現(xiàn)彈幕動(dòng)畫(huà)效果
- Android 實(shí)現(xiàn)仿網(wǎng)絡(luò)直播彈幕功能詳解及實(shí)例
- Android實(shí)現(xiàn)橫向無(wú)限循環(huán)滾動(dòng)的單行彈幕效果
相關(guān)文章
Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能示例
這篇文章主要介紹了Android基于widget組件實(shí)現(xiàn)物體移動(dòng)/控件拖動(dòng)功能,結(jié)合實(shí)例形式分析了widget組件在桌面應(yīng)用中的事件響應(yīng)與屬性動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-10-10Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫(huà)View效果的思路代碼
這篇文章主要介紹了Android實(shí)現(xiàn)長(zhǎng)按圓環(huán)動(dòng)畫(huà)View效果,本文給大家分享實(shí)現(xiàn)思路,通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09Kotlin設(shè)計(jì)模式之委托模式使用方法詳解
Kotlin提供了兩個(gè)本機(jī)功能來(lái)實(shí)現(xiàn)委托模式,第一個(gè)是接口委托(例如策略模式),另一種是屬性委托,它專(zhuān)注于類(lèi)成員/屬性(例如延遲加載、observable等),它們共同提供了一組豐富而簡(jiǎn)潔的功能,通過(guò)本博客,您將了解在什么情況下使用此模式2023-09-09TextView實(shí)現(xiàn)圖文混合編排的方法
這篇文章主要為大家詳細(xì)介紹了TextView實(shí)現(xiàn)圖文混合編排的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08Android基于ImageView繪制的開(kāi)關(guān)按鈕效果示例
這篇文章主要介紹了Android基于ImageView繪制的開(kāi)關(guān)按鈕效果,結(jié)合實(shí)例形式分析了Android使用ImageView進(jìn)行按鈕繪制的界面布局、功能實(shí)現(xiàn)及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-03-03Android 跨進(jìn)程通Messenger(簡(jiǎn)單易懂)
這篇文章主要介紹了Android Messenger跨進(jìn)程通的相關(guān)資料,非常簡(jiǎn)單容易理解,對(duì)android messenger 進(jìn)程通訊的相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-08-08Android開(kāi)發(fā)之多線程中實(shí)現(xiàn)利用自定義控件繪制小球并完成小球自動(dòng)下落功能實(shí)例
這篇文章主要介紹了Android開(kāi)發(fā)之多線程中實(shí)現(xiàn)利用自定義控件繪制小球并完成小球自動(dòng)下落功能的方法,涉及Android多線程編程及圖形繪制相關(guān)技巧,需要的朋友可以參考下2015-12-12