編輯:關於 Android 程式設計
上一篇文章自定義viewgroup(1)地址:https://www.jb51.net/article/100608.htm
上一篇把子 View 橫向排好了,但畫面是死的,滑不動。這篇就來加上觸控拖動,讓它能跟著手指走。直接上程式碼:
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 * 在橫向布局的基礎上,增加啦滾動效果,但是沒有邊界限制 */ 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) { //重新設置寬高 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; 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); }
拖動的實作只有幾行:ACTION_DOWN 時記下起始 X,並順手中止可能還在跑的 Scroller 動畫(否則手指按下後畫面還會繼續滑一段);ACTION_MOVE 時用「上次位置減去目前位置」算出位移量,取整後交給 scrollBy。
要注意的是這個版本尚未加邊界判斷,往兩側拖過頭會露出空白,下一篇會補上。另外 computeScroll 裡用的是 postInvalidate 而非 invalidate,因為它可能在非 UI 執行緒被呼叫,用 postInvalidate 更安全。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持本站。
Notification 實作狀態列通知的完整流程
用手机的時候,如果有未接來電或新短訊,系統會在螢幕上方的狀態列給出提示。Android 對應的兩個類是 Notification 與 NotificationManage
自定義 TextView 讓 drawableLeft 與文字整體居中
使用 TextView 的 drawableLeft 屬性時,圖示會貼齊文字左側,導致「圖片+文字」整體偏左,視覺上不居中。要解決這個問題,最簡單的辦法是自定義一個 Te
把 Android 模組打成 jar 並用 ProGuard 混淆的實操筆記
又是一個睡不著的夜裡,來處理一件拖了很久的事:專案裡有部分程式碼想對外提供成 jar,而且不希望被人直接反編譯看光光。 步驟一:打包 jar 在 Eclipse 中
Android 開發:以 MVP 架構實作仿微信圖片選擇器
這篇記錄一個仿微信風格的圖片選擇器。整體架構採用 MVP,並搭配一套自己寫的基礎 lib(開源在 ndileber),目的是把資料來源、業務邏輯與 UI 徹底分開,後續要