【问题标题】:Android - How to scale image while scrolling NestedScrollView?Android - 如何在滚动 NestedScrollView 时缩放图像?
【发布时间】:2018-03-28 01:43:17
【问题描述】:

我是安卓开发新手。我正在尝试扩展ImageViewNestedScrollView scrollChange。但我不知道该怎么做。这就是我的布局。

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:elevation="8dp"
        android:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?colorPrimary"
            app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
            app:layout_scrollFlags="exitUntilCollapsed|scroll"
            app:titleEnabled="false">

            <ImageView
                android:id="@+id/bg"
                android:layout_width="match_parent"
                android:layout_height="220dp"
                android:scaleType="centerCrop"
                android:src="@drawable/background"
                app:layout_collapseMode="pin"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/polygon_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:contentInsetStartWithNavigation="0dp"
                app:layout_collapseMode="pin"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/polygon_scroll_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:scrollbars="none"
        android:scrollingCache="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center_horizontal"
            android:orientation="vertical">

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>

    <com.seluhadu.style.RoundedImageView
        android:id="@+id/user_image"
        android:layout_width="100dp"
        android:layout_height="100dp"
        app:layout_anchor="@id/appBarLayout"
        app:layout_anchorGravity="bottom|center"
        app:srcCompat="@drawable/logo"/>
</android.support.design.widget.CoordinatorLayout>

我试过 onScrollChangeListenrpolygonScrollView.setOnScrollChangeListener 但它没有检测到滚动。我无法实现它。这是我想要实现的一个例子。

【问题讨论】:

    标签: android android-layout android-collapsingtoolbarlayout android-nestedscrollview


    【解决方案1】:

    试试这个

    <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appBarLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:elevation="8dp"
            android:fitsSystemWindows="true">
    
            <android.support.design.widget.CollapsingToolbarLayout
                android:layout_width="match_parent"
                android:id="@+id/collapsingToolbarLayout"
                android:layout_height="match_parent"
                android:fitsSystemWindows="true"
                app:contentScrim="?colorPrimary"
                app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Title"
                app:layout_scrollFlags="exitUntilCollapsed|scroll"
                app:titleEnabled="false">
    
                <ImageView
                    android:id="@+id/bg"
                    android:layout_width="match_parent"
                    android:layout_height="220dp"
                    android:scaleType="centerCrop"
                    android:src="@color/colorPrimaryDark"
                    app:layout_collapseMode="pin" />
    
                <android.support.v7.widget.Toolbar
                    android:id="@+id/polygon_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="?attr/actionBarSize"
                    app:contentInsetStartWithNavigation="0dp"
                    app:layout_collapseMode="pin" />
            </android.support.design.widget.CollapsingToolbarLayout>
        </android.support.design.widget.AppBarLayout>
    
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/imageView"
            android:layout_width="96dp"
            android:layout_height="96dp"
            android:src="@drawable/abc"
            android:animateLayoutChanges="true"
            app:layout_anchor="@id/appBarLayout"
            app:layout_anchorGravity="center|bottom" />
    </android.support.design.widget.CoordinatorLayout>
    

    活动代码

    import android.os.Bundle;
    import android.support.design.widget.AppBarLayout;
    import android.support.design.widget.CollapsingToolbarLayout;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.view.animation.Animation;
    import android.view.animation.AnimationUtils;
    import android.view.animation.ScaleAnimation;
    import android.widget.ImageView;
    
    
    
    public class MainActivity extends AppCompatActivity {
    
    
        ImageView imageView;
        CollapsingToolbarLayout collapsingToolbarLayout;
        AppBarLayout appBarLayout;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.activity_main);
    
    
    
            collapsingToolbarLayout = findViewById(R.id.collapsingToolbarLayout);
            appBarLayout = findViewById(R.id.appBarLayout);
            imageView = findViewById(R.id.imageView);
    
            appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
                boolean isShow = true;
                int scrollRange = -1;
    
                @Override
                public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                    if (scrollRange == -1) {
                        scrollRange = appBarLayout.getTotalScrollRange();
                    }
                    if (scrollRange + verticalOffset == 0) {
                        isShow = true;
                        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_down);
                        imageView.startAnimation(animation);
                        imageView.setVisibility(View.GONE);
                        //imageView.setVisibility(View.GONE);
                    } else if (isShow) {
                        Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.scale_up);
                        imageView.startAnimation(animation);
                        imageView.setVisibility(View.VISIBLE);
                        imageView.setVisibility(View.VISIBLE);
                        isShow = false;
                    }
                }
            });
    
        }
        public void scaleView(View v, float startScale, float endScale) {
            Animation anim = new ScaleAnimation(
                    1f, 1f, // Start and end values for the X axis scaling
                    startScale, endScale, // Start and end values for the Y axis scaling
                    Animation.RELATIVE_TO_SELF, 0f, // Pivot point of X scaling
                    Animation.RELATIVE_TO_SELF, 1f); // Pivot point of Y scaling
            anim.setFillAfter(true); // Needed to keep the result of the animation
            anim.setDuration(1000);
            v.startAnimation(anim);
        }
    
    }
    

    动画文件

    缩小

    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <scale
            android:duration="1000"
            android:fromXScale="1.0"
            android:fromYScale="1.0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toXScale="0"
            android:toYScale="0" />
    </set>
    

    放大

    <set xmlns:android="http://schemas.android.com/apk/res/android">
        <scale
            android:duration="1000"
            android:fromXScale="0"
            android:fromYScale="0"
            android:pivotX="50%"
            android:pivotY="50%"
            android:toXScale="1.0"
            android:toYScale="1.0" />
    </set>
    

    【讨论】:

    • 感谢您的重播,它有效,但我真正想要的是当开始上下滚动时,根据 appBar 的高度使图像越来越小。如果你看到 gif 视频,当我向上滚动时,图像会变得越来越小,向下滚动会变得越来越大。当 appbar 完成图像停止。谢谢!
    • @SeluHadu 你可以根据自己的要求更改比例动画
    • 对于迟到的重播,我深表歉意。不,我没有解决它。我是新手,所以我不知道向上或向下滚动位置时如何获取。我试图做手势,但我无法解决它。感谢您的提问。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-04
    相关资源
    最近更新 更多