iis服务器助手广告广告
返回顶部
首页 > 资讯 > 精选 >android ScrollView怎么实现水平滑动回弹
  • 665
分享到

android ScrollView怎么实现水平滑动回弹

2023-06-30 05:06:26 665人浏览 安东尼
摘要

这篇文章主要介绍“Android ScrollView怎么实现水平滑动回弹”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“android ScrollView怎么实现水平滑动回弹”

这篇文章主要介绍“Android ScrollView怎么实现水平滑动回弹”的相关知识,小编通过实际案例向大家展示操作过程,操作方法简单快捷,实用性强,希望这篇“android ScrollView怎么实现水平滑动回弹”文章能帮助大家解决问题。

效果图:

android 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.TranslateAnimation;import android.widget.HorizontalScrollView; public class MHorizontalScrollView extends HorizontalScrollView {     private View view;        private static final int deltaX = 1;    private Rect nORMalRt = new Rect();     public MHorizontalScrollView(Context context) {        super(context);    }     public MHorizontalScrollView(Context context, AttributeSet attrs) {        super(context, attrs);    }         protected void onFinishInflate() {        if (getChildCount() > 0) {            view = getChildAt(0);        }    }     @Override    public boolean onTouchEvent(MotionEvent event) {        if (view != null) {            onTouchEventImpl(event);        }        return super.onTouchEvent(event);    }     private void onTouchEventImpl(MotionEvent event) {        switch (event.getAction()) {        case MotionEvent.ACTION_MOVE:            // 在当前视图内容继续偏移(x , y)个单位,显示(可视)区域也跟着偏移(x,y)个单位            scrollBy(deltaX, 0);            // 当滚动到最左或最右时就不会再滚动,这时移动布局达到回弹效果            if (isLayoutMove()) {                if (normalRt.isEmpty()) {                    // 保存当前正常的布局位置,拉过头才能回弹到正常位置                    normalRt.set(view.getLeft(), view.getTop(),                            view.getRight(), view.getBottom());                 }                // 移动布局                view.layout(view.getLeft() - deltaX, view.getTop(),                        view.getRight() - deltaX, view.getBottom());            }            break;        case MotionEvent.ACTION_UP:            if (isNeedAnimation()) {                animationImpl();            }            break;        default:            break;        }    }         private void animationImpl() {        // 移动动画        TranslateAnimation ta = new TranslateAnimation(view.getLeft(),                normalRt.left, 0, 0);        // 动画持续时间        ta.setDuration(50);        view.startAnimation(ta);        // 设置回到当前正常的布局位置        view.layout(normalRt.left, normalRt.top, normalRt.right,                normalRt.bottom);        normalRt.setEmpty();    }         private boolean isNeedAnimation() {        return !normalRt.isEmpty();    }         private boolean isLayoutMove() {        int offset = view.getMeasuredWidth() - getWidth();        if (offset <= 0) {            return false;        }        // 上面已固定deltaX=1,scrollX永远等于1所以向右拉不动        // 但当向左拉动到内容布局的最右端时scrollX == offset时还可以继续拉动        int scrollX = getScrollX();        if (scrollX == 0 || scrollX == offset) {            return true;        }        return false;    } }

在xml布局文件里直接使用:

<LinearLayout xmlns:android="Http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"     android:background="@drawable/background"    >     <cn.qhg.MHorizontalScrollView        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:scrollbars="none"         >         <LinearLayout            android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:orientation="horizontal"             android:paddingTop="100dp"            android:id="@+id/ll_test"            android:onClick="test"            >             <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/house_1"                 android:layout_marginRight="40dp"                />             <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/house_4"                 android:layout_marginRight="40dp"                />             <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/house_2"                android:layout_marginRight="40dp"                 />             <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/house_3"                android:layout_marginRight="40dp"                 />            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/house_1"                 android:layout_marginRight="40dp"                />             <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/house_4"                 android:layout_marginRight="40dp"                />             <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/house_2"                android:layout_marginRight="40dp"                 />             <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/house_3"                android:layout_marginRight="40dp"                 />            <!-- 使右边多空一点 -->            <ImageView                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_marginRight="20dp"                 />        </LinearLayout>    </cn.qhg.MHorizontalScrollView> </LinearLayout>

关于“android ScrollView怎么实现水平滑动回弹”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识,可以关注编程网精选频道,小编每天都会为大家更新不同的知识点。

--结束END--

本文标题: android ScrollView怎么实现水平滑动回弹

本文链接: https://www.lsjlt.com/news/327460.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • C++ 生态系统中流行库和框架的贡献指南
    作为 c++++ 开发人员,通过遵循以下步骤即可为流行库和框架做出贡献:选择一个项目并熟悉其代码库。在 issue 跟踪器中寻找适合初学者的问题。创建一个新分支,实现修复并添加测试。提交...
    99+
    2024-05-15
    框架 c++ 流行库 git
  • C++ 生态系统中流行库和框架的社区支持情况
    c++++生态系统中流行库和框架的社区支持情况:boost:活跃的社区提供广泛的文档、教程和讨论区,确保持续的维护和更新。qt:庞大的社区提供丰富的文档、示例和论坛,积极参与开发和维护。...
    99+
    2024-05-15
    生态系统 社区支持 c++ overflow 标准库
  • c++中if elseif使用规则
    c++ 中 if-else if 语句的使用规则为:语法:if (条件1) { // 执行代码块 1} else if (条件 2) { // 执行代码块 2}// ...else ...
    99+
    2024-05-15
    c++
  • c++中的继承怎么写
    继承是一种允许类从现有类派生并访问其成员的强大机制。在 c++ 中,继承类型包括:单继承:一个子类从一个基类继承。多继承:一个子类从多个基类继承。层次继承:多个子类从同一个基类继承。多层...
    99+
    2024-05-15
    c++
  • c++中如何使用类和对象掌握目标
    在 c++ 中创建类和对象:使用 class 关键字定义类,包含数据成员和方法。使用对象名称和类名称创建对象。访问权限包括:公有、受保护和私有。数据成员是类的变量,每个对象拥有自己的副本...
    99+
    2024-05-15
    c++
  • c++中优先级是什么意思
    c++ 中的优先级规则:优先级高的操作符先执行,相同优先级的从左到右执行,括号可改变执行顺序。操作符优先级表包含从最高到最低的优先级列表,其中赋值运算符具有最低优先级。通过了解优先级,可...
    99+
    2024-05-15
    c++
  • c++中a+是什么意思
    c++ 中的 a+ 运算符表示自增运算符,用于将变量递增 1 并将结果存储在同一变量中。语法为 a++,用法包括循环和计数器。它可与后置递增运算符 ++a 交换使用,后者在表达式求值后递...
    99+
    2024-05-15
    c++
  • c++中a.b什么意思
    c++kquote>“a.b”表示对象“a”的成员“b”,用于访问对象成员,可用“对象名.成员名”的语法。它还可以用于访问嵌套成员,如“对象名.嵌套成员名.成员名”的语法。 c++...
    99+
    2024-05-15
    c++
  • C++ 并发编程库的优缺点
    c++++ 提供了多种并发编程库,满足不同场景下的需求。线程库 (std::thread) 易于使用但开销大;异步库 (std::async) 可异步执行任务,但 api 复杂;协程库 ...
    99+
    2024-05-15
    c++ 并发编程
  • 如何在 Golang 中备份数据库?
    在 golang 中备份数据库对于保护数据至关重要。可以使用标准库中的 database/sql 包,或第三方包如 github.com/go-sql-driver/mysql。具体步骤...
    99+
    2024-05-15
    golang 数据库备份 mysql git 标准库
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作