android 自訂滾動上下回彈scollView

來源:互聯網
上載者:User

標籤:android   android自訂view   

這是一個自訂view,在xml布局中用這個view嵌套要使之可以上下回彈的view,

就能實現布局可以滾動上下回彈了,自訂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);}@Overrideprotected void onFinishInflate() {if (getChildCount() > 0) {inner = getChildAt(0);}}@Overridepublic boolean onInterceptTouchEvent(MotionEvent ev) {return super.onInterceptTouchEvent(ev);}@Overridepublic 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() {@Overridepublic void onAnimationStart(Animation animation) {animationFinish = false;}@Overridepublic void onAnimationRepeat(Animation animation) {}@Overridepublic 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 自訂滾動上下回彈scollView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.