滚动时移动背景 [英] move background when scrolling
本文介绍了滚动时移动背景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道如何在谷歌日历(android 5.1.0)中创建视差效果
I wonder how to create a paralax effect like in google calendar(android 5.1.0)
当您滚动图像时,它的滚动速度比其他视图慢.而且看起来非常棒.
When you scroll the image, it's scrolling slower then the other views. And it looks pretty awesome.
图片 1
图片2
如你所见图片被移动了为什么滚动(看鱼)
As you see image was moved why scrolling(watch the fish)
推荐答案
这两个方法可以给你一个思路:
This two methods could give you an idea:
这是正确的做法.
- 使用
ConstraintLayout.LayoutParams代码>
这个方法从ConstraintLayout
中获取一个特定的参数并修改它.
This method get a specific parameter from the
ConstraintLayout
and modify it.
Guideline guideLine = (Guideline) findViewById(R.id.your_guideline);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideLine.getLayoutParams();
params.guidePercent = 0.05f; // 5% // range: 0 <-> 1
guideLine.setLayoutParams(params);
<小时>
使用 LayoutParams 创建视差效果
在 AndroidManifest.xml
的 Activity
中,添加:android:hardwareAccelerated="true"
.
<activity
android:name=".MainActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:hardwareAccelerated="true">
...
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="@+id/constraint_root"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.6"
tools:layout_editor_absoluteY="410dp"
tools:layout_editor_absoluteX="0dp" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:scaleType="centerCrop"
app:srcCompat="@drawable/bg_blur"
app:layout_constraintBottom_toTopOf="@+id/guideline"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline2"
app:layout_constraintHorizontal_bias="0.0" />
<android.support.constraint.Guideline
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/guideline2"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.3"
tools:layout_editor_absoluteY="205dp"
tools:layout_editor_absoluteX="0dp" />
<ScrollView
android:id="@+id/scroll_front"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.101">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.Guideline
android:id="@+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.2269821" />
<TextView
android:id="@+id/textView3"
android:layout_width="0dp"
android:layout_height="1200dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="@android:color/white"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="@+id/guideline6" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
</android.support.constraint.ConstraintLayout>
MainActivity.java
Guideline guideTopInfo, guideTopInfo2;
ConstraintLayout.LayoutParams params, params2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
guideTopInfo = (Guideline) findViewById(R.id.guideline);
guideTopInfo2 = (Guideline) findViewById(R.id.guideline2);
params = (ConstraintLayout.LayoutParams) guideTopInfo.getLayoutParams();
params2 = (ConstraintLayout.LayoutParams) guideTopInfo2.getLayoutParams();
final ScrollView scrollView = (ScrollView) findViewById(R.id.scroll_front);
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
float percentage = scrollView.getScrollY() * 0.0001f; // 0.001f faster // 0.00001f slower parallax animation
Log.d("mLog", String.valueOf(percentage));
params.guidePercent = 0.6f - percentage;
guideTopInfo.setLayoutParams(params);
params2.guidePercent = 0.3f - percentage;
guideTopInfo2.setLayoutParams(params2);
}
});
}
这篇关于滚动时移动背景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文