android 手电筒 开源,Android手电筒
packagecom.example.torch;/***@author'jacky'**/importjava.io.File;importjava.io.FileOutputStream;importjava.text.*;importandroid.app.Activity;importandroid.graphics.Color;importandroid.hardwar...
package com.example.torch;
/**
* @author 'jacky'
*
*/
import java.io.File;
import java.io.FileOutputStream;
import java.text.*;
import android.app.Activity;
import android.graphics.Color;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener,OnTouchListener,OnGestureListener {
private Button switcher;
private Camera camera;
public Parameters parameters;
public boolean isopen = false;
public long exitme = 0;
private Bitmap bmp;
public GestureDetector decetor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 设置全屏模式和屏幕常亮
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
View mainView = findViewById(R.id.testid);
decetor=new GestureDetector(this);
decetor.setIsLongpressEnabled(true);
decetor.isLongpressEnabled();
// 设置主界面的背景
mainView.setBackgroundColor(Color.BLACK);
switcher = (Button) findViewById(R.id.switcher);
// 给开关button添加监听
switcher.setOnTouchListener(this);
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit();
return false;
}
return super.onKeyDown(keyCode, event);
}
public void exit() {
/**
* 使用时间差判断退出 2000ms
*/
if (System.currentTimeMillis() - exitme > 2000) {
Toast.makeText(this, R.string.tips, Toast.LENGTH_LONG).show();
exitme = System.currentTimeMillis();
} else {
if (camera != null) {
camera.release();
camera = null;
}
finish();
System.exit(0);
}
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
if(e1.getY()-e2.getY()>1)
{
if (!isopen) {
camera = Camera.open();
// 获取camera 的参数
parameters = camera.getParameters();
// 设置闪光灯为手电筒模式
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
// 更新button背景
switcher.setBackgroundResource(R.drawable.open);
// 开启手电筒
camera.startPreview();
isopen = true;
}
}
if(e2.getY()-e1.getY()>1)
{
if(isopen)
{
switcher.setBackgroundResource(R.drawable.close);
// 获取camera的初始参数
// parameters = camera.getParameters();
// 设置闪光灯为关闭
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
// 给camera添加参数
camera.setParameters(parameters);
// 释放camera对象
camera.release();
camera = null;
isopen = false;
}
}
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return decetor.onTouchEvent(event);
}
}
通过ontouch添加手势判断方式控制开关状态。onGesture onTouch 两者的结合。
更多推荐
所有评论(0)