Android ScrollView實現(xiàn)反彈效果的實例
更新時間:2017年07月22日 16:21:50 投稿:lqh
這篇文章主要介紹了 Android ScrollView實現(xiàn)反彈效果的實例的相關(guān)資料,這里自定義scrollview 并實現(xiàn)反彈效果,需要的朋友可以參考下
Android ScrollView實現(xiàn)反彈效果
自定義ScrollView控件:
/**
* ScrollView反彈效果的實現(xiàn)
*/
public class BounceScrollView extends ScrollView {
private View inner;// 孩子View
private float y;// 點擊時y坐標(biāo)
private Rect normal = new Rect();// 矩形(這里只是個形式,只是用于判斷是否需要動畫.)
private boolean isCount = false;// 是否開始計算
public BounceScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/***
* 根據(jù) XML 生成視圖工作完成.該函數(shù)在生成視圖的最后調(diào)用,在所有子視圖添加完之后. 即使子類覆蓋了 onFinishInflate
* 方法,也應(yīng)該調(diào)用父類的方法,使該方法得以執(zhí)行.
*/
@Override
protected void onFinishInflate() {
if (getChildCount() > 0) {
inner = getChildAt(0);
}
}
/***
* 監(jiān)聽touch
*/
@Override
public 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坐標(biāo), 在MotionEvent.ACTION_DOWN中獲取不到,
* 因為此時是MyScrollView的touch事件傳遞到到了LIstView的孩子item上面.所以從第二次計算開始.
* 然而我們也要進(jìn)行初始化,就是第一次移動的時候讓滑動距離歸0. 之后記錄準(zhǔn)確了就正常執(zhí)行.
*/
case MotionEvent.ACTION_MOVE:
final float preY = y;// 按下時的y坐標(biāo)
float nowY = ev.getY();// 時時y坐標(biāo)
int deltaY = (int) (preY - nowY);// 滑動距離
if (!isCount) {
deltaY = 0; // 在這里要歸0.
}
y = nowY;
// 當(dāng)滾動到最上或者最下時就不會再滾動,這時移動布局
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);
// 設(shè)置回到正常的布局位置
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():獲取的是控件的總高度
*
* getHeight():獲取的是屏幕的高度
*
* @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;
}
}
實現(xiàn)反彈效果:
<com.techrare.view.BounceScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/tab_chart_bg"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp" >
<span style="white-space:pre"> </span><!-- 這里可以盡情的布局 -->
</LinearLayout>
</com.techrare.view.BounceScrollView>
以上就是 Android ScrollView實現(xiàn)反彈效果的實例的講解,本站關(guān)于Android開發(fā)的文章還有很多,歡迎大家搜索查閱,感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
您可能感興趣的文章:
相關(guān)文章
android studio使用SQLiteOpenHelper()建立數(shù)據(jù)庫的方法
這篇文章主要介紹了android studio使用SQLiteOpenHelper()建立數(shù)據(jù)庫的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-03-03
flutter 輸入框組件TextField的實現(xiàn)代碼
這篇文章主要介紹了flutter 輸入框組件TextField的實現(xiàn)代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07
Android開發(fā)實現(xiàn)的簡單五子棋游戲示例
這篇文章主要介紹了Android開發(fā)實現(xiàn)的簡單五子棋游戲,結(jié)合實例形式分析了Android實現(xiàn)五子棋游戲功能的布局、游戲功能等具體實現(xiàn)步驟與相關(guān)算法實現(xiàn)技巧,需要的朋友可以參考下2017-12-12
Android實現(xiàn)計算器(計算表達(dá)式/計算小數(shù)點以及括號)
這篇文章主要為大家詳細(xì)介紹了Android實現(xiàn)計算器功能,計算表達(dá)式,能計算小數(shù)點以及括號,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-09-09

