如何更改滚动视图中元素的滚动速度
How can i change the scroll speed of elements in a scroll view
我目前有一个基本的 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_width="match_parent"
android:layout_height="match_parent"
android:background="#EDEDED"
tools:context=".firstFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/logoHome"
android:layout_width="match_parent"
android:layout_height="420dp"
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_width="match_parent"
android:layout_height="420dp"
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 慢。那可能吗?任何帮助将不胜感激:)
这确实花了我 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);
}
我确信这段代码可以改进很多,但我现在很高兴。
我目前有一个基本的 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_width="match_parent"
android:layout_height="match_parent"
android:background="#EDEDED"
tools:context=".firstFragment">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/logoHome"
android:layout_width="match_parent"
android:layout_height="420dp"
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_width="match_parent"
android:layout_height="420dp"
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 慢。那可能吗?任何帮助将不胜感激:)
这确实花了我 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);
}
我确信这段代码可以改进很多,但我现在很高兴。