1

我已经制作了一个手电筒应用程序,我想尝试制作插件以允许频闪灯功能。

我想在不同的按钮上设置它,而不是同一个。我想我需要使用计时器,但我从未使用过计时器,因为我是 Java 新手。

这是我的手电筒代码:

public class FlashLightActivity extends Activity {


private boolean isLighOn = false;
private Camera camera;
private Button button;

@Override
protected void onStop() {
    super.onStop();
    if (camera != null) {
        camera.release();
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    button = (Button) findViewById(R.id.buttonFlashlight);
    Context context = this;
    PackageManager pm = context.getPackageManager();

    if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        Log.e("err", "Device has no camera!");
        return;
    }

    camera = Camera.open();
    final Parameters p = camera.getParameters();

    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            if (isLighOn) {
                Log.i("info", "torch is turn off!");
                p.setFlashMode(Parameters.FLASH_MODE_OFF);
                camera.setParameters(p);
                camera.stopPreview();
                isLighOn = false;
            } else {
                Log.i("info", "torch is turn on!");
                p.setFlashMode(Parameters.FLASH_MODE_TORCH);
                camera.setParameters(p);
                camera.startPreview();
                isLighOn = true;
            }
        }
    });
    }
 }
4

1 回答 1

1

您可以使用处理程序

public class Strobe extends Activity {

    private LinearLayout mLinearLayout;

    private Handler mHander = new Handler();

    private boolean mActive = false;
    private boolean mSwap = true;   

    private final Runnable mRunnable = new Runnable() {

        public void run() {         
            if (mActive) {
                if (mSwap) {                    
                    mLinearLayout.setBackgroundColor(Color.WHITE);
                    mSwap = false;
                    mHander.postDelayed(mRunnable, 20);
                } else {
                    mLinearLayout.setBackgroundColor(Color.BLACK);
                    mSwap = true;
                    mHander.postDelayed(mRunnable, 100);
                }
            }           
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mLinearLayout = (LinearLayout) findViewById(R.id.strobe);
        startStrobe();
    }

    private void startStrobe() {        
        mActive = true;
        mHander.post(mRunnable);
    }
}

为活动设置主题以使其全屏显示。

android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
于 2012-12-28T02:07:33.537 回答