- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我正在阅读一个关于自定义 ViewPager
的滑动页面动画的示例,该动画需要将页面 (View
) 翻译到一定数量。该示例来自 docs .具体来说,它是关于一个名为 ZoomOutPageTransformer
的实现,它可以通过 setPageTransformer()
在 ViewPager
上设置以自定义页面的滑动方式(结合在其中缩放动画)。
最终结果应该是这样的:
现在,他们描述了他们将如何做:
In your implementation of
transformPage()
, you can then create custom slide animations by determining which pages need to be transformed based on the position of the page on the screen, which is obtained from theposition
parameter of thetransformPage()
method.The
position
parameter indicates where a given page is located relative to the center of the screen. It is a dynamic property that changes as the user scrolls through the pages. When a page fills the screen, itsposition
value is 0. When a page is drawn just off the right side of the screen, itsposition
value is 1. If the user scrolls halfway between pages one and two, page one has aposition
of -0.5 and page two has aposition
of 0.5. Based on theposition
of the pages on the screen, you can create custom slide animations by setting page properties with methods such assetAlpha()
,setTranslationX()
, orsetScaleY()
.
这是他们提供的 PageTransformer
的实现:
public class ZoomOutPageTransformer implements ViewPager.PageTransformer {
private static final float MIN_SCALE = 0.85f;
private static final float MIN_ALPHA = 0.5f;
public void transformPage(View view, float position) {
int pageWidth = view.getWidth();
int pageHeight = view.getHeight();
if (position < -1) { // [-Infinity,-1)
// This page is way off-screen to the left.
view.setAlpha(0);
} else if (position <= 1) { // [-1,1]
// Modify the default slide transition to shrink the page as well
float scaleFactor = Math.max(MIN_SCALE, 1 - Math.abs(position));
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
}
// Scale the page down (between MIN_SCALE and 1)
view.setScaleX(scaleFactor);
view.setScaleY(scaleFactor);
// Fade the page relative to its size.
view.setAlpha(MIN_ALPHA +
(scaleFactor - MIN_SCALE) /
(1 - MIN_SCALE) * (1 - MIN_ALPHA));
} else { // (1,+Infinity]
// This page is way off-screen to the right.
view.setAlpha(0);
}
}
}
问题:
我无法理解这个陈述,
view.setTranslationX(horzMargin - vertMargin / 2);
我的理解是,position
参数的值为 1.0 相当于覆盖屏幕宽度,即 w。因此,如果页面中心移动了 x 个单位的 position
,则以像素/dp 为单位的转换将为 w*x。但是他们在计算翻译金额时使用了一些 margin 计算。
谁能解释一下他们是如何进行这个计算的,我的计算有什么问题吗?
最佳答案
您在这里遗漏了一件事,PageTransformer 应用于AFTER它们已定位的 View 。
因此,无论有没有 PageTransformer 附加到 PageView - 当您在页面之间滚动时 - 您只需使用 SNAP 功能进行滚动(就像在 LiseView 中一样)。
PageTransformer 仅在其上添加效果。
所以目的,
float vertMargin = pageHeight * (1 - scaleFactor) / 2;
float horzMargin = pageWidth * (1 - scaleFactor) / 2;
if (position < 0) {
view.setTranslationX(horzMargin - vertMargin / 2);
} else {
view.setTranslationX(-horzMargin + vertMargin / 2);
}
不是移动页面 - 而是补偿页面缩小。
没有它,页面将产生一些丑陋的副作用。尝试一下,删除线条 :) - View 将向右/向左移动,但定位将关闭。
所以,翻译 X 不会移动页面,但是,在这种情况下,只是管理页面间距以改善动画感觉 - 以上是它的数学。它所做的是根据屏幕高度/宽度减少页面之间的空间。首先它取消空格 (horzMargin) 然后它增加一点间距 (- vertMargin/2)
这就是为什么您的计算不好 (w*x) - 您正在尝试移动页面 - 但它已经在移动了。
关于android - 在滑动 ViewPager 中查看翻译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45251758/
这是在我的 MainActivity 中用作 BroadcastReceiver 的代码 mRegistrationBroadcastReceiver = new BroadcastRecei
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
我想在大部分时间隐藏 UISearchBar,只在用户需要时调用它来显示。 我在 Interface Builder 中放置了一个 UISearchBar 并将其隐藏在 View 后面,当用户单击按钮
我有一个包含 CCMenuItemImage 的菜单(“myMenu”)。我希望此菜单能够检测手指滑动并相应地滑动。 我的问题是 CCMenuItemImage 似乎吸收了触摸事件。当用户触摸 CCM
我正在寻找一个简单的 jQuery 或 Javascript 解决方案,以使导航侧边栏在用户向下滚动页面时顺利跟随用户。像这里一样:http://ucon-acrobatics.com/shop/ 任
我有一个 ListView 控件来显示项目,我想提供一个滑动/滑动手势来选择一个项目。我用 GestureRecognizer类来识别交叉滑动手势,但我还想通过水平移动选择的项目来为这个手势设置动画。
我想将 String 行标记化为标记(存储到 String 表中),并且我只能使用 java.io.*它是为了实现一个计算器。 例如:第一行:1+2+3第二行:1+ 2*3(标记之间有空格) 进入表{
我有一个 ListView 控件来显示项目,我想提供一个滑动/滑动手势来选择一个项目。我用 GestureRecognizer类来识别交叉滑动手势,但我还想通过水平移动选择的项目来为这个手势设置动画。
我有一个导航栏,当单击菜单图标时,它将滑入“#secondary-nav”并隐藏“#primary-nav”。然而 jquery 似乎没有显示“#secondary-menu”。下面提供的是 HTMl
这个问题已经有答案了: how to make a sliding up panel like the Google Maps app? (2 个回答) 已关闭 7 年前。 我正在寻找类似的实现,如下
我有 ViewPager(Slide) 和 3 张图片。共有三个图像是通过 Internet 下载的。如果我将图片换到服务器上的另一台服务器上,链接保持不变,但应用程序中的图片没有改变,仍然是缓存中的
我在 gridview 中创建了两个按钮。 我想达到以下目的,但不知道应该用什么方法? 首先我触摸第一个按钮,将显示 toast 1 msg。通过将我的手指滑到第二个按钮而不抬起我的手指,将显示 to
所以我设置了一个小的 jquery 动画,用户将鼠标悬停在容器上一段时间,这会导致容器 split ,然后显示内部信息。 我不希望鼠标一进入容器就开始动画,所以我在动画上放了一个delay()。现在动
这个问题在这里已经有了答案: Simulate swipe with mouse in javascript (5 个答案) 关闭 7 年前。
我希望我的 Sprite 像在冰上一样滑动。因此,如果他在地面上,那么他可以正常行走,但当他接触冰时,他会滑动,直到有东西阻止他。有谁知道如何才能做到这一点?谢谢 最佳答案 像“Sprite Move
我的代码有几个问题:HTML: Bellevue
我正在尝试实现从 fragment1 过渡到 fragment2 的滑动动画。我正在使用 setCustomAnimations 方法。而且我知道我需要使用框架方法来替换 fragment 。 我的代
我不知道你们是否听说过 app chomp,但应用程序中有一个布局,如下图所示。我想知道他们是如何设置的,我将如何使用它来为我自己的应用程序制作类似的东西。有趣的是,当你滑动时,没有像水平 Scrol
我想检测用户何时在一个单元格占据整个屏幕宽度的 collectionView 中向左或向右滑动。是否可以不添加手势识别器。我试过添加手势识别器,但只有当我们将 collectionView 的 scr
我正在尝试开发一个应用程序来复制类似 tinder 的基于滑动的提要。该应用程序的想法与火种非常相似,也具有向右滑动和向左滑动匹配功能。 到目前为止我做了什么-我在 MongoDB 中创建了一个刷卡集
我是一名优秀的程序员,十分优秀!