一起學android之如何以最簡單方式開啟閃光燈(35)

來源:互聯網
上載者:User

標籤:parameters   camera   閃光燈   android   java   

我們以360手機小幫手中有一款叫“最美手電筒”,:

                              

通過圖中手電筒的開關來開啟或關閉閃光燈,OK,以下是實現此功能的代碼:

public class MainActivity extends Activity implements OnClickListener {private ImageView iv_off_on;private ImageView iv_light;private Parameters parameter = null;private Camera camera = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);requestWindowFeature(Window.FEATURE_NO_TITLE);setContentView(R.layout.activity_main);initView();initListener();}private void initListener() {iv_off_on.setOnClickListener(this);}private void initView() {iv_off_on = (ImageView) findViewById(R.id.iv_off_on);iv_light = (ImageView) findViewById(R.id.iv_light);iv_light.setVisibility(View.INVISIBLE);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.iv_off_on:if (iv_off_on.isSelected()) {// 關閉iv_off_on.setSelected(false);iv_light.setVisibility(View.INVISIBLE);parameter.setFlashMode(Parameters.FLASH_MODE_OFF);camera.setParameters(parameter);camera.release();} else {// 開啟iv_off_on.setSelected(true);iv_light.setVisibility(View.VISIBLE);camera = Camera.open();camera.startPreview();parameter = camera.getParameters();parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);camera.setParameters(parameter);}break;default:break;}}}


最後在AndroidManifest.xml中添加相應的許可權:

<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.autofocus" />


轉載請註明出處:http://blog.csdn.net/hai_qing_xu_kong/article/details/45504303 情緒控_

一起學android之如何以最簡單方式開啟閃光燈(35)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.