- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试使用回收器 View 编写我自己的视差效果。它目前工作得很好......直到我不改变我的设备方向。
在状态恢复时,我取回了我用来计算标题 Y 翻译的“指标”变量,这没问题。但是在恢复后我很难从不同的角度获得高度。
基本上这是我记录的内容:
D/parallax﹕ gridHeight: 0 - toolbar: 0 - poster: 0
我尝试使用 .measure() 然后使用 getMeasuredHeight() 但这是我得到的:
D/parallax﹕ gridHeight: 0 - toolbar: 128 - poster: 1108
也许我误用了 Measure。或者也许我应该在另一个时刻使用我的视差方法? (运行时的衣服?)如果你有任何线索......
这是我阅读一年后的第一个问题。希望我这样做的好方法。并感谢您的帮助 ;)
这是我的代码:
public class ParallaxActivity extends Activity {
private int metrics = 0;
private int resize = 0;
private int gridHeight = -1;
private int toolbarHeight = -1;
private int posterHeight = -1;
private boolean docked = false;
private Toolbar toolbar;
private ImageView poster;
private LinearLayout header;
private RecyclerView grid;
private RecyclerView.LayoutManager manager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parallax2);
toolbar = (Toolbar)findViewById(R.id.toolbar_actionbar);
toolbar.setNavigationIcon(R.drawable.ic_launcher);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
finish();
}
});
poster = (ImageView)findViewById(R.id.poster);
grid = (RecyclerView)findViewById(R.id.list);
header = (LinearLayout)findViewById(R.id.header);
manager = new GridLayoutManager(this, 2);
grid.setLayoutManager(manager);
DefaultItemAnimator animator = new DefaultItemAnimator();
grid.setItemAnimator(animator);
RecyclerGridAdapter ad = new RecyclerGridAdapter(this);
grid.setAdapter(ad);
ad.update();
grid.setOnScrollListener(new RecyclerView.OnScrollListener()
{
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
metrics += dy / 3;
parallax();
}
});
}
@Override
protected void onSaveInstanceState(Bundle b)
{
super.onSaveInstanceState(b);
b.putInt("metrics", metrics);
}
@Override
protected void onRestoreInstanceState(Bundle b)
{
super.onRestoreInstanceState(b);
metrics = b.getInt("metrics", 0);
}
@Override
public void onAttachedToWindow()
{
if (metrics != 0)
parallax();
}
private void parallax()
{
if (gridHeight == -1)
{
gridHeight = grid.getHeight();
toolbarHeight = toolbar.getHeight();
posterHeight = poster.getHeight();
Log.d("parallax", "gridHeight: " + gridHeight + " - toolbar: " + toolbarHeight + " - poster: " + posterHeight);
}
if (!docked && metrics > posterHeight - toolbarHeight)
{
docked = true;
toolbar.setBackgroundColor(0xff000000);
}
if (docked && metrics < posterHeight - toolbarHeight)
{
docked = false;
toolbar.setBackgroundColor(0x00000000);
}
if (metrics < 0)
resize = 0;
else if ( metrics > posterHeight - toolbarHeight)
resize = posterHeight - toolbarHeight;
else
resize = metrics;
header.setTranslationY(-resize);
grid.setTranslationY(-resize);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) grid.getLayoutParams();
params.height = gridHeight + resize;
grid.setLayoutParams(params);
}
}
和我的布局:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar_actionbar"
android:background="@null"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="@+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:elevation="0dp">
<ImageView
android:id="@+id/poster"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:src="@drawable/jpeg"/>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SerieKids"
android:textSize="20dp"
android:textColor="#ffffff"
android:background="#000000"
android:paddingStart="100dp"/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"/>
</LinearLayout>
</RelativeLayout>
最后是我使用 measure() 的方式
if (gridHeight == -1)
{
toolbar.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
poster.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
grid.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
gridHeight = grid.getMeasuredHeight();
toolbarHeight = toolbar.getMeasuredHeight();
posterHeight = poster.getMeasuredHeight();
Log.d("parallax", "gridHeight: " + gridHeight + " - toolbar: " + toolbarHeight + " - poster: " + posterHeight);
}
最佳答案
将 onGlobalLayoutListener
添加到您的 RecyclerView
。这样您就可以确保您的 parallax
例程仅在您的 View 被完全绘制后才被调用。还要确保取消注册 globalLayout,否则每次绘制 View 时都会调用它(如果您使用它来控制旋转变化,那么我认为无需删除监听器。这样它会一直旋转时调用,因为布局将再次绘制)。
如何注册和取消注册布局监听器的示例:
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
} else {
view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
}
//your parallax routine here
}
});
关于安卓 : Restoring recyclerview parallax,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26807370/
http://pixelcog.github.io/parallax.js/ 世界,您好!我对上面的插件有问题。我已经设法制作了几个视差背景并且可以用内容填充它们没有问题但是在文档中它提到能够使用视差
当网页提供给 iPhone 时,如何在网页上的 javascript 中接收视差事件?我查看了 github.com 的 404 页面,它正在使用加速度计响应运动。只需尝试访问 github.com/
我需要帮助来创建一个 css 视差页面,就像 http://www.wideeyecreative.com 我希望视差采用这种格式: [固定div] [滚动 div] [滚动 div] [固定格] [
我目前正在开发一个视差插件,以帮助创建垂直滚动网站之一,其中某些元素会创建视差效果。 大多数插件使用巨大的 DIV 标签并滚动背景。就我而言,这是行不通的,因为客户希望动画角色 Sprite 和各种其
我正在尝试在实现框架中实现视差效果 http://materializecss.com/parallax.html在我的项目中。我已经能够初始化并实现它。 如何自定义背景图像和内容的速度。 这是我的代
我尝试使用回收器 View 编写我自己的视差效果。它目前工作得很好......直到我不改变我的设备方向。 在状态恢复时,我取回了我用来计算标题 Y 翻译的“指标”变量,这没问题。但是在恢复后我很难从不
请在 chrome 和 firefox 中查看下面给出的链接我正面临一些奇怪的问题 http://amaru-ventures.in/Headline_factory/ 第一期 滚动在 Firefox
您好,我正在使用以下库:http://pixelcog.github.io/parallax.js/我有一个问题。 我很喜欢我的形象,我希望它看起来像我 Image with problems 我的代
所以我尝试使用插件 parallax.js 创建空间效果和创建星星的两个独立的 Canvas 元素。我已经按照插件的 github 的指示设置了 HTML 和 JS,但是这两个 Canvas 元素都没
我正在尝试使用 parallax.js 将视差部分滚动组合在一起。我似乎无法让这些部分正常工作。我正在关注他们的教程 http://pixelcog.github.io/parallax.js/但由于
我有一个基本的视差网站 here ,但是当我尝试添加动态嵌入式 youtube 视频时出现错误。流体宽度视频解释 here 我试图保持我的视差功能,同时拥有流畅宽度的视频,但我在
我希望当鼠标点击英雄面板区域时,飞机和火箭仅从其原始位置移动约 5%。 当前代码使图像跟随和偏移鼠标位置。 请协助。 $(document).ready(function () { $('#h
我在一个网站上工作,我正在尝试使用 js 插件 scrollorama - http://johnpolacek.github.io/scrollorama/ 我已经在网站上很好地设置了插件,除了我的
当我向 div 添加背景图像时,下面的 div 会滚动经过该图像,从而产生很酷的视差效果。 但是,这仅在固定背景附件时有效。但是在固定位置,我无法让背景图像大小“覆盖”或“包含”div,而图像不会消失
我已经尝试使用 parallax.js jquery 插件超过 10 次。但这不起作用。我不明白我这是怎么了。 插件站点:http://matthew.wagerfield.com/parallax/
我已经使用 css 设置了视差效果:JS Fiddle 代码: body { margin: 0px; } header { height: 218px; background: blue
这是我的 html 文件: Html 这是我的 CSS 文件: CSS 问题是:如果我从 css 中的“body, html”中删除“html”在第 35 行,我的 scrollspy 导航栏正在工作
我在网站上使用 parallax.js,但遇到了一些问题。 似乎每次我想从右侧滚动一个 div 时,我正在滚动的 div 中的所有内容都非常向左对齐,并且 div 的内容都被压在左边 margin 。
是否可以在特定时间段/特定像素数内动态触发鼠标滚动? 基本上我需要建立一个视差网站 - 但简报要求一个自动滚动的介绍动画自动滚动到页面中的某个点 - 我正在考虑为整个网站使用 Scrolldeck 以
我在这个方程上遇到了一些麻烦。 本质上,当光标位于边缘 200 像素范围内时,我希望画板(网格)div 将自身定位到浏览器的边缘。 这可以通过观察鼠标坐标来设置位置来轻松完成,但我希望它是流畅的。 I
我是一名优秀的程序员,十分优秀!