【问题标题】:Android, ObjectAnimator lagging on two overlapping LayoutsAndroid,ObjectAnimator 滞后于两个重叠的布局
【发布时间】:2012-10-26 07:37:16
【问题描述】:

我的应用程序在两个重叠布局上的平滑度方面存在问题。我建立了某种幻灯片菜单(如 Facebook 或 YouTube)。此菜单包含两种布局:显示数据的主布局并包括其下方的菜单布局。然后是菜单布局本身,一个带有自定义行布局的普通 ListView。

主要:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<include
        android:id="@+id/menu_list"
        layout="@layout/menu_list"/>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:id="@+id/news_content"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
              android:background="#FFF"
              android:orientation="vertical">
    <ListView
            android:id="@android:id/list"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

 </LinearLayout>

 <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="horizontal"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

    <ProgressBar
            android:id="@+id/NewsProgressBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:visibility="gone"
            style="@android:style/Widget.Holo.Light.ProgressBar.Large"/>

 </LinearLayout>

</RelativeLayout>

菜单:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:layout_width="250dip"
          android:layout_height="fill_parent"
          android:orientation="vertical"
          android:background="@drawable/ic_sidebar_bg">

<RelativeLayout
        android:layout_height="wrap_content"
        android:layout_width="wrap_content">

    <ImageView
            android:alpha="0.8"
            android:layout_height="40dp"
            android:layout_width="match_parent"
            android:background="#222222"/>

    <TextView
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:layout_marginLeft="10dip"
            android:layout_marginTop="12dip"
            android:textSize="14dp"
            android:textColor="#666"
            android:text="@string/menu_cat_main"/>

    <ImageView
            android:layout_height="1dp"
            android:layout_marginTop="40dp"
            android:layout_width="match_parent"
            android:background="#666666"/>

</RelativeLayout>

<ListView
        android:id="@+id/mainMenuList"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

我已经认识到这种重叠会导致 ListView 严重滞后。我所做的是让菜单包含“visibility:gone”,这使得 ListViews 工作得非常好。但是,如果我尝试将内容侧向滑动,则包含(菜单)会再次可见,并且幻灯片不会平滑。但除了这个外观问题之外,菜单的工作方式与 YouTube 中的完全一样。

为了明确以下代码: viewContent (View) 包含 LinearLayout 及其内容 viewMenu (View) 包含菜单布局包括 canSlide (bool) 菜单是否可以通过触摸滑动(触摸屏幕的特定区域时为真) isMenuScrolled (bool) 菜单是否滑出时

这里是触摸事件的代码:

public boolean dispatchTouchEvent(MotionEvent touchevent) {

    switch (touchevent.getAction()) {
        case MotionEvent.ACTION_MOVE:

            if (canSlide) {
                if (touchevent.getX() < viewMenu.getWidth()) {
                    viewContent.setX(touchevent.getX());
                } else if (touchevent.getX() < 0) {
                    viewContent.setX(0);
                } else if (touchevent.getX() > viewMenu.getWidth()) {
                    viewContent.setX(viewMenu.getWidth());
                }

                viewContent.setEnabled(false);
            }
            return true;

        case MotionEvent.ACTION_DOWN:

            if (touchevent.getY() > 200 && touchevent.getX() < 50 && !isMenuScrolled) {
                viewMenu.setVisibility(View.VISIBLE);
                touchStart = touchevent.getX();
                canSlide = true;
            } else if (touchevent.getY() > 200 && touchevent.getX() > viewMenu.getWidth() && isMenuScrolled) {
                viewMenu.setVisibility(View.VISIBLE);
                touchStart = touchevent.getX();
                canSlide = true;
            } else {
                canSlide = false;
            }
            return true;

        case MotionEvent.ACTION_UP:

            if (viewContent.getX() != 0 || viewContent.getX() != viewMenu.getWidth()) {
                if (viewContent.getX() < touchStart) {
                    ObjectAnimator transAnimation = ObjectAnimator.ofFloat(viewContent, "x", viewContent.getX(), 0);

                    transAnimation.addListener(new Animator.AnimatorListener() {

                        @Override
                        public void onAnimationStart(Animator animator) {
                            viewContent.setEnabled(false);
                            isMenuScrolled = false;
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            viewMenu.setVisibility(View.GONE);
                            viewContent.setEnabled(true);
                        }

                        @Override
                        public void onAnimationCancel(Animator animator) {
                            // nothing
                        }

                        @Override
                        public void onAnimationRepeat(Animator animator) {
                            // nothing
                        }

                    });

                    transAnimation.setDuration(300);
                    transAnimation.start();

                } else if (viewContent.getX() > touchStart) {
                    ObjectAnimator transAnimation = ObjectAnimator.ofFloat(viewContent, "x", viewContent.getX(), viewMenu.getWidth());

                    transAnimation.addListener(new Animator.AnimatorListener() {

                        @Override
                        public void onAnimationStart(Animator animator) {
                            viewContent.setEnabled(false);
                            isMenuScrolled = true;
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            viewContent.setEnabled(true);
                        }

                        @Override
                        public void onAnimationCancel(Animator animator) {
                            // nothing
                        }

                        @Override
                        public void onAnimationRepeat(Animator animator) {
                            // nothing
                        }

                    });

                    transAnimation.setDuration(300);
                    transAnimation.start();
                }
            }
            return true;
    }

    return true;
}

如何解决此问题,使其滚动更流畅? 最后一件事,在滚动菜单时禁用内容的最佳方法是什么? viewContent.setEnabled 无法正常工作。

提前致谢!

【问题讨论】:

    标签: java android android-layout listview animation


    【解决方案1】:

    试试:

    在清单的应用标签下添加android:hardwareAccelerated="true"

    和:myViewContainer.setLayerType(View.LAYER_TYPE_HARDWARE, null);

    在某些环境情况下,这可能会更顺畅,试试你的运气。

    【讨论】:

      猜你喜欢
      • 2013-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-18
      相关资源
      最近更新 更多