Android活动需要时间加载

时间:2016-10-03 12:45:24

标签: java android

我的应用中有3 activities

向左/向右滑动时的第二个显示其他活动。中心活动(第二个)将相机作为背景(背景显示相机看到的内容)。

当我从这个activity导航到另一个时,它们会快速打开,但从其他活动回到这个时间需要时间。

如何优化相机加载时间?

MainActivity.java //刚刚拥有相机背景

package com.example.nick.myapplication;
import android.view.GestureDetector;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

String TAG = "TAG";

private GestureDetectorCompat gestureDetectorCompat; // ####################################

private Camera mCamera;
private CameraPreview mPreview;
private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        // this device has a camera
        return true;
    } else {
        // no camera on this device
        return false;
    }
}


public static Camera getCameraInstance() {
    Camera c = null;
    try {
        c = Camera.open(0); // 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
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    gestureDetectorCompat = new GestureDetectorCompat(this, new MyGestureListener());
    mCamera = getCameraInstance();

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

}
@Override // ############################################################# {{{
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetectorCompat.onTouchEvent(event);
    return super.onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
    //handle 'swipe left' action only

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2,
                           float velocityX, float velocityY) {

     /*
     Toast.makeText(getBaseContext(),
      event1.toString() + "\n\n" +event2.toString(),
      Toast.LENGTH_SHORT).show();
     */

        if(event2.getX() < event1.getX()){
            Toast.makeText(getBaseContext(),
                    "Swipe left - startActivity()",
                    Toast.LENGTH_SHORT).show();

            //switch another activity
            Intent intent = new Intent(
                    MainActivity.this, Main3Activity.class);
            startActivity(intent);
        }
        if(event2.getX() > event1.getX()){
            Toast.makeText(getBaseContext(),
                    "Swipe left - startActivity()",
                    Toast.LENGTH_SHORT).show();

            //switch another activity
            Intent intent = new Intent(
                    MainActivity.this, Main4Activity.class);
            startActivity(intent);
        }

        return true;
    }
} // ############################################################# }}}

Main3Activity.java //只有一个textview并轻扫以更改活动手势

package com.example.nick.myapplication;

import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v7.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.FrameLayout;
import android.widget.Toast;

public class Main3Activity extends AppCompatActivity {
private GestureDetectorCompat gestureDetectorCompat;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main3);
    gestureDetectorCompat = new GestureDetectorCompat(this, new MyGestureListener());
}
@Override // ############################################################# {{{
public boolean onTouchEvent(MotionEvent event) {
    this.gestureDetectorCompat.onTouchEvent(event);
    return super.onTouchEvent(event);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
    //handle 'swipe left' action only

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2,
                           float velocityX, float velocityY) {

        if(event2.getX() > event1.getX()){

            //switch another activity
            Intent intent = new Intent(
                    Main3Activity.this, MainActivity.class);
            startActivity(intent);
        }

        return true;
    }
} // #############################################################     }}}
}
}

1 个答案:

答案 0 :(得分:0)

您正在活动用户界面中加载相机。异步加载相机。