【问题标题】:ViewPager swiping lags when fragment contains camera preview当片段包含相机预览时,ViewPager 滑动滞后
【发布时间】:2015-01-28 01:10:46
【问题描述】:

我有一个使用 ViewPager 和 3 个片段的 3 选项卡设置。其中一个片段实现了 QR 码扫描器 (ZBarScanner),它使用设备摄像头的实时视图填充整个片段。

我发现此相机视图会导致用户界面严重滞后。在标签之间滑动的动画要慢得多,并且应用程序的 CPU 使用率大幅增加。运行 traceview 显示扫描程序库的“onPreviewFrame”方法占用了大部分处理器时间。

我尝试过使用 offscreenPageLimit - 我发现需要将其设置为 2 以保持相机视图处于活动状态,否则由于反复启动和关闭相机视图而在滑动时会出现令人难以置信的严重延迟。

我可以做些什么来减少这个相机预览在我的应用程序中造成的延迟?

如果有帮助,我可以发布代码,但这一切都相当简单。

【问题讨论】:

  • 我可以做些什么来减少这个相机预览在我的应用程序中造成的延迟?你可以购买更强大的设备......这也是一个有点奇怪的用户界面。 ..刷到二维码扫描仪?而是调用扫描仪 Intent?
  • @Selvin 好点。但是,我正在使用我的 Nexus 5,它相当强大,并且在我运行的任何应用程序中都没有给我造成任何问题。所以这让我相信我可以优化处理我的应用程序的方式
  • @Selvin 关于您的编辑:我希望 QR 扫描是独立的,以获得无缝的用户体验。这将是应用程序试图实现的一个组成部分
  • 然后当片段正在交换时(或视图/片段不可见)在onPreviewFrame 中跳过处理(fx 设置了一些标志,我不知道,ViewPager.OnPageChangeListener.onPageScrollStateChanged 看起来很有希望)跨度>

标签: android android-fragments camera zbar


【解决方案1】:

我花了很多时间和咖啡,但找到了麻烦的原因。 使用 SurfaceView 显示预览的问题。

使用 TextureView 显示预览。

会有用的:How can I make my view pager more smooth?

祝你好运!

更新:

添加了 TextureView 的示例

CameraModule.java

public class CameraModule implements SurfaceTextureListener {
    private Camera mCamera;

    @Override
    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
        mCamera = getCamera();

        try {
            mCamera.setPreviewTexture(surface);
            mCamera.startPreview();
        } catch (IOException ioe) {
            // Something bad happened
        }
    }

    @Override
    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
        // Ignored, Camera does all the work for us
    }

    @Override
    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
        mCamera.stopPreview();
        mCamera.release();
        return true;
    }

    @Override
    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
        // Invoked every time there's a new Camera preview frame
    }

    private Camera getCamera() {
        Camera cam = null;

        try {
            cam = Camera.open();
        } catch (RuntimeException e) {
            loggerManager.error("Camera not available");
        }

        return cam;
    }
}

CameraFragment.java

public class CameraFragment extends Fragment {

    // You create an instance of the module. I use a singleton.
    CameraModule mCameraModule = new CameraModule();

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_camera, container, false);

        TextureView mTextureView = (TextureView) view.findViewById(R.id.camera_preview);
        mTextureView.setSurfaceTextureListener(mCameraModule);

        return view;
    }
}

fragment_camera.xml

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

    <TextureView
        android:id="@+id/camera_preview"
        android:layout_width="match_parent"
        android:background="#000"
        android:layout_height="match_parent" />


</RelativeLayout>

祝你好运!

【讨论】:

  • 这解决了您的问题吗?我遇到的另一个问题是当我离开包含带有相机片段(surfaceview)的viewpager的当前活动时,开始一个新的活动,然后我尝试返回......它很慢。至少有 2-3 秒的延迟。离开和返回活动时(可能是因为正在重新启动相机并松手)。我该如何解决?请帮忙!
猜你喜欢
  • 1970-01-01
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 2019-11-07
  • 1970-01-01
  • 2014-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多