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

    }
}
相关推荐
雨白5 分钟前
Android 自定义 View:从绘制基础到实战仪表盘与饼图
android
jiunian_cn25 分钟前
【Linux】线程
android·linux·运维·c语言·c++·后端
Frank_HarmonyOS9 小时前
Android MVVM(Model-View-ViewModel)架构
android·架构
新子y13 小时前
【操作记录】我的 MNN Android LLM 编译学习笔记记录(一)
android·学习·mnn
lincats14 小时前
一步一步学习使用FireMonkey动画(1) 使用动画组件为窗体添加动态效果
android·ide·delphi·livebindings·delphi 12.3·firemonkey
想想吴15 小时前
Android.bp 基础
android·安卓·android.bp
写点啥呢1 天前
Android为ijkplayer设置音频发音类型usage
android·音视频·usage·mediaplayer·jikplayer
coder_pig1 天前
🤡 公司Android老项目升级踩坑小记
android·flutter·gradle
死就死在补习班1 天前
Android系统源码分析Input - InputReader读取事件
android
死就死在补习班1 天前
Android系统源码分析Input - InputChannel通信
android