日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

ScrollView反弹效果

發布時間:2025/3/20 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 ScrollView反弹效果 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1 public class BounceScrollView extends ScrollView { 2 private View inner;// 孩子View 3 4 private float y;// 點擊時y坐標 5 6 private Rect normal = new Rect();// 矩形(這里只是個形式,只是用于判斷是否需要動畫.) 7 8 private boolean isCount = false;// 是否開始計算 9 10 public BounceScrollView(Context context, AttributeSet attrs) { 11 super(context, attrs); 12 } 13 14 /*** 15 * 根據 XML 生成視圖工作完成.該函數在生成視圖的最后調用,在所有子視圖添加完之后. 即使子類覆蓋了 onFinishInflate 16 * 方法,也應該調用父類的方法,使該方法得以執行. 17 */ 18 @Override 19 protected void onFinishInflate() { 20 if (getChildCount() > 0) { 21 inner = getChildAt(0); 22 } 23 } 24 25 /*** 26 * 監聽touch 27 */ 28 @Override 29 public boolean onTouchEvent(MotionEvent ev) { 30 if (inner != null) { 31 commOnTouchEvent(ev); 32 } 33 34 return super.onTouchEvent(ev); 35 } 36 37 /*** 38 * 觸摸事件 39 * 40 * @param ev 41 */ 42 public void commOnTouchEvent(MotionEvent ev) { 43 int action = ev.getAction(); 44 switch (action) { 45 case MotionEvent.ACTION_DOWN: 46 break; 47 case MotionEvent.ACTION_UP: 48 // 手指松開. 49 if (isNeedAnimation()) { 50 animation(); 51 isCount = false; 52 } 53 break; 54 /*** 55 * 排除出第一次移動計算,因為第一次無法得知y坐標, 在MotionEvent.ACTION_DOWN中獲取不到, 56 * 因為此時是MyScrollView的touch事件傳遞到到了LIstView的孩子item上面.所以從第二次計算開始. 57 * 然而我們也要進行初始化,就是第一次移動的時候讓滑動距離歸0. 之后記錄準確了就正常執行. 58 */ 59 case MotionEvent.ACTION_MOVE: 60 final float preY = y;// 按下時的y坐標 61 float nowY = ev.getY();// 時時y坐標 62 int deltaY = (int) (preY - nowY);// 滑動距離 63 if (!isCount) { 64 deltaY = 0; // 在這里要歸0. 65 } 66 67 y = nowY; 68 // 當滾動到最上或者最下時就不會再滾動,這時移動布局 69 if (isNeedMove()) { 70 // 初始化頭部矩形 71 if (normal.isEmpty()) { 72 // 保存正常的布局位置 73 normal.set(inner.getLeft(), inner.getTop(), 74 inner.getRight(), inner.getBottom()); 75 } 76 // Log.e("jj", "矩形:" + inner.getLeft() + "," + inner.getTop() 77 // + "," + inner.getRight() + "," + inner.getBottom()); 78 // 移動布局 79 inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, 80 inner.getRight(), inner.getBottom() - deltaY / 2); 81 } 82 isCount = true; 83 break; 84 85 default: 86 break; 87 } 88 } 89 90 /*** 91 * 回縮動畫 92 */ 93 public void animation() { 94 // 開啟移動動畫 95 TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(), normal.top); 96 ta.setDuration(200); 97 inner.startAnimation(ta); 98 // 設置回到正常的布局位置 99 inner.layout(normal.left, normal.top, normal.right, normal.bottom); 100 101 // Log.e("jj", "回歸:" + normal.left + "," + normal.top + "," + normal.right 102 // + "," + normal.bottom); 103 104 normal.setEmpty(); 105 106 } 107 108 // 是否需要開啟動畫 109 public boolean isNeedAnimation() { 110 return !normal.isEmpty(); 111 } 112 113 /*** 114 * 是否需要移動布局 inner.getMeasuredHeight():獲取的是控件的總高度 115 * 116 * getHeight():獲取的是屏幕的高度 117 * 118 * @return 119 */ 120 public boolean isNeedMove() { 121 int offset = inner.getMeasuredHeight() - getHeight(); 122 int scrollY = getScrollY(); 123 // Log.e("jj", "scrolly=" + scrollY); 124 // 0是頂部,后面那個是底部 125 if (scrollY == 0 || scrollY == offset) { 126 return true; 127 } 128 return false; 129 } 130 131 }

?

此View用于,在沒有充滿父級的時候,下拉會有反彈效果。但是Android自帶的ScrollView就不可以。

?

轉載于:https://www.cnblogs.com/royi123/p/4257788.html

總結

以上是生活随笔為你收集整理的ScrollView反弹效果的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。