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);
            }
        });

    }
}
相关推荐
vocal3 小时前
【我的安卓第一课】Android 多线程与异步通信机制(1)
android
顾林海3 小时前
ViewModel 销毁时机详解
android·面试·android jetpack
恋猫de小郭5 小时前
Google I/O Extended :2025 Flutter 的现状与未来
android·前端·flutter
@Ryan Ding5 小时前
MySQL主从复制与读写分离概述
android·mysql·adb
移动开发者1号6 小时前
Android 同步屏障(SyncBarrier)深度解析与应用实战
android·kotlin
移动开发者1号6 小时前
深入协程调试:协程调试工具与实战
android·kotlin
雨白14 小时前
Jetpack系列(三):Room数据库——从增删改查到数据库平滑升级
android·android jetpack
花王江不语17 小时前
android studio 配置硬件加速 haxm
android·ide·android studio
江太翁19 小时前
mediapipe流水线分析 三
android·mediapipe
与火星的孩子对话20 小时前
Unity进阶课程【六】Android、ios、Pad 终端设备打包局域网IP调试、USB调试、性能检测、控制台打印日志等、C#
android·unity·ios·c#·ip