android自定義滾動上下回彈scollView
更新時間:2022年04月19日 10:04:41 作者:夢天2015
這篇文章主要為大家詳細介紹了android自定義滾動上下回彈scollView,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了android自定義滾動上下回彈scollView的具體代碼,供大家參考,具體內容如下
這是一個自定義view,在xml布局中用這個view嵌套要使之可以上下回彈的view
就能實現(xiàn)布局可以滾動上下回彈了,自定義view代碼如下:
package com.loopfire.meitaotao.view.scrollView;
?
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.TranslateAnimation;
import android.widget.ScrollView;
?
/**
?* 上下回彈 scollView
?*?
?* @author Administrator
?*?
?*/
public class MyScrollView extends ScrollView {
?? ?private View inner;
?? ?private float y;
?? ?private Rect normal = new Rect();
?? ?private boolean animationFinish = true;
?
?? ?public MyScrollView(Context context) {
?? ??? ?super(context);
?? ?}
?
?? ?public MyScrollView(Context context, AttributeSet attrs) {
?? ??? ?super(context, attrs);
?? ?}
?
?? ?@Override
?? ?protected void onFinishInflate() {
?? ??? ?if (getChildCount() > 0) {
?? ??? ??? ?inner = getChildAt(0);
?? ??? ?}
?? ?}
?
?? ?@Override
?? ?public boolean onInterceptTouchEvent(MotionEvent ev) {
?? ??? ?return super.onInterceptTouchEvent(ev);
?? ?}
?
?? ?@Override
?? ?public boolean onTouchEvent(MotionEvent ev) {
?? ??? ?if (inner == null) {
?? ??? ??? ?return super.onTouchEvent(ev);
?? ??? ?} else {
?? ??? ??? ?commOnTouchEvent(ev);
?? ??? ?}
?? ??? ?return super.onTouchEvent(ev);
?? ?}
?
?? ?private void commOnTouchEvent(MotionEvent ev) {
?? ??? ?if (animationFinish) {
?? ??? ??? ?int action = ev.getAction();
?? ??? ??? ?switch (action) {
?? ??? ??? ?case MotionEvent.ACTION_DOWN:
?? ??? ??? ??? ?// System.out.println("ACTION_DOWN");
?? ??? ??? ??? ?y = ev.getY();
?? ??? ??? ??? ?super.onTouchEvent(ev);
?? ??? ??? ??? ?break;
?? ??? ??? ?case MotionEvent.ACTION_UP:
?? ??? ??? ??? ?// System.out.println("ACTION_UP");
?? ??? ??? ??? ?y = 0;
?? ??? ??? ??? ?if (isNeedAnimation()) {
?? ??? ??? ??? ??? ?animation();
?? ??? ??? ??? ?}
?? ??? ??? ??? ?super.onTouchEvent(ev);
?? ??? ??? ??? ?break;
?? ??? ??? ?case MotionEvent.ACTION_MOVE:
?? ??? ??? ??? ?// System.out.println("ACTION_MOVE");
?? ??? ??? ??? ?final float preY = y == 0 ? ev.getY() : y;
?? ??? ??? ??? ?float nowY = ev.getY();
?? ??? ??? ??? ?int deltaY = (int) (preY - nowY);
?? ??? ??? ??? ?// 滾動
?? ??? ??? ??? ?// scrollBy(0, deltaY);
?
?? ??? ??? ??? ?y = nowY;
?? ??? ??? ??? ?// 當滾動到最上或者最下時就不會再滾動,這時移動布局
?? ??? ??? ??? ?if (isNeedMove()) {
?? ??? ??? ??? ??? ?if (normal.isEmpty()) {
?? ??? ??? ??? ??? ??? ?// 保存正常的布局位置
?? ??? ??? ??? ??? ??? ?normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?// 移動布局
?? ??? ??? ??? ??? ?inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, inner.getRight(), inner.getBottom() - deltaY / 2);
?? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ?super.onTouchEvent(ev);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?default:
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?
?? ?// 開啟動畫移動
?
?? ?private void animation() {
?? ??? ?// 開啟移動動畫
?? ??? ?TranslateAnimation ta = new TranslateAnimation(0, 0, 0, normal.top - inner.getTop());
?? ??? ?ta.setDuration(200);
?? ??? ?ta.setAnimationListener(new AnimationListener() {
?? ??? ??? ?@Override
?? ??? ??? ?public void onAnimationStart(Animation animation) {
?? ??? ??? ??? ?animationFinish = false;
?
?? ??? ??? ?}
?
?? ??? ??? ?@Override
?? ??? ??? ?public void onAnimationRepeat(Animation animation) {
?
?? ??? ??? ?}
?
?? ??? ??? ?@Override
?? ??? ??? ?public void onAnimationEnd(Animation animation) {
?? ??? ??? ??? ?inner.clearAnimation();
?? ??? ??? ??? ?// 設置回到正常的布局位置
?? ??? ??? ??? ?inner.layout(normal.left, normal.top, normal.right, normal.bottom);
?? ??? ??? ??? ?normal.setEmpty();
?? ??? ??? ??? ?animationFinish = true;
?? ??? ??? ?}
?? ??? ?});
?? ??? ?inner.startAnimation(ta);
?? ?}
?
?? ?// 是否需要開啟動畫
?? ?private boolean isNeedAnimation() {
?? ??? ?return !normal.isEmpty();
?? ?}
?
?? ?// 是否需要移動布局
?? ?private boolean isNeedMove() {
?? ??? ?int offset = inner.getMeasuredHeight() - getHeight();
?? ??? ?int scrollY = getScrollY();
?? ??? ?if (scrollY == 0 || scrollY == offset) {
?? ??? ??? ?return true;
?? ??? ?}
?? ??? ?return false;
?? ?}
?
}在xml中使用如下:
<com.loopfire.meitaotao.view.scrollView.MyScrollView> ? ? ? ? ? <TextView ? ? ? ? ? ? ? style="@style/form_left_text_style" ? ? ? ? ? ? ? android:layout_width="wrap_content" ? ? ? ? ? ? ? android:layout_height="wrap_content" ? ? ? ? ? ? ? android:layout_marginLeft="@dimen/text_margin_left2" ? ? ? ? ? ? ? android:text="@string/about" /> </com.loopfire.meitaotao.view.scrollView.MyScrollView>
那么包含的這個textview可以上下滾動并且回彈了
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
Android中Bitmap、File與Uri之間的簡單記錄
這篇文章主要給大家介紹了關于Android中Bitmap、File與Uri之間的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧2019-02-02
Android中TimePicker與DatePicker時間日期選擇組件的使用實例
這篇文章主要介紹了Android中TimePicker時間選擇與DatePicker日期選擇組件的使用實例,這兩個組件加上去的效果就是我們平時在iOS上設置鬧鐘時調整時間類似的滾動選項,需要的朋友可以參考下2016-04-04
Android實現(xiàn)鍵盤彈出界面上移的實現(xiàn)思路
這篇文章主要介紹了Android實現(xiàn)鍵盤彈出界面上移的實現(xiàn)思路,需要的朋友可以參考下2018-04-04
Android OpenGL ES 實現(xiàn)抖音傳送帶特效(原理解析)
這篇文章主要介紹了Android OpenGL ES 實現(xiàn)抖音傳送帶特效,抖音傳送帶特效推出已經(jīng)很長一段時間了,前面也實現(xiàn)了下,最近把它整理出來了,如果你有仔細觀測傳送帶特效,就會發(fā)現(xiàn)它的實現(xiàn)原理其實很簡單,需要的朋友可以參考下2022-07-07

