package com.example.angluswang.superflashlight;

import android.content.pm.PackageManager;
import android.graphics.Point;
import android.graphics.SurfaceTexture;
import android.graphics.drawable.TransitionDrawable;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import java.io.IOException;

/**
 * Created by Jeson on 2016/5/30.
 */

public class FlashActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        imgFlashlight.setTag(false); // 设置闪光灯 默认是关闭状态

        setFlashControllerSize();
    }

    /**
     * 检查设备是否支持闪光灯
     */
    public void check_flashlight(View v) {
        // 检测当前设备是否支持闪光灯
        if (!getPackageManager().hasSystemFeature(
                PackageManager.FEATURE_CAMERA_FLASH)) {
            Toast.makeText(FlashActivity.this, "没有闪光灯",
                    Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(FlashActivity.this, "可以使用闪光灯",
                    Toast.LENGTH_SHORT).show();
        }

        // 打开、关闭闪光灯
        if ((Boolean) imgFlashlight.getTag() == false) {
            openFlash();
        } else {
            closeFlash();
        }
    }

    /**
     * 根据屏幕来 动态 设置热区域的宽和高
     */
    private void setFlashControllerSize() {
        Point point = new Point();
        getWindowManager().getDefaultDisplay().getSize(point);  // 获得屏幕的像素

        ViewGroup.LayoutParams lp = imgFlashController.getLayoutParams();
        lp.width = point.x / 5;
        lp.height = point.y / 6;
        imgFlashController.setLayoutParams(lp);
    }

    /**
     * 打开闪光灯
     */
    protected void openFlash() {
        TransitionDrawable transDraw = (TransitionDrawable) imgFlashlight.getDrawable();
        transDraw.startTransition(300);     // 播放图片到开启状态
        imgFlashlight.setTag(true);

        try {
            mCamera = android.hardware.Camera.open();

            int textureId = 0;
            mCamera.setPreviewTexture(new SurfaceTexture(textureId));
            mCamera.startPreview();

            mParameters = mCamera.getParameters();
            mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);

            mCamera.setParameters(mParameters);


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 关闭闪光灯
     */
    protected void closeFlash() {
        TransitionDrawable transDraw = (TransitionDrawable) imgFlashlight.getDrawable();
        if ((Boolean) imgFlashlight.getTag()) {
            transDraw.reverseTransition(300);   //回滚图片到关闭状态
            imgFlashlight.setTag(false);
            if (mCamera != null) {
                mParameters = mCamera.getParameters();
                mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

                mCamera.setParameters(mParameters);

                //释放资源
                mCamera.stopPreview();
                mCamera.release();
                mCamera = null;
            }

        }
    }
}

@@ -3081,11 +3081,139 @@
  %7D%0A    %7D
+%0A%0A    @Override%0A    protected void onPause() %7B%0A        super.onPause();%0A%0A        //%E5%BD%93%E5%89%8D%E6%B4%BB%E5%8A%A8%E5%A4%B1%E5%8E%BB%E7%84%A6%E7%82%B9%E6%97%B6%EF%BC%8C%E8%87%AA%E5%8A%A8%E5%85%B3%E9%97%AD%E9%97%AA%E5%85%B3%E7%81%AF%0A        closeFlash();%0A    %7D
 %0A%7D%0A

添加失去焦点时 自动关闭闪关灯 功能