Развернуть и свернуть анимацию на горизонтальной прокрутке - PullRequest
0 голосов
/ 26 июня 2018

Я работаю над приложением, в котором есть возможность выбора из списка продуктов. Я показываю эти изображения / продукты (ImageView) в HorizontalScrollView. Все работает нормально, кроме этого последнего требования, которое у меня есть: у меня есть анимация - когда вид находится в центре или когда прокрутка вида достигает центра, он должен расширяться, а все другие виды в виде прокрутки должны уменьшаться. вроде как док в MacBook - когда вы наводите курсор мыши на него, он расширяется.

После многих поисков я пришёл к этому, но этот код работает, если видимость видна. Я хочу заставить это работать, если представления находятся в центре.

код для раскрытия и свертывания:

private void horizontalScrollAnimation(ImageView imageView1, ImageView imageView2, ImageView imageView3) {

    Rect scrollBounds = new Rect();
    horizontal_scroll_view.getHitRect(scrollBounds);
    if (imageView1.getLocalVisibleRect(scrollBounds)) {
        expand(imageView1);
    } else {
        collapse(imageView1);
    }

    if (imageView1.getLocalVisibleRect(scrollBounds)) {
        expand(imageView2);
    } else {
        collapse(imageView2);
    }

    if (imageView1.getLocalVisibleRect(scrollBounds)) {
        expand(imageView3);
    } else {
        collapse(imageView3);
    }


}

развернуть и свернуть код:

public static void expand(final View v) {
    v.measure(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final int targetHeight = v.getMeasuredHeight();

    v.getLayoutParams().height = 1;
    v.setVisibility(View.VISIBLE);
    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            v.getLayoutParams().height = interpolatedTime == 1
                    ? LinearLayout.LayoutParams.WRAP_CONTENT
                    : (int)(targetHeight * interpolatedTime);
            v.requestLayout();
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int)(targetHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

public static void collapse(final View v) {
    final int initialHeight = v.getMeasuredHeight();

    Animation a = new Animation()
    {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            if(interpolatedTime == 1){
                v.setVisibility(View.GONE);
            }else{
                v.getLayoutParams().height = initialHeight - (int)(initialHeight * interpolatedTime);
                v.requestLayout();
            }
        }

        @Override
        public boolean willChangeBounds() {
            return true;
        }
    };

    a.setDuration((int)(initialHeight / v.getContext().getResources().getDisplayMetrics().density));
    v.startAnimation(a);
}

как узнать, находятся ли виды по центру в HorizontalScrollView?

1 Ответ

0 голосов
/ 26 июня 2018
 ObjectAnimator objectanimator1, objectanimator2;

 objectanimator1 = ObjectAnimator.ofFloat(view,"scaleX",1.0f,1.2f);

 objectanimator2 = ObjectAnimator.ofFloat(view,"scaleY",1.0f,1.2f); 


 objectanimator1.setDuration(4000);
 objectanimator2.setDuration(4000);

 objectanimator1.start();
 objectanimator2.start();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...