Android使用背景图片时侧滑菜单慢的问题

4
我使用来自https://github.com/jfeinstein10/SlidingMenu的滑动菜单库。它工作得很好,除了一个小问题:当我将滑动菜单附加到具有图像背景的活动时,它开始滞后。当我向右或向左滑动时,菜单需要几秒钟才能响应。有人之前见过这个问题吗?非常感谢任何帮助。

我使用大小约为650 KB的PNG图像,但我也尝试使用不到20 KB的低质量图片,但问题仍然存在。

我的Min SDK是13,目标SDK是17(我还尝试更改这些值,但没有帮助)

这是一个使用滑动菜单的我的活动的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="@drawable/my_background"
  android:orientation="vertical" >

如果我删除android:background值,菜单就可以正常滑动。


这不是对你问题的回答。这是一个替代SlidingMenu的选择。Android support library 提供了一个类似的小部件叫做SlidingPaneLayout - Wenhui
有人有这个问题的解决方案吗? - Lakshmanan
2个回答

2

您应该为每个drawable文件夹(如drawable-mdpi,drawable-hdpi,drawable-xhdpi等)添加单独的背景图片。我测试过只在基本的drawable文件夹中添加一个图片,打开和关闭速度非常慢,并且不流畅。如果您将不同尺寸的背景图片添加到所有drawable文件夹中,它会像魔法般地工作。


1
你是我的英雄!滑动菜单的迟缓让我疯了一个月,我将所有图像添加到mdpi、hdpi等中,一切都正常工作了!再次感谢你,给你点赞+1! - Haifeng Zhang

0
如果有人(像我一样)仍然遇到滑动菜单和背景图像的问题,我会尝试解释一下我是如何解决这个问题的。@netis的解决方案对我没有帮助。 正如您所知道的,如果您不在滑动菜单中使用背景,则问题会消失,因此我们需要使用其他东西来代替标准的Android背景。我使用TextureView来实现这一点。 在我的菜单xml中,我添加了:
<TextureView
    android:id="@+id/menu_texture_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

在活动代码中初始化菜单时添加:

final TextureView texture = (TextureView) menuView.findViewById(R.id.menu_texture_view);

    final Drawable picture = getResources().getDrawable(R.drawable.menu_background);

    texture.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
        @Override
        public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
            Canvas canvas = texture.lockCanvas();
            picture.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
            picture.draw(canvas);
            texture.unlockCanvasAndPost(canvas);
        }

        @Override
        public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {

        }

        @Override
        public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
            return false;
        }

        @Override
        public void onSurfaceTextureUpdated(SurfaceTexture surface) {

        }
    });

基本上我们将背景放入TextureView中,而不是使用标准的安卓方法(android:backgroundimageView等)。

另外,作为建议,您需要添加所有dpi(mdpi、hpdi等)的背景以获得更好的性能。

我知道这个解决方案很丑陋,但当没有其他帮助时,它对我有用...


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接