如何更改滚动视图中元素的滚动速度

Posted

技术标签:

【中文标题】如何更改滚动视图中元素的滚动速度【英文标题】:How can i change the scroll speed of elements in a scroll view 【发布时间】:2021-05-20 09:54:12 【问题描述】:

我目前有一个基本的xml文件,结构如下:

<ScrollView 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:layout_
    android:layout_
    android:background="#EDEDED"
    tools:context=".firstFragment">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_
        android:layout_>

        <ImageView
            android:id="@+id/logoHome"
            android:layout_
            android:layout_
            android:background="@drawable/baywatchstandard"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <ImageView
            android:id="@+id/waveHome"
            android:layout_
            android:layout_
            android:background="@drawable/wave"
            android:layout_marginTop="180dp"
            app:layout_constraintTop_toTopOf="@id/logoHome"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintStart_toEndOf="parent"/>

    </androidx.constraintlayout.widget.ConstraintLayout>

</ScrollView>

我想创建一个视差效果,其中“logoHome”ImageView 在向下滚动时比“waveHome”ImageView 移动得慢。那可能吗?任何帮助将不胜感激:)

【问题讨论】:

【参考方案1】:

这确实花了我 5 个小时,但我终于弄明白了。不知何故,很难为这个简单的问题找到一个可行的答案,所以如果有人偶然发现这个问题,那就去吧:D

private String event = "UP";
    private float mouseY = 0;
    private float lastY = 0;
    private Handler handler = new Handler();

    private ImageView element;  //ImageView can be replaced with everything that has setTranslation

    @SuppressLint("ClickableViewAccessibility")
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) 

        super.onViewCreated(view, savedInstanceState);

        element = view.findViewById(R.id.logoHome);

        Scroll scroll = new Scroll();
        scroll.run();

        ConstraintLayout page = view.findViewById(R.id.page);
        page.setOnTouchListener(new View.OnTouchListener() 
            @Override
            public boolean onTouch(View v, MotionEvent e) 
                if(e.getAction() == MotionEvent.ACTION_MOVE) 
                    event = "MOVE";
                    mouseY = e.getY();
                 else if (e.getAction() == MotionEvent.ACTION_DOWN) 
                    event = "DOWN";
                    lastY = e.getY();
                 else 
                    event = "UP";
                
                return true;
            
        );

    

    private class Scroll extends Thread
        @Override
        public void run() 
            super.run();
            element.setTranslationY(scrollY(logoHome.getTranslationY(), -700, 0, (float) 0.15));
            lastY = lerp(lastY, mouseY, (float) 0.2);
            handler.postDelayed(this, 10);
        
    



    private float scrollY(float currentY, float min, float max, float scalar) 

        if(event.equals("MOVE") || event.equals("UP"))

            if((currentY + (mouseY - lastY)) < min) 
                return lerp(currentY, min, (float) 0.2);
            
            if((currentY + (mouseY - lastY)) > max)
                return lerp(currentY, max, (float) 0.2);
            
            return currentY + (mouseY - lastY) * scalar;
         else 
            return currentY;
        
    

    float lerp(float a, float b, float f) 
        return a + f * (b - a);
    

我确信这段代码可以改进很多,但我现在很高兴。

【讨论】:

以上是关于如何更改滚动视图中元素的滚动速度的主要内容,如果未能解决你的问题,请参考以下文章

降低滚动视图中平滑滚动的速度[重复]

滚动视图滚动时如何播放取决于滚动视图滚动速度的声音?

表格视图中的慢速/口吃滚动

如何在 SwiftUI 中更改滚动视图的滚动方向?

如何在 ios 中使用自动布局更改滚动视图内容大小

自动滚动:如何在滚动视图中自动滚动图像