手电筒项目开发一闪光灯
重点:
1、SurfaceView, OpenGL ES 的纹理------照相机捕获图像流纹理
2、style.xml配置文件。设置无标题等信息
<!--Applicationtheme.--><stylename="AppTheme"parent="AppBaseTheme"><itemname="android:windowNoTitle">true</item><itemname="android:windowFullscreen">true</item><itemname="android:windowBackground">@drawable/bg</item></style>
3、使用FrameLayout布局,<include/>标签引入子布局。
main.xml
<p><FrameLayoutxmlns:android="<atarget=_blankhref="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"xmlns:tools="<atarget=_blankhref="http://schemas.android.com/tools">http://schemas.android.com/tools</a>"android:layout_width="match_parent"android:layout_height="match_parent"android:background="@drawable/bg"><includelayout="@layout/mui_flashlight"/></p><p></FrameLayout></p>
ui_flashlight.xml
<?xmlversion="1.0"encoding="utf-8"?><FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:id="@+id/mui_flashlight"><ImageViewandroid:id="@+id/img_flashlight"android:layout_width="match_parent"android:layout_height="match_parent"android:src="@drawable/flashlight"/><ImageViewandroid:id="@+id/img_flashlight_controller"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_gravity="bottom|center_horizontal"android:onClick="onClick_Flashlight"/></FrameLayout>
4、使用flash.xml文件实现渐变效果,然后利用android:src="@drawable/flashlight",引入。
flash.xml
<?xmlversion="1.0"encoding="utf-8"?><transitionxmlns:android="http://schemas.android.com/apk/res/android"><itemandroid:drawable="@drawable/off"/><itemandroid:drawable="@drawable/on"/></transition>
5、功能代码
Flashlight.java
publicclassMyFlashLightextendsMyBaseActivity{@OverrideprotectedvoidonCreate(BundlesavedInstanceState){super.onCreate(savedInstanceState);iView.setTag(false);//获取屏幕的尺寸Pointpoint=newPoint();getWindowManager().getDefaultDisplay().getSize(point);LayoutParamslayoutParams=(LayoutParams)iViewController.getLayoutParams();//设置热点区的范围layoutParams.height=point.y*3/4;layoutParams.width=point.x/3;iViewController.setLayoutParams(layoutParams);}publicvoidonClick_Flashlight(Viewview){if(!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)){Toast.makeText(this,"当前设备没有闪光灯",Toast.LENGTH_SHORT).show();return;}if(((Boolean)iView.getTag())==false){openFlashlight();}else{closeFlashlight();}}privatevoidopenFlashlight(){//TransitionDrawable是一个特殊的Drawable对象,可以实现两个drawable资源之间淡入淡出的效果TransitionDrawabletDrawable=(TransitionDrawable)iView.getDrawable();//切换时间200毫秒tDrawable.startTransition(200);iView.setTag(true);try{//打开相机mCamera=Camera.open();inttexturId=0;//预览纹理默认给个值即可//照相机获取图像流纹理mCamera.setPreviewTexture(newSurfaceTexture(texturId));//启动预览mCamera.startPreview();//使用getParameters()获取Camera的缺省配置参数mParameters=mCamera.getParameters();//设置闪光灯模式mParameters.setFlashMode(mParameters.FLASH_MODE_TORCH);mCamera.setParameters(mParameters);}catch(IOExceptione){e.printStackTrace();}}privatevoidcloseFlashlight(){TransitionDrawabletDrawable2=(TransitionDrawable)iView.getDrawable();if((Boolean)iView.getTag()){//倒过来时间切换tDrawable2.reverseTransition(200);iView.setTag(false);if(mCamera!=null){//使用getParameters()获取Camera的缺省配置参数mParameters=mCamera.getParameters();//设置闪光灯模式mParameters.setFlashMode(Parameters.FLASH_MODE_OFF);mCamera.setParameters(mParameters);//停止预览mCamera.stopPreview();//释放相机对象mCamera.release();//为了使垃圾回收器尽快回收mCamera=null;}}}//退出程序关闭闪光灯@OverrideprotectedvoidonPause(){super.onPause();closeFlashlight();}}
完整代码见附件(进入我的资料即可下载):MyFlashLight.zip
实用代码参考:
http://blog.csdn.net/dclchj/article/details/7421778
http://www.cnblogs.com/over140/archive/2012/09/26/2611999.html
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。