日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

android自定義滾動(dòng)上下回彈scollView_Android

作者:夢(mèng)天2015 ? 更新時(shí)間: 2022-06-18 編程語(yǔ)言

本文實(shí)例為大家分享了android自定義滾動(dòng)上下回彈scollView的具體代碼,供大家參考,具體內(nèi)容如下

這是一個(gè)自定義view,在xml布局中用這個(gè)view嵌套要使之可以上下回彈的view

就能實(shí)現(xiàn)布局可以滾動(dòng)上下回彈了,自定義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);
?? ??? ??? ??? ?// 滾動(dòng)
?? ??? ??? ??? ?// scrollBy(0, deltaY);
?
?? ??? ??? ??? ?y = nowY;
?? ??? ??? ??? ?// 當(dāng)滾動(dòng)到最上或者最下時(shí)就不會(huì)再滾動(dòng),這時(shí)移動(dòng)布局
?? ??? ??? ??? ?if (isNeedMove()) {
?? ??? ??? ??? ??? ?if (normal.isEmpty()) {
?? ??? ??? ??? ??? ??? ?// 保存正常的布局位置
?? ??? ??? ??? ??? ??? ?normal.set(inner.getLeft(), inner.getTop(), inner.getRight(), inner.getBottom());
?? ??? ??? ??? ??? ?}
?? ??? ??? ??? ??? ?// 移動(dòng)布局
?? ??? ??? ??? ??? ?inner.layout(inner.getLeft(), inner.getTop() - deltaY / 2, inner.getRight(), inner.getBottom() - deltaY / 2);
?? ??? ??? ??? ?} else {
?? ??? ??? ??? ??? ?super.onTouchEvent(ev);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?break;
?? ??? ??? ?default:
?? ??? ??? ??? ?break;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?
?? ?// 開(kāi)啟動(dòng)畫(huà)移動(dòng)
?
?? ?private void animation() {
?? ??? ?// 開(kāi)啟移動(dòng)動(dòng)畫(huà)
?? ??? ?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();
?? ??? ??? ??? ?// 設(shè)置回到正常的布局位置
?? ??? ??? ??? ?inner.layout(normal.left, normal.top, normal.right, normal.bottom);
?? ??? ??? ??? ?normal.setEmpty();
?? ??? ??? ??? ?animationFinish = true;
?? ??? ??? ?}
?? ??? ?});
?? ??? ?inner.startAnimation(ta);
?? ?}
?
?? ?// 是否需要開(kāi)啟動(dòng)畫(huà)
?? ?private boolean isNeedAnimation() {
?? ??? ?return !normal.isEmpty();
?? ?}
?
?? ?// 是否需要移動(dòng)布局
?? ?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>

那么包含的這個(gè)textview可以上下滾動(dòng)并且回彈了

原文鏈接:https://blog.csdn.net/u014763302/article/details/45957657

欄目分類(lèi)
最近更新