以编程方式更改滑动时的 ViewPager 动画持续时间

2024-04-08

我正在使用以下代码更改幻灯片:

viewPager.setCurrentItem(index++, true);

但变化太快了。有没有办法手动设置动画速度?


I've wanted to do myself and have achieved a solution (using reflection, however). I haven't tested it yet but it should work or need minimal modification. Tested on Galaxy Nexus JB 4.2.1. You need to use a ViewPagerCustomDuration in your XML instead of ViewPager, and then you can do this:

ViewPagerCustomDuration vp = (ViewPagerCustomDuration) findViewById(R.id.myPager);
vp.setScrollDurationFactor(2); // make the animation twice as slow

ViewPagerCustomDuration.java:

import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.animation.Interpolator;

import java.lang.reflect.Field;

public class ViewPagerCustomDuration extends ViewPager {

    public ViewPagerCustomDuration(Context context) {
        super(context);
        postInitViewPager();
    }

    public ViewPagerCustomDuration(Context context, AttributeSet attrs) {
        super(context, attrs);
        postInitViewPager();
    }

    private ScrollerCustomDuration mScroller = null;

    /**
     * Override the Scroller instance with our own class so we can change the
     * duration
     */
    private void postInitViewPager() {
        try {
            Field scroller = ViewPager.class.getDeclaredField("mScroller");
            scroller.setAccessible(true);
            Field interpolator = ViewPager.class.getDeclaredField("sInterpolator");
            interpolator.setAccessible(true);

            mScroller = new ScrollerCustomDuration(getContext(),
                    (Interpolator) interpolator.get(null));
            scroller.set(this, mScroller);
        } catch (Exception e) {
        }
    }

    /**
     * Set the factor by which the duration will change
     */
    public void setScrollDurationFactor(double scrollFactor) {
        mScroller.setScrollDurationFactor(scrollFactor);
    }

}

ScrollerCustomDuration.java:

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.animation.Interpolator;
import android.widget.Scroller;

public class ScrollerCustomDuration extends Scroller {

    private double mScrollFactor = 1;

    public ScrollerCustomDuration(Context context) {
        super(context);
    }

    public ScrollerCustomDuration(Context context, Interpolator interpolator) {
        super(context, interpolator);
    }

    @SuppressLint("NewApi")
    public ScrollerCustomDuration(Context context, Interpolator interpolator, boolean flywheel) {
        super(context, interpolator, flywheel);
    }

    /**
     * Set the factor by which the duration will change
     */
    public void setScrollDurationFactor(double scrollFactor) {
        mScrollFactor = scrollFactor;
    }

