手電筒項目開發一閃光燈

重點:

1、SurfaceView, OpenGL ES 的紋理------照相機捕獲圖像流紋理

2、style.xml配置文件。設置無標題等信息

<!-- Application theme. -->

    <style name="AppTheme" parent="AppBaseTheme">

      <item name="android:windowNoTitle">true</item>

      <item name="android:windowFullscreen">true</item>

      <item name="android:windowBackground">@drawable/bg</item>

    </style>

 

3、使用FrameLayout佈局,<include/>標籤引入子佈局。
main.xml

<p><FrameLayout xmlns:android="<a target=_blank href="http://schemas.android.com/apk/res/android">http://schemas.android.com/apk/res/android</a>"

    xmlns:tools="<a target=_blank href="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"

    >

<include layout="@layout/mui_flashlight"/></p><p></FrameLayout></p>

 

ui_flashlight.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/mui_flashlight"
    >
    <ImageView 
        android:id="@+id/img_flashlight"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/flashlight"
        
        />
    <ImageView 
        android: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

<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:drawable="@drawable/off"/>
    <item android:drawable="@drawable/on"/>
</transition>

 

 

5、功能代碼

Flashlight.java

public class MyFlashLight extends MyBaseActivity {
 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  
  iView.setTag(false);
  
  //獲取屏幕的尺寸
  Point point  = new Point();
  getWindowManager().getDefaultDisplay().getSize(point);
  LayoutParams layoutParams = (LayoutParams) iViewController.getLayoutParams();
  //設置熱點區的範圍
  layoutParams.height = point.y * 3/4;
  layoutParams.width = point.x /3;
  iViewController.setLayoutParams(layoutParams);
 }
 
 public void onClick_Flashlight(View view){
  if (!getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
   
   Toast.makeText(this, "當前設備沒有閃光燈", Toast.LENGTH_SHORT).show();
   return;
  }
  if (((Boolean)iView.getTag()) == false) {
   openFlashlight();
  } else {
   closeFlashlight();
  }
 }
 private void openFlashlight() {
  //TransitionDrawable是一個特殊的Drawable對象,可以實現兩個drawable資源之間淡入淡出的效果
  TransitionDrawable tDrawable = (TransitionDrawable) iView.getDrawable();
  //切換時間200毫秒
  tDrawable.startTransition(200);
  iView.setTag(true);
  
  try {
   //打開相機
   mCamera = Camera.open();
   int texturId = 0; //預覽紋理默認給個值 即可
   //照相機獲取圖像流紋理
   mCamera.setPreviewTexture(new SurfaceTexture(texturId));
   //啓動預覽
   mCamera.startPreview();
   //使用getParameters()獲取Camera 的缺省配置參數
   mParameters = mCamera.getParameters();
   //設置閃光燈模式
   mParameters.setFlashMode(mParameters.FLASH_MODE_TORCH);
   mCamera.setParameters(mParameters);
  } catch (IOException e) {
   e.printStackTrace();
  }
  
  
  
 }
 
 
 private void closeFlashlight() {
  TransitionDrawable tDrawable2 = (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;
    
   }
  }
  
 }
 //退出程序關閉閃光燈
 @Override
 protected void onPause() {
  super.onPause();
  closeFlashlight();
 }
}

 

 

 完整代碼見附件(進入我的資料即可下載):MyFlashLight.zip

 實用代碼參考:

http://blog.csdn.net/dclchj/article/details/7421778

http://www.cnblogs.com/over140/archive/2012/09/26/2611999.html

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章