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

    }
}
相关推荐
炸炸鱼.20 分钟前
Python 操作 MySQL 数据库
android·数据库·python·adb
用户41659673693552 小时前
nextlib 项目架构与深度技术指南 (Architecture & Technical Master Guide)
android
aq55356002 小时前
Laravel10.x重磅升级,新特性一览
android·java·开发语言
Trouvaille ~3 小时前
【MySQL篇】数据类型:存储数据的基础
android·数据库·mysql·adb·字符集·数据类型·基础入门
2401_885885044 小时前
开发视频短信接口好开发吗?图文视频短信接口对接教程
android·音视频
千码君20165 小时前
kotlin:Jetpack Compose 给APP添加声音(点击音效/背景音乐)
android·开发语言·kotlin·音效·jetpack compose
Fᴏʀ ʏ꯭ᴏ꯭ᴜ꯭.5 小时前
MySQL半同步复制与GTID实战详解
android·mysql·adb
用户41659673693556 小时前
深度解码:记一次视频时间戳(PTS)异常导致的播放故障排查
android
大白菜和MySQL8 小时前
linux系统环境常用命令
android·linux·adb
Ehtan_Zheng8 小时前
彻底告别 AndroidX 依赖:如何在 KMP 中构建 100% 复用的 UI 逻辑层?
android