自定义折叠工具栏平滑标题调整大小

时间:2017-02-07 10:04:15

标签: java android xamarin android-collapsingtoolbarlayout

我使用的是自定义折叠工具栏布局,其中包含标题和副标题。
我获得了在弯曲的路径上折叠和动画的标题,但是标题的一部分变小了,因为折叠并不平滑。它以锯齿状的方式调整大小。
这是我的行为,负责移动和调整标题的大小:

public class ViewBehavior : CoordinatorLayout.Behavior
{
    private Context mContext;

    private int mStartMarginRight;
    private int mEndMargintRight;
    private int mMarginLeft;
    private int mStartMarginBottom;
    private bool isHide;
    private static float SCALE_MINIMUM = 0.5f;
    public ViewBehavior(Context context, IAttributeSet attrs)
    {
        mContext = context;
    }

    public override bool LayoutDependsOn(CoordinatorLayout parent, Java.Lang.Object child, View dependency)
    {
        return dependency is AppBarLayout;
    }

    public override bool OnDependentViewChanged(CoordinatorLayout parent, Java.Lang.Object child, View dependency)
    {
        ShouldInitProperties((child as HeaderView), dependency);

        int maxScroll = ((AppBarLayout)dependency).TotalScrollRange;
        float percentage = System.Math.Abs(dependency.GetY()) / (float)maxScroll;

        float childPosition = dependency.Height
                + dependency.GetY()
                - (child as View).Height
                - (getToolbarHeight() - (child as View).Height) * percentage / 2;


        childPosition = childPosition - mStartMarginBottom * (1f - percentage);

        CoordinatorLayout.LayoutParams lp = (CoordinatorLayout.LayoutParams)(child as View).LayoutParameters;
        lp.RightMargin = (int)(100 * System.Math.Sin(percentage * System.Math.PI)) + mStartMarginRight / 2 + mEndMargintRight / 2;
        lp.LeftMargin = mMarginLeft;
        (child as View).LayoutParameters = lp;

        (child as View).SetY(childPosition);
        float x = (child as HeaderView).Title.TextSize;
        //Here is the algorithm for setting the text size
        (child as HeaderView).Title.SetTextSize(ComplexUnitType.Sp, 36 * (1 - percentage / 2));
        (child as HeaderView).SubTitle.SetTextSize(ComplexUnitType.Sp, 26 * (1 - percentage / 2));

        var toolbarTitleSize = (int)TypedValue.ApplyDimension(ComplexUnitType.Sp, 18, Application.Context.Resources.DisplayMetrics);
        var toolbarSubTitleSize = (int)TypedValue.ApplyDimension(ComplexUnitType.Sp, 16, Application.Context.Resources.DisplayMetrics);
        if ((child as HeaderView).Title.TextSize < toolbarTitleSize)
            (child as HeaderView).Title.SetTextSize(ComplexUnitType.Sp, 18);
        if ((child as HeaderView).SubTitle.TextSize < toolbarSubTitleSize)
            (child as HeaderView).SubTitle.SetTextSize(ComplexUnitType.Sp, 14);
        if (Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
        {
            if (isHide && percentage < 1)
            {
                (child as View).Visibility = ViewStates.Visible;
                isHide = false;
            }
            else if (!isHide && percentage == 1)
            {
                (child as View).Visibility = ViewStates.Gone;
                isHide = true;
            }
        }
        return true;
    }


    public void ShouldInitProperties(HeaderView child, View dependency)
    {

        if (mStartMarginRight == 0)
            mStartMarginRight = mContext.Resources.GetDimensionPixelOffset(Resource.Dimension.header_view_start_margin_right);

        if (mEndMargintRight == 0)
            mEndMargintRight = mContext.Resources.GetDimensionPixelOffset(Resource.Dimension.header_view_end_margin_right);

        if (mStartMarginBottom == 0)
            mStartMarginBottom = mContext.Resources.GetDimensionPixelOffset(Resource.Dimension.header_view_start_margin_bottom);

        if (mMarginLeft == 0)
            mMarginLeft = mContext.Resources.GetDimensionPixelOffset(Resource.Dimension.header_view_end_margin_left);

    }


    public int getToolbarHeight()
    {
        int result = 0;
        TypedValue tv = new TypedValue();
        if (mContext.Theme.ResolveAttribute(Android.Resource.Attribute.ActionBarSize, tv, true))
        {
            result = TypedValue.ComplexToDimensionPixelSize(tv.Data, mContext.Resources.DisplayMetrics);
        }
        return result;
    }
}

如何更改算法以便以更平滑的方式调整大小?

编辑 - 视频
https://youtu.be/j6LseSW6h1s

4 个答案:

答案 0 :(得分:3)

正如其他人所提到的,通过textSize进行缩放在动画中的Android上效果不佳,因为它不够准确(它将小数值四舍五入为整数)。

如果它满足您的需要,您应该使用scaleX / scaleY属性执行动画,例如:

float scale = 1 - percentage * SCALE_MINIMUM;
(child as HeaderView).Title.SetScaleX(scale);
(child as HeaderView).Title.SetScaleY(scale);
(child as HeaderView).SubTitle.SetScaleX(scale);
(child as HeaderView).SubTitle.SetScaleY(scale);

答案 1 :(得分:2)

您遇到的问题是,即使您将比例计算为化学值,它们也会在TextView中变为整数值。您应该在TextView的Paint类中启用LINEAR_TEXT_FLAGSUBPIXEL_TEXT_FLAG标志,以实现平滑缩放和定位。

这样的事情:

yourTextView.Paint.SubpixelText = true;
yourTextView.Paint.LinearText = true;

答案 2 :(得分:1)

我使用这种布局格式来折叠工具栏,它可以顺利运行。

