android 快速实现 HorizontalScrollView滑动时,背景跟随缓慢滑动

1.布局:activity_main.xml

XML 复制代码
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ImageView
        android:id="@+id/bg"
        android:layout_width="500dp"
        android:layout_height="match_parent"
        android:scaleType="fitXY"
        android:src="@mipmap/ic_launcher" />

    <HorizontalScrollView
        android:id="@+id/hsv"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent">

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:src="@mipmap/ic_launcher" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="220dp"
                android:src="@mipmap/ic_launcher" />
            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginStart="220dp"
                android:src="@mipmap/ic_launcher" />
        </LinearLayout>
    </HorizontalScrollView>

</FrameLayout>

2.activity实现:

java 复制代码
public class MainActivity extends AppCompatActivity {
    private String TAG = MainActivity.class.getSimpleName();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ImageView bg = findViewById(R.id.bg);
        HorizontalScrollView hsv = findViewById(R.id.hsv);

        hsv.setOnScrollChangeListener(new View.OnScrollChangeListener() {
            @Override
            public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                float canScrollX = hsv.getChildAt(0).getMeasuredWidth() - hsv.getMeasuredWidth();//计算hsv可滑动的距离X
                float bgCanScrollX=bg.getMeasuredWidth()-hsv.getMeasuredWidth();//计算bg可滑动的距离X
                float percent=scrollX/canScrollX;//计算滑动距离百分比
                int bgSx= (int) (percent*bgCanScrollX);//bg滑动距离
                bg.scrollTo(bgSx,scrollY);//bg滑动
                Log.i(TAG,"canScrollX="+canScrollX+",bgCanScrollX="+bgCanScrollX+",percent="+percent+",bgSx="+bgSx);
            }
        });

    }
}
相关推荐
JMchen1238 小时前
Android UDP编程:实现高效实时通信的全面指南
android·经验分享·网络协议·udp·kotlin
黄林晴9 小时前
Android 17 再曝猛料:通知栏和快捷设置终于分家了,这操作等了十年
android
有位神秘人9 小时前
Android获取设备中本地音频
android·音视频
JMchen1239 小时前
Android网络安全实战:从HTTPS到双向认证
android·经验分享·网络协议·安全·web安全·https·kotlin
CS创新实验室9 小时前
Pandas 3 的新功能
android·ide·pandas
ujainu10 小时前
护眼又美观:Flutter + OpenHarmony 鸿蒙记事本一键切换夜间模式(四)
android·flutter·harmonyos
三少爷的鞋10 小时前
为什么我不在 Android ViewModel 中直接处理异常?
android
草莓熊Lotso11 小时前
Linux 文件描述符与重定向实战:从原理到 minishell 实现
android·linux·运维·服务器·数据库·c++·人工智能
恋猫de小郭11 小时前
Flutter Zero 是什么?它的出现有什么意义?为什么你需要了解下?
android·前端·flutter
工程师老罗17 小时前
如何在Android工程中配置NDK版本
android