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

    }
}
相关推荐
a3158238062 小时前
Android Framework开发知识点整理
android·java·linux·服务器·framework·android源码开发
k***82512 小时前
图文详述:MySQL的下载、安装、配置、使用
android·mysql·adb
小七有话说3 小时前
DevUI与企业级中后台系统融合:低代码表单构建器实战
android·rxjava·devui
暗碳4 小时前
安卓abx二进制xml文件转换普通xml文件
android·xml
4z334 小时前
Android15 Framework(3):系统服务进程 SystemServer 解析
android·源码阅读
没有了遇见5 小时前
Android 之Google Play bundletool 校验 AAB包
android·google
yuanhello5 小时前
【Android】Android的键值对存储方案对比
android·java·android studio
Ditglu.5 小时前
CentOS7 MySQL5.7 主从复制最终版搭建流程(避坑完整版)
android·adb
恋猫de小郭5 小时前
Android Studio Otter 2 Feature 发布,最值得更新的 Android Studio
android·前端·flutter
走在路上的菜鸟6 小时前
Android学Dart学习笔记第十二节 函数
android·笔记·学习·flutter