萬盛學電腦網

 萬盛學電腦網 >> 網絡編程 >> 安卓開發 >> Android 通用型手電筒代碼

Android 通用型手電筒代碼

   說到手機手電筒功能,很多人都是直接調用閃光燈,而本文給大家介紹的是用相機功能來實現的,有需要的小伙伴可以參考下。

  前幾天這邊一個小區斷電,黑麻麻的,一只貓闖進寢室,把我嚇哭,然後就果斷寫了個手電筒,下次斷電,再來,我要嚇死它。

  我之前看到很多人都是直接調用閃光燈,其實大部分機型都不支持,我這個是用相機功能來實現的,大部分機型都支持。

  MainActivity.class

  ?

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 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 hol
copyright © 萬盛學電腦網 all rights reserved