Android 手电筒 附源码

//图片用Draw-9-Path处理了下。
package com.myflashlight;
import android.hardware.Camera;
import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

public class MyFlashlight extends Activity {
	Button on_off;
    Camera camera;
    public static final long longTime=2000,shortTime=1000,pauseTime=200;
    boolean isOn=false;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		noTitle();
		setContentView(R.layout.my_flashlight);
		initView();
		addListener();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.my_flashlight, menu);
		return true;
	}
//销毁进程	
	@Override
	protected void onDestroy() {
		System.exit(0);
		// TODO Auto-generated method stub
		super.onDestroy();
	}
//返回键退出
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
	    while (keyCode==event.KEYCODE_BACK) {
	    	onDestroy();
		}
		return super.onKeyDown(keyCode, event);
	}
//去标题
    public void noTitle(){
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }
    
	private void initView(){
		on_off=(Button) findViewById(R.id.on_off);		
	}
	
	private void addListener(){
		on_off.setOnClickListener(new FlashlightOnClick());
	}

	private class FlashlightOnClick implements View.OnClickListener{

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.on_off:
				if(!isOn){
					BtnStyle_Off();
					openFlashlight();
				}else if(isOn){
					BtnStyle_On();
					closeFlashlight();
				}
				break;

			default:
				break;
			}
		}
		
	}
	//开关开启样式
	private void BtnStyle_On(){
		on_off.setText(R.string.turnOn);
		on_off.setTextColor(getResources().getColor(R.color.darkblue));
		on_off.setBackgroundResource(R.drawable.open);
	}
	//开关关闭样式
	private void BtnStyle_Off(){
		on_off.setText(R.string.turnOff);
		on_off.setTextColor(getResources().getColor(R.color.darkred));
		on_off.setBackgroundResource(R.drawable.close);
	}
	//开启手电筒
	private void openFlashlight(){
		camera=Camera.open();
		Camera.Parameters parameters=camera.getParameters();
		parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
		camera.setParameters(parameters);
		camera.startPreview();
		isOn=true;
		Toast.makeText(MyFlashlight.this,"已开启", 2000).show();
	}
	//关闭手电筒
	private void closeFlashlight(){
		Camera.Parameters parameters=camera.getParameters();
		parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
		camera.setParameters(parameters);
		camera.stopPreview();
		camera.release();
		isOn=false;
		Toast.makeText(MyFlashlight.this,"已关闭", 2000).show();
	}

	
}

 

你可能感兴趣的:(android)