    @Override
    public void startScroll(int startX, int startY, int dx, int dy, int duration) {
        super.startScroll(startX, startY, dx, dy, (int) (duration * mScrollFactor));
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

以编程方式更改滑动时的 ViewPager 动画持续时间 的相关文章

随机推荐

  • 在 Qt 中将多个小部件合并为一个

    我反复使用一对QComboBox and QListWidget在一个项目中 它们的交互是高度耦合的 当在组合框中选择一个项目时 列表会以某种方式进行过滤 我在多个对话框实现中复制粘贴这两个小部件之间的所有信号和插槽连接 我认为这不是一个好
  • 什么是杂注头?缓存页面..和 IE

    因此 我在 php 中发送标头来缓存我的页面 这也集成到我们的 CDN contendo akamai 中 我总是使用这个 pragma cache header 我见过各种例子也使用它 然而 我刚刚检查了 fiddler 来测试我们开发的
  • PHP $_FILES MIME 类型返回 null

    现在我正在尝试编写一个脚本 该脚本仅接受某些音频文件上传到服务器 但是 某些 MIME 类型将返回为 null 这是一些代码片段 PHP allowedExt array audio mp4a latm if isset POST prin
  • 项目同时使用MySQL和PostgreSQL时从JsonStringType切换为JsonBinaryType

    当需要从 PostgreSQL 切换到 MariaDB MySql 时 我遇到了 json 列的问题 我使用 Spring Boot JPA Hibernate hibernate types 52 我想要映射的表是这样的 CREATE T
  • BreakIterator 在 Android 中如何工作?

    我正在 Android 中制作自己的文本处理器 蒙古语的自定义垂直脚本 TextView 我以为我必须自己找到所有换行位置 以便我可以实现换行 但后来我发现BreakIterator https developer android com
  • 如何根据另一个字段的值禁止 TFS 要求工作项中的状态从“建议”更改为“活动”?

    I ve added department approvals to the standard CMMI Template Requirement work item I d like to limit the System State f
  • iOS 通知服务扩展会从设备中删除附加文件吗?

    我遇到了一个奇怪的问题 iOS 通知服务扩展将从设备中删除附件 我使用 SDWebImage 来显示和缓存图像 并实现了通知服务扩展以在通知警报视图中显示图像 就我而言 图像已在本地缓存 然后 我单击主页按钮 我的应用程序在后台运行 应用程
  • 如何将远程 Git 存储库添加到 Ubuntu 服务器?

    我在我的桌面计算机 Windows 7 上创建了一个 Git 存储库 git init git add
  • 如何运行 Flutter 脚本

    我正在尝试对 flutter 库进行一些基准测试 但是我不知道如何运行需要 flutter 库的脚本 我能够做到这一点的唯一方法是将其作为测试代码运行 但是我没有找到在测试模式下禁用断言的方法 Works flutter test mySc
  • 如何实现 Gmail 风格的标签选择器?

    实现 类似 Gmail 标签邮件界面的最简单方法是什么 有没有 JavaScript 库有这样的小部件 http img294 imageshack us img294 7097 36698396 png http img294 image
  • 在没有游标的情况下合并单个 SQL 表中的数据

    我有一个包含 ID 列的表和另一个包含数字的列 一个ID可以有多个号码 例如 ID Number 1 25 1 26 1 30 1 24 2 4 2 8 2 5 现在根据这些数据 在一个新表中 我想要这个 ID Low High 1 24
  • 在没有按钮连接的情况下以编程方式执行 Segue?

    我的故事板中有两个视图控制器 我需要从视图 1 推送到视图 2 我需要在不直接从故事板中的按钮连接转场的情况下执行此操作 我需要以编程方式执行此操作 我怎么能够 Thanks 当您单击按钮时调用 self performSegueWithI
  • VBScript 宏 getParentFolder 名称

    我正在尝试创建一个 vbscript 宏 它将获取存储宏的文件夹位置并将输出文件创建到同一文件夹中 我正在使用下面的代码 但它没有获得正确的位置 Set obj1FSO CreateObject Scripting FileSystemOb
  • 跨包模块设置日志记录的有效方法

    我有一个包 其中包含多个组件 这些组件将从使用日志记录和输出有用信息中受益匪浅 我不想做的是为每个文件 设置 正确的日志记录 并在以下位置进行 import logging logging basicConfig level DEBUG m
  • Boost:为什么 ~/user-config.jam 中列出的工具集不可用于 ./b2?

    在我试图回答我自己的问题时Clang 链接器报告 未找到符号 尽管 nm m 显示该名称存在于正在链接的库中 https stackoverflow com questions 20599721 clang linker reports s
  • window.onbeforeunload 执行查询

    我试图在用户离开页面时执行后查询 我正在使用的代码是 我在这里做的事情有什么问题吗 我在 FF 错误控制台中得到的结果只是说其他不相关的函数 变量未定义 因为它们正在卸载 对于我需要修复的问题有什么提示或指示吗 简单的答案是你不能在中进行异
  • 使用 sql 通过 csv 将产品导入到 woocommerce

    当sql表post meta不断增长时 通过插件wp all import导入产品会花费太多时间 目前有200 000种产品进口 有没有办法直接在sql中通过csv导入产品而不需要wordpress 我不需要导入任何图像 只需导入标题和描述
  • 应用程序图标创建叠加信息(数字)?

    如何在 Android 应用程序上克隆此行为 iOS 从技术上讲 这绝对是可能的 因为我在 Android 手机上有一个自己的应用程序 它是一个电子邮件应用程序 图标上有一个非常相似的指示器 显示未读邮件数量 是的 你可以像 ios 一样实
  • 用户输入-DOS批处理文件

    我得到一个bat文件 如下所示 ECHO Executing scripts PAUSE for X in SQL do SQLCMD S localhost d CTL I i X gt gt ResultScript txt pause
  • 以编程方式更改滑动时的 ViewPager 动画持续时间

    我正在使用以下代码更改幻灯片 viewPager setCurrentItem index true 但变化太快了 有没有办法手动设置动画速度 I ve wanted to do myself and have achieved a sol