3

I'm new to android development. I'm trying to scale ImageView while NestedScrollView scrollChange. but I don't know how to do it. This is what look like my layout.

<?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>

I tried onScrollChangeListenr to polygonScrollView.setOnScrollChangeListener but it doesn't detect scrolling. I could not achieve it. this is an example what I want to achieve.

enter image description here

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Selu Hadu
  • 109
  • 2
  • 6

1 Answers1

3

Try this

<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>

Activity code

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

}

ANIMATION file

scale_down

<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>

scale_up

<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>
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
  • Thank you for replayed and it worked but what I want really is when start scrolling up and down make the image smaller and bigger according to the appBar height. if you see the gif video, when I scrolled up the image gets smaller and smaller and scroll down gets bigger and bigger. when appbar is finish the image stop. Thank you! – Selu Hadu Mar 28 '18 at 20:46
  • @SeluHadu than you can change scale animation as per your reqirement – AskNilesh Mar 29 '18 at 07:52
  • I'm sorry for the late replay. No, I did not solve it. I'm new so I don't know how to get when scrolling up or down position. I tried to gesture but I couldn't able to solve it. Thanks for the question. – Selu Hadu Apr 04 '18 at 03:27