  <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar_layout"
        android:layout_width="match_parent"
        android:layout_height="@dimen/height_300dp"
        android:background="?colorPrimary">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsible_Toolbar_Layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:collapsedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
            app:contentScrim="@color/colorDarkBlue"
            app:expandedTitleMarginEnd="@dimen/margin_64dp"
            app:expandedTitleMarginStart="@dimen/margin_20dp"
            app:expandedTitleTextAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Title.Inverse"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap">

            <ImageView
                android:id="@+id/image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:clickable="true"
                android:fitsSystemWindows="true"
                android:focusableInTouchMode="true"
                android:scaleType="centerCrop"
                app:layout_collapseMode="parallax" />

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@color/colorTransparent" />

            <ImageButton
                android:id="@+id/ib"
                android:layout_width="@dimen/margin_35dp"
                android:layout_height="@dimen/margin_35dp"
                android:layout_gravity="right"
                android:layout_marginRight="@dimen/margin_10dp"
                android:layout_marginTop="@dimen/height_245dp"
                android:background="@null"
                android:src="@drawable/ic_launcher" />


            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?actionBarSize"
                android:background="@android:color/transparent"
                android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                app:contentInsetStart="@dimen/margin_50dp"
                app:layout_collapseMode="pin">

                <ImageButton
                    android:id="@+id/ib2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="right"
                    android:layout_marginRight="@dimen/margin_10dp"
                    android:background="@null"
                    android:popupTheme="@style/ThemeOverlay.AppCompat.Light"
                    android:src="@drawable/ic_launcher"
                    android:visibility="invisible" />

            </android.support.v7.widget.Toolbar>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

之后在IntializeComponents的Fragment中我只初始化这个布局并设置Text

     appBarLayout = (AppBarLayout)view.findViewById(R.id.app_bar_layout);

        CollapsingToolbarLayout toolbarLayout = (CollapsingToolbarLayout) view.findViewById(R.id.collapsible_Toolbar_Layout);
        toolbarLayout.setTitle(yourTitle);
        toolbarLayout.setCollapsedTitleTextColor(Color.WHITE);
        toolbarLayout.setExpandedTitleColor(Color.WHITE);
        appBarLayout.addOnOffsetChangedListener(this);

添加此方法以处理工具栏

    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
        int maxScroll = appBarLayout.getTotalScrollRange();
        float percentage = (float) Math.abs(offset) / (float) maxScroll;
        handleToolbarTitleVisibility(percentage);
    }

    private void handleToolbarTitleVisibility(float percentage) {
        if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) {
            if(!mIsTheTitleVisible) {
                imageButton.setVisibility(View.VISIBLE);
                ib.setVisibility(View.INVISIBLE);
                mIsTheTitleVisible = true;
            }

        } else  {

            if (mIsTheTitleVisible) {
                imageButton.setVisibility(View.INVISIBLE);
                ibMap.setVisibility(View.VISIBLE);
                mIsTheTitleVisible = false;
            }
        }
    }

我希望它能帮到你:)。

答案 3 :(得分:1)

不,如果您使用的是customTextview,那么它也会起作用,因为我也只使用customTextView。

您会在此处发布customtextview代码吗?所以所有人都可以在customTextview中看到问题。