实现手电筒Flash Light 关键代码

  1. 云栖社区>
  2. 博客>
  3. 正文

实现手电筒Flash Light 关键代码

zting科技 2017-10-05 11:20:00 浏览976
展开阅读全文

实现手电筒Flash Light 关键代码

实现Flash的逻辑

1
2
3
4
5
6
7
8
9
10
11
12
13
14
view.setOnClickListener(new OnClickListener() { 
          @Override 
            public void onClick(View v) { 
                if(!isOpen){ 
                    view.setBackgroundResource(R.drawable.open);    //改变背景图片 
                openFlash();          
                    isOpen = true
               }else
                    view.setBackgroundResource(R.drawable.off);     //改变背景图片 
                    closeFlash();                                     
                    isOpen = false
                
            
        }); 

  

打开手电筒

1
2
3
4
5
6
7
8
private void openFlash(){              
     camera = Camera.open();  //打开照相机              
     Parameters params = camera.getParameters();                
     params.setFlashMode(Parameters.FLASH_MODE_TORCH);
     //设置照相机参数,FLASH_MODE_TORCH  持续的亮灯,FLASH_MODE_ON 只闪一下             
     camera.setParameters(params);           
     camera.startPreview();      //开始亮灯 
}

  

关掉手电筒

1
2
3
4
5
6
private void closeFlash(){
    if(camera != null){
      camera.stopPreview();  //关掉亮灯                
    camera.release();   //关掉照相机 
     }    
}

  

Release:

1
2
3
4
5
private void release(){
    if(camera != null){         
    camera.release();       
   
}

 参考:android小记之FlashLight --- 高仿小米手电筒


本文转自Work Hard Work Smart博客园博客,原文链接:http://www.cnblogs.com/linlf03/p/3682385.html,如需转载请自行联系原作者

网友评论

登录后评论
0/500
评论
zting科技
+ 关注