Android 动画仅在滚动其父视图后才开始

发布于 2024-10-31 21:32:06 字数 5494 浏览 7 评论 0原文

我正在尝试在 android 中创建一个可扩展的菜单项,它看起来像一个按钮,单击按钮时,按钮将通过动画向下扩展。我为布局设置了展开动画,当单击我的视图时我想展开该布局,但我遇到了动画问题。当我单击视图时,它不会立即启动,而是当我向下滚动或向上滚动视图的容器时启动。如果容器不可滚动,我的动画将永远不会开始。我做错了什么?

这是我的展开方法、onClick 方法和自定义视图的布局 xml 文件,它将执行以下操作:

expand:

public void expand(final View v) {

        try {
            Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class);
            m.setAccessible(true);
            m.invoke(v,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(((View)v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST)
            );
        } catch (Exception e) {
            Log.e(TAG , "Caught an exception!", e);
        }
        final int initialHeight = v.getMeasuredHeight();
        Log.d("test", "initialHeight="+initialHeight);

        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);


        Animation a = new Animation() {

            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                final int newHeight = (int) (initialHeight * interpolatedTime);
                v.getLayoutParams().height = newHeight;
                v.requestLayout();
            }

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

        };

        a.setDuration(1000);
        a.setInterpolator(AnimationUtils.loadInterpolator(context,
                android.R.anim.accelerate_decelerate_interpolator));
        v.startAnimation(a);

        isExpanded = !isExpanded;

    }

onClick:

public void onClick(View v) {
    if (!isExpanded) {
        expand(subButtonsLayout);
    } else {
        collapse(subButtonsLayout);
    }
}

自定义菜单项视图的布局 xml:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mtx="http://schemas.android.com/apk/res/com.matriksdata.trademaster"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonTop"
        android:background="@drawable/opened_menu_bg_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
    <LinearLayout
        android:background="@drawable/opened_menu_bg_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical">
        <LinearLayout
            android:id="@+id/xExpandableMenuButtonTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">
            <TextView
                android:id="@+id/xExpandableMenuButtonText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                        android:textAppearance="@style/expandable_menu_button_textstyle"
                android:text="Button Text">
            </TextView>
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:src="@drawable/menu_button_down_arrow">
            </ImageView>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonSubButtonsLayout"
        android:background="@drawable/opened_menu_bg_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:visibility="gone">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu1">
            </ccom.myproject.control.XSubMenuButton>
            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu2">
            </com.myproject.control.XSubMenuButton>
            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu3">
            </com.myproject.control.XSubMenuButton>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonBottom"
        android:background="@drawable/opened_menu_bg_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
</LinearLayout>

I'm trying to create an expandable menu item in android, which will look like a button and on button click, button will expand to down with animation. I set an expand animation for the layout which i want to expand when clicked to my view and I have problem with animation. It doesn't start immediately when I clicked the view, and it starts when I scroll-down or scroll-up the container of the view. And if the container is not scrollable, my animation never starts. What am I doing wrong?

Here is my expand method, onClick method and the layout xml file for my custom view which will do this things:

expand:

public void expand(final View v) {

        try {
            Method m = v.getClass().getDeclaredMethod("onMeasure", int.class, int.class);
            m.setAccessible(true);
            m.invoke(v,
                    MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
                    MeasureSpec.makeMeasureSpec(((View)v.getParent()).getMeasuredWidth(), MeasureSpec.AT_MOST)
            );
        } catch (Exception e) {
            Log.e(TAG , "Caught an exception!", e);
        }
        final int initialHeight = v.getMeasuredHeight();
        Log.d("test", "initialHeight="+initialHeight);

        v.getLayoutParams().height = 0;
        v.setVisibility(View.VISIBLE);


        Animation a = new Animation() {

            @Override
            protected void applyTransformation(float interpolatedTime,
                    Transformation t) {
                final int newHeight = (int) (initialHeight * interpolatedTime);
                v.getLayoutParams().height = newHeight;
                v.requestLayout();
            }

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

        };

        a.setDuration(1000);
        a.setInterpolator(AnimationUtils.loadInterpolator(context,
                android.R.anim.accelerate_decelerate_interpolator));
        v.startAnimation(a);

        isExpanded = !isExpanded;

    }

onClick:

public void onClick(View v) {
    if (!isExpanded) {
        expand(subButtonsLayout);
    } else {
        collapse(subButtonsLayout);
    }
}

Layout xml for custom menu item view:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:mtx="http://schemas.android.com/apk/res/com.matriksdata.trademaster"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:gravity="center_horizontal">
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonTop"
        android:background="@drawable/opened_menu_bg_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
    <LinearLayout
        android:background="@drawable/opened_menu_bg_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical">
        <LinearLayout
            android:id="@+id/xExpandableMenuButtonTitle"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center_vertical">
            <TextView
                android:id="@+id/xExpandableMenuButtonText"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                        android:textAppearance="@style/expandable_menu_button_textstyle"
                android:text="Button Text">
            </TextView>
            <ImageView
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_weight="6"
                android:src="@drawable/menu_button_down_arrow">
            </ImageView>
        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonSubButtonsLayout"
        android:background="@drawable/opened_menu_bg_center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="center_vertical"
        android:visibility="gone">
        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:orientation="vertical">

            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu1">
            </ccom.myproject.control.XSubMenuButton>
            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu2">
            </com.myproject.control.XSubMenuButton>
            <com.myproject.control.XSubMenuButton
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                mtx:XSubMenuButtonText="SubMenu3">
            </com.myproject.control.XSubMenuButton>

        </LinearLayout>
    </LinearLayout>
    <LinearLayout
        android:id="@+id/xExpandableMenuButtonBottom"
        android:background="@drawable/opened_menu_bg_bottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
    </LinearLayout>
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

归属感 2024-11-07 21:32:06

如果您还没有解决问题,或者其他人也遇到过这个问题,这里有一个快速解决方案。每次点击时使父视图失效(在 onClick 方法中)。无论父级是否可滚动,这都应该有效。

也就是说,您的代码将类似于:


public void onClick(View v) {
    if (!isExpanded) {
        expand(subButtonsLayout);
    } else {
        collapse(subButtonsLayout);
    }
    rootView.invalidate();
}

If you haven't solved your problem or if anyone else comes across this problem, here is a quick solution. Invalidate your parent view on each click (in the onClick method). This should work regardless of if the parent is scrollable or not.

That is, your code will be something like:


public void onClick(View v) {
    if (!isExpanded) {
        expand(subButtonsLayout);
    } else {
        collapse(subButtonsLayout);
    }
    rootView.invalidate();
}
紅太極 2024-11-07 21:32:06

对于任何可能再次面临这种情况的人。

如果您的视图(您要设置动画的视图)处于消失状态,那么在这种情况下,在开始动画之前,将可见性设置为不可见。而且它会在第一次尝试时发挥作用。

来源自此处

To anyone who might face this again.

If your view, the one that you are animating is in the gone state, then in that case, before starting the animation, set Visiblity to invisible. And it will work in the first go.

Source from here.

虐人心 2024-11-07 21:32:06

我在布局末尾声明了一个视图,以使其 Z 索引保持在其同级视图之上。我必须触摸页面才能使动画生效。

于是我通过Java再次设置了Z索引,结果成功了。

view.bringToFront();

I had a view that I declared at the end of my layout to keep its Z index above its siblings. I had to touch the page to make the animation work.

So I set the Z index again through Java and it worked.

view.bringToFront();
在风中等你 2024-11-07 21:32:06

好吧,我只用了 2 行就解决了这个问题。

scroll.setFocusableInTouchMode(true); 
scroll.requestFocus();
animation.Start();//Start anim

祝你好运

Well, I fixed the problem using only 2 lines.

scroll.setFocusableInTouchMode(true); 
scroll.requestFocus();
animation.Start();//Start anim

Good luck

小镇女孩 2024-11-07 21:32:06

“如果容器不可滚动,则动画永远不会开始” -->看看我的类似问题, Scrollview 在以下情况下不滑动太短了,无法滚动。我发现我的情况是 Android 2.1 及以下版本中的触摸错误。

"If the container is not scrollable, the animation never starts" --> Have a look at my similar problem, Scrollview doesn't swipe when it's too short to scroll. I figured out in my case it's a touch bug in Android 2.1 and under.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文