android 闪光灯编程
2016-07-16 19:50
267 查看
关于Camera的UI已经失效。
package com.example.flashlight; import java.io.IOException; import android.content.pm.PackageManager; import android.graphics.Point; import android.graphics.SurfaceTexture; import android.graphics.drawable.TransitionDrawable; import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.os.Bundle; import android.view.View; import android.view.ViewGroup.LayoutParams; import android.widget.Toast; public class FlashLight extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); mImageViewFlashLight.setTag(false); Point point = new Point(); getWindowManager().getDefaultDisplay().getSize(point); LayoutParams layoutParams = (LayoutParams) mImageViewFlashLightController.getLayoutParams(); System.out.println(layoutParams.height); System.out.println(layoutParams.width); layoutParams.height = point.y * 3 / 4; layoutParams.width = point.x * 1 / 3; mImageViewFlashLightController.setLayoutParams(layoutParams); } public void onClick_flashlight(View view) { if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) { Toast.makeText(this, "当前设备没有闪光灯", Toast.LENGTH_LONG).show(); return; } if ((Boolean) mImageViewFlashLight.getTag()==false) { openFlashLight(); }else { closeFlashLight(); } } protected void openFlashLight() { TransitionDrawable drawable = (TransitionDrawable) mImageViewFlashLight.getDrawable(); drawable.startTransition(200); mImageViewFlashLight.setTag(true); mCamera = Camera.open(); int textureId = 0; try { mCamera.setPreviewTexture(new SurfaceTexture(textureId)); mCamera.startPreview(); mParameters = mCamera.getParameters(); mParameters.setFlashMode(mParameters.FLASH_MODE_TORCH); mCamera.setParameters(mParameters); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } protected void closeFlashLight() { TransitionDrawable drawable = (TransitionDrawable) mImageViewFlashLight.getDrawable(); if ((Boolean) mImageViewFlashLight.getTag()) { drawable.reverseTransition(200); mImageViewFlashLight.setTag(false); if (mCamera != null) { mParameters = mCamera.getParameters(); mParameters.setFlashMode(Parameters.FLASH_MODE_OFF); mCamera.setParameters(mParameters); mCamera.stopPreview(); mCamera.release(); mCamera = null; } } } @Override protected void onPause() { // TODO Auto-generated method stub super.onPause(); closeFlashLight(); } }
相关文章推荐
- Android学习笔记--AIDL
- Android——RuntimePermission介绍
- Android studio gradle 环境变量配置
- 安卓项目之淘忆的项目来源
- Android 中this的用法
- 自定义Toast的显示时长
- 通讯录
- Android属性动画完全解析
- View详解之二View的事件体系
- AndroidStudio的安装配置&&注意要点
- Android实现静默安装与卸载
- android开源框架
- Android开发---Vitamio框架的实战应用
- Android开发学习笔记:5大布局方式详解
- Android RecyclerView 使用例子
- Android 计算器解析(三): 美化计算器界面
- Android异常-android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.Bin
- android note
- Android开发笔记(一百一十一)聊天室中的Socket通信
- 自学之——SurfaceView