国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android開發實現帶有反彈效果仿IOS反彈scrollview教程詳解

2019-12-12 05:20:25
字體:
來源:轉載
供稿:網友

首先給大家看一下我們今天這個最終實現的效果圖:

這里寫圖片描述 

這個是ios中的反彈效果。當然我們安卓中如果想要實現這種效果,感覺不會那么生硬,滾動到底部或者頂部的時候。當然

使用scrollview是無法實現的。所以我們需要新建一個view繼承ScrollView

package davidbouncescrollview.qq986945193.com.davidbouncescrollview;import android.annotation.SuppressLint;import android.content.Context;import android.graphics.Rect;import android.util.AttributeSet;import android.view.MotionEvent;import android.view.View;import android.view.animation.TranslateAnimation;import android.widget.ScrollView;/*** @author :程序員小冰* @新浪微博 :http://weibo.com/mcxiaobing* @GitHub:https://github.com/QQ986945193* @CSDN博客: http://blog.csdn.net/qq_21376985* @交流Qq :986945193* 類名:帶有反彈效果的scrollview*/public class BounceScrollView extends ScrollView {private View inner;// 孩子Viewprivate float y;// 點擊時y坐標private Rect normal = new Rect();// 矩形(這里只是個形式,只是用于判斷是否需要動畫.)private boolean isCount = false;// 是否開始計算public BounceScrollView(Context context, AttributeSet attrs) {super(context, attrs);}/**** 根據 XML 生成視圖工作完成.該函數在生成視圖的最后調用,在所有子視圖添加完之后. 即使子類覆蓋了 onFinishInflate* 方法,也應該調用父類的方法,使該方法得以執行.*/@SuppressLint("MissingSuperCall")@Overrideprotected void onFinishInflate() {if (getChildCount() > 0) {inner = getChildAt(0);}}/**** 監聽touch*/@Overridepublic boolean onTouchEvent(MotionEvent ev) {if (inner != null) {commOnTouchEvent(ev);}return super.onTouchEvent(ev);}/**** 觸摸事件** @param ev*/public void commOnTouchEvent(MotionEvent ev) {int action = ev.getAction();switch (action) {case MotionEvent.ACTION_DOWN:break;case MotionEvent.ACTION_UP:// 手指松開.if (isNeedAnimation()) {animation();isCount = false;}break;/**** 排除出第一次移動計算,因為第一次無法得知y坐標, 在MotionEvent.ACTION_DOWN中獲取不到,* 因為此時是MyScrollView的touch事件傳遞到到了LIstView的孩子item上面.所以從第二次計算開始.* 然而我們也要進行初始化,就是第一次移動的時候讓滑動距離歸0. 之后記錄準確了就正常執行.* https://github.com/QQ986945193*/case MotionEvent.ACTION_MOVE:final float preY = y;// 按下時的y坐標float nowY = ev.getY();// 時時y坐標int deltaY = (int) (preY - nowY);// 滑動距離if (!isCount) {deltaY = 0; // 在這里要歸0.}y = nowY;// 當滾動到最上或者最下時就不會再滾動,這時移動布局if (isNeedMove()) {// 初始化頭部矩形if (normal.isEmpty()) {// 保存正常的布局位置normal.set(inner.getLeft(), inner.getTop(),inner.getRight(), inner.getBottom());}// Log.e("jj", "矩形:" + inner.getLeft() + "," + inner.getTop()// + "," + inner.getRight() + "," + inner.getBottom());// 移動布局inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2,inner.getRight(), inner.getBottom() - deltaY / 2);}isCount = true;break;default:break;}}/**** 回縮動畫*/public void animation() {// 開啟移動動畫TranslateAnimation ta = new TranslateAnimation(0, 0, inner.getTop(),normal.top);ta.setDuration(200);inner.startAnimation(ta);// 設置回到正常的布局位置inner.layout(normal.left, normal.top, normal.right, normal.bottom);// Log.e("jj", "回歸:" + normal.left + "," + normal.top + "," + normal.right// + "," + normal.bottom);normal.setEmpty();}// 是否需要開啟動畫public boolean isNeedAnimation() {return !normal.isEmpty();}/**** 是否需要移動布局 inner.getMeasuredHeight():獲取的是控件的總高度* <p/>* getHeight():獲取的是屏幕的高度* <p/>* https://github.com/QQ986945193** @return*/public boolean isNeedMove() {int offset = inner.getMeasuredHeight() - getHeight();int scrollY = getScrollY();// Log.e("jj", "scrolly=" + scrollY);// 0是頂部,后面那個是底部if (scrollY == 0 || scrollY == offset) {return true;}return false;}}

然后他的用法就是和ScrollView用法一樣了。比如直接在布局中引用:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context="davidbouncescrollview.qq986945193.com.davidbouncescrollview.MainActivity"><davidbouncescrollview.qq986945193.com.davidbouncescrollview.BounceScrollViewandroid:layout_width="match_parent"android:layout_height="match_parent"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@android:color/black" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="大家好,我是程序員小冰" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@android:color/black" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="大家好,我是程序員小冰" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@android:color/black" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="大家好,我是程序員小冰" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@android:color/black" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="大家好,我是程序員小冰" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@android:color/black" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="大家好,我是程序員小冰" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@android:color/black" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="大家好,我是程序員小冰" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@android:color/black" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="大家好,我是程序員小冰" /><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:background="@android:color/black" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:text="大家好,我是程序員小冰" /></LinearLayout></davidbouncescrollview.qq986945193.com.davidbouncescrollview.BounceScrollView></LinearLayout>

最后直接運行即可看到上面的效果。

(AndroidStudio版)github下載地址:

https://github.com/QQ986945193/DavidBounceScrollView

以上所述是小編給大家介紹的Android開發實現帶有反彈效果仿IOS反彈scrollview教程詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對武林網網站的支持!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 江华| 北流市| 龙州县| 太仓市| 光山县| 台东县| 淮北市| 彭州市| 西畴县| 山西省| 大石桥市| 汉川市| 犍为县| 麻城市| 肥乡县| 泸水县| 汉源县| 珲春市| 合作市| 蒙城县| 调兵山市| 商洛市| 来安县| 绥宁县| 隆昌县| 富平县| 吉木乃县| 保德县| 武陟县| 东台市| 新源县| 米泉市| 夏津县| 安新县| 汕尾市| 司法| 黑龙江省| 黑龙江省| 无锡市| 那曲县| 兖州市|