- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我看到很多例子说明视差背景当你滚动或 ListView 视差但是我找不到一个清楚的例子如何在你滚动 Activity 时对图像实现视差效果。
可以在 Airbnb 应用程序中找到示例实现。向下滚动时,您可以看到更多图片底部,向上滚动时,您可以看到更多图片顶部。
关于如何创建这种效果有什么提示和技巧吗?
最佳答案
有一些库可以产生视差效果,这取决于您的应用程序是否对您的特定情况有用,例如:
Google 是您的好 friend ;) 如果这些都不符合您的需求,那么您必须创建一个自定义的 ScrollView
但这是一个较长的故事,请先尝试一下并发布您的结果。
如果这些都不符合您的要求,那么您必须这样做:
首先,创建一个自定义的 ScrollView
,这样您就可以监听滚动变化。
public class ObservableScrollView extends ScrollView {
public interface OnScrollChangedListener {
public void onScrollChanged(int deltaX, int deltaY);
}
private OnScrollChangedListener mOnScrollChangedListener;
public ObservableScrollView(Context context) {
super(context);
}
public ObservableScrollView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public ObservableScrollView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if(mOnScrollChangedListener != null) {
mOnScrollChangedListener.onScrollChanged(l - oldl, t - oldt);
}
}
public void setOnScrollChangedListener(OnScrollChangedListener listener) {
mOnScrollChangedListener = listener;
}
}
显然,您需要在布局中使用它而不是默认的 ScrollView
:
<your.app.package.ObservableScrollView
android:id="@+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="match_parent">
此外,您还需要将 ImageView 包装在一个容器中,以使视差起作用:
<FrameLayout
android:id="@+id/img_container"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
</FrameLayout>
最后将您的 Activity
设置为您全新的 ObservableScrollView
的监听器并让视差开始:
public class MyActivity extends Activity implements ObservableScrollView.OnScrollChangedListener {
private ObservableScrollView mScrollView;
private View imgContainer;
@Override
public void onCreate(Bundle savedInstanceState) {
// Init your layout and set your listener
mScrollView = (ObservableScrollView)findViewById(R.id.scroll_view);
mScrollView.setOnScrollChangedListener(this);
// Store the reference of your image container
imgContainer = findViewById(R.id.img_container);
}
@Override
public void onScrollChanged(int deltaX, int deltaY) {
int scrollY = mScrollView.getScrollY();
// Add parallax effect
imgContainer.setTranslationY(scrollY * 0.5f);
}
}
您可以根据需要多少视差修改 0.5 值。
如果您的 ImageView 位于 Activity 的顶部,则上述答案可以正常工作。我在下面发布了一些代码来添加功能,使 ImageView 在我成功工作的 Activity 布局中的任何位置。这些是通用计算(可能有一些错误),稍作调整就可以让它适用于您自己的情况。
对于这个例子,我有一个固定高度的图像容器 200dp 和图像 240dp。主要目的是当图像容器位于屏幕中间时没有视差效果,并且当用户向上或向下滚动以应用效果时。因此,随着图像容器靠近屏幕顶部或屏幕底部,将应用更多的视差效果。以下计算通过阅读它们有点难以理解,因此尝试在纸上用实数做一个例子。
public class MyActivity extends Activity implements ObservableScrollView.OnScrollChangedListener {
private ObservableScrollView mScrollView;
private View imgContainer;
private ImageView mImageView;
@Override
public void onCreate(Bundle savedInstanceState) {
// Init your layout and set your listener
mScrollView = (ObservableScrollView)findViewById(R.id.scroll_view);
mScrollView.setOnScrollChangedListener(this);
// Store the reference of your image container
imgContainer = findViewById(R.id.img_container);
// Store the reference of your image
mImageView = findViewById(R.id.img);
}
@Override
public void onScrollChanged(int deltaX, int deltaY) {
// Get scroll view screen bound
Rect scrollBounds = new Rect();
mScrollView.getHitRect(scrollBounds);
// Check if image container is visible in the screen
// so to apply the translation only when the container is visible to the user
if (imgContainer.getLocalVisibleRect(scrollBounds)) {
Display display = getWindowManager().getDefaultDisplay();
DisplayMetrics outMetrics = new DisplayMetrics ();
display.getMetrics(outMetrics);
// Get screen density
float density = getResources().getDisplayMetrics().density;
// Get screen height in pixels
float dpHeight = outMetrics.heightPixels / density;
int screen_height_pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpHeight, getResources().getDisplayMetrics());
int half_screen_height = screen_height_pixels/2;
// Get image container height in pixels
int container_height_pixels = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 200, getResources().getDisplayMetrics());
// Get the location that consider a vertical center for the image (where the translation should be zero)
int center = half_screen_height - (container_height_pixels/2);
// get the location (x,y) of the image container in pixels
int[] loc_screen = {0,0};
imgContainer.getLocationOnScreen(loc_screen);
// trying to transform the current image container location into percentage
// so when the image container is exaclty in the middle of the screen percentage should be zero
// and as the image container getting closer to the edges of the screen should increase to 100%
int final_loc = ((loc_screen[1]-center)*100)/half_screen_height;
// translate the inner image taking consideration also the density of the screen
mImageView.setTranslationY(-final_loc * 0.4f * density);
}
}
}
我希望它可以帮助正在寻找类似功能的人。
关于android - 滚动时的 Imageview 视差,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26990195/
好的,所以我已经创建了自己的视差 jQuery/JS 脚本,但我只是不知道如何定位支持以下内容(需要以下内容)的浏览器: 支持背景位置的转换 我正在尝试的站点(预览)是: My Test Site 我
我正在制作带有固定背景和封面的整页。 当我添加视差时 - 动画看起来不错,但它滞后太多,我不知道为什么。我有一台很好的 PC,所以这不是这个问题,我之前让其他人产生了视差,这并不滞后。 有什么解决办法
我有一个奇怪的组合 - 视差和汉堡菜单。问题是米 导航会在您悬停的任何地方下降,不仅 当您将鼠标悬停在汉堡菜单图标上时。我认为这是一个厚厚的错误 代码,但我找不到它。我没有 jquery 的。还有,我
因此,我一直在努力围绕这种名为“视差”的整洁效果。基本上,背景滚动比前景元素慢。 我发现这个新的“技巧”正在起作用。随着滚动的进行,更改top属性以创建视差效果。 问题... 因此,为了提高性能并减轻
我的导航栏在 CSS 中加入视差效果后无法保持固定。 我在我的网站上放置了一个带有过渡 JS“Math.round($(window).scrollTop()) > 490)”的固定导航栏。 之后,我
这是一个非常模糊的问题,但我不确定从哪里开始,所以如果有人能给我一些想法,那你就太好了。 我想在我的网站主页上使用视差效果,但我不确定如何创建这种效果。我想要的效果在这里装箱http://davega
我使用了本教程中的视差脚本 http://tympanus.net/codrops/2011/01/03/parallax-slider/ 我在这里发布了一个 fiddle http://jsfidd
我想知道如何创建视差滚动效果之类的东西。向下滚动得越远,div 变得越不透明,在某个点之后,它又开始变得更加透明。我知道需要 JS/JQuery 才能做到这一点。谁能给我一个简单的方法来实现这个目标?
我的页面上有一个元素是绝对定位的。 我正在尝试编写一段 jQuery 代码,使该元素的滚动速度比页面上其他元素的滚动速度慢。 到目前为止,我已经写了这篇文章,但似乎根本无法让它发挥作用。有没有人有这方
我是 cocos2d 世界的新手,我正在开发我的第一个教程并面临一个问题我的问题是我有一个图像(1024 X 320)并且我的方向是横向我需要从右到左连续移动该图像为此目的我使用了 Ray 的太空射击
我需要调试此 jQuery 的帮助,其中我操纵容器中元素的上边距,该容器具有固定大小并将溢出设置为隐藏,以便我可以创建视差效果。视差有效,但如果您向下滚动一个像素,然后重新加载页面,它就会中断。谁能帮
我有一个 div,我想留在原地,这样导航栏和其他部分就可以通过它。 这是我目前的工作:
我建了一个网站,页面很长。我们决定使用纯 CSS3 为它添加视差并且它起作用了。 我从这里得到的 CSS3 视差代码: codepen.io/keithclark/pen/JycFw 后来,我们决定应
由于我对 Javascript/Jquery/HTML5/CSS3 还是个新手,我想我应该问一个问题给你们这些更有经验的程序员。 我有点卡在我的程序逻辑的开头。我想做一个具有视差“滚动”效果的网站,但
已关闭。此问题旨在寻求有关书籍、工具、软件库等的建议。不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,
-- 更新 2 -- 如果您使用单个相机计算距离,下面的文章非常有用(尽管它使用的是 Python 而不是 C++):Find distance from camera to object/marke
我有一个使用 skrollr 构建的视差网站在我到达页面末尾之前效果很好。我想要一个 成为您可以在页面上滚动的最后/最远点。 目前,如果我在大屏幕上查看时会留下巨大的空白,或者我无法进一步向下滚动以在
我不明白为什么会这样:我有: https://jsfiddle.net/d5jehq02/1 background forefront 我正在
我看到很多例子说明视差背景当你滚动或 ListView 视差但是我找不到一个清楚的例子如何在你滚动 Activity 时对图像实现视差效果。 可以在 Airbnb 应用程序中找到示例实现。向下滚动时,
我的背景图片来自background-url,我的视差适用于我放置的图片:background-attachment: fixed; 我怎样才能让背景图片在我滚动时稍微移动一点? 这是我正在谈论的例子
我是一名优秀的程序员,十分优秀!