滚动

时间:2016-06-20 17:31:33

标签: android scroll scrollview

我想知道如何创建像谷歌日历(Android 5.1.0)

中的减法效果

滚动图像时,它的滚动速度比其他视图慢。它看起来非常棒。

图片1

enter image description here

图像2

enter image description here

如你所见,图像被移动了为什么滚动(看鱼)

1 个答案:

答案 0 :(得分:0)

这两种方法可以给你一个想法:

  

这是正确的方法

  

此方法从ConstraintLayout获取特定参数并进行修改。

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.xmlActivity内,添加: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);
        }
    });
}