Android - 下一个活动的视图显示得太早

时间:2016-07-28 21:13:01

标签: android android-intent android-camera

我有一个按钮,按下时将MainActivity更改为包含相机预览和进度条的CameraActivity。但是,在活动切换之前,会出现进度条,因此您最终会在MainActivity上看到进度条一瞬间。可能是什么导致了这个?感谢

这是一些相关的代码。

MainActivity.java

public void openCamera(View view) {
    if (getCameraInstance() == null) {
        Toast.makeText(FeedActivity.this, "Camera is unavailable right now", Toast.LENGTH_LONG).show();
    } else {
        Intent i = new Intent(MainActivity.this, CameraActivity.class);
        startActivity(i);
    }
}

CameraActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    getSupportActionBar().hide();

    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    animation = ObjectAnimator.ofInt(progressBar, "progress", 0, 100);
    animation.setDuration(10000);
    animation.setInterpolator(new LinearInterpolator());

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera, currentCameraId);
    preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mPreview);


}

/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance(){
    Camera c = null;
    try {
        c = Camera.open(); // attempt to get a Camera instance
    }
    catch (Exception e){
        // Camera is not available (in use or does not exist)
    }
    return c; // returns null if camera is unavailable
}

1 个答案:

答案 0 :(得分:1)

检查此答案 - Is there any way to listen for when the camera has loaded in? cwac-camera

相机加载速度比标准视图慢,所以我的主张是创建加载视图(在你的布局中专门为此创建一些ViewGroup)并且默认情况下它应该是可见的(当相机加载到较长时,这样的加载视图更加用户友好),进度条应隐藏在默认状态,接下来当相机准备就绪时(检查链接问题)显示进度条并隐藏加载视图。

一些代码

mCamera.setOneShotPreviewCallback(new Camera.PreviewCallback() {
    @Override
    public void onPreviewFrame(byte[] data, Camera camera) {
        // camera is "loaded" 
        //show progressbar
        //hide loading screen
    });