Android 通用型手电筒代码
2018-07-20 来源:open-open
用相机功能来实现的,大部分机型都支持。
MainActivity.class
MainActivity.class
public class MainActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (isFlashSupported()) { if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction().add(R.id.container, new FlashlightFragment()).commit(); } } else { showNoFlashAlert(); } } private void showNoFlashAlert() { new AlertDialog.Builder(this) .setMessage(R.string.device_not_support_msg) .setIcon(android.R.drawable.ic_dialog_alert).setTitle(R.string.device_not_support_title) .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); finish(); } }).create().show(); } private boolean isFlashSupported() { PackageManager pm = getPackageManager(); return pm.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH); } public static class FlashlightFragment extends Fragment implements SurfaceHolder.Callback { public FlashlightFragment() { } ImageButton btnSwitch; ImageView bg; private boolean isLighOn = false; private Camera camera; Camera.Parameters params; @Override public void onStart() { super.onStart(); SurfaceView preview = (SurfaceView) getView().findViewById(R.id.PREVIEW); SurfaceHolder mHolder = preview.getHolder(); mHolder.addCallback(this); } @Override public void onPause() { super.onPause(); turnOffFlash(); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_main_activity, container, false); // flash switch button btnSwitch = (ImageButton) view.findViewById(R.id.flashlight_button); bg = (ImageView) view.findViewById(R.id.flashlight_bg); // displaying button image toggleBgImage(); // Switch button click event to toggle flash on/off btnSwitch.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (isLighOn) { turnOffFlash(); } else { turnOnFlash(); } } }); return view; } // Turning On flash private void turnOnFlash() { if (!isLighOn) { if (camera == null || params == null) { return; } params = camera.getParameters(); params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.startPreview(); isLighOn = true; toggleBgImage(); } } // Turning Off flash private void turnOffFlash() { if (isLighOn) { if (camera == null || params == null) { return; } params = camera.getParameters(); params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); camera.setParameters(params); camera.stopPreview(); isLighOn = false; toggleBgImage(); } } private void toggleBgImage() { if (isLighOn) { bg.setImageResource(R.mipmap.f_on); } else { bg.setImageResource(R.mipmap.f_off); } } @Override public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) { } @Override public void surfaceDestroyed(SurfaceHolder holder) { if (camera != null) { camera.stopPreview(); camera.setPreviewCallback(null); camera.release(); camera = null; } } @Override public void surfaceCreated(SurfaceHolder holder) { if (camera == null) { camera = Camera.open(); params = camera.getParameters(); try { camera.setPreviewDisplay(holder); } catch (IOException e) { camera.release(); camera = null; } } turnOnFlash(); } } }
标签: isp
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
下一篇:python递归删除指定目录
最新资讯
热门推荐