Android 手电筒

layout:activity_main.xml

布局可以用imageButton , 使用背景图片

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
    <imageButton
        android:id="@+id/on_off_button"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_background="@drawalbe/off_button"/>
</RelativeLayout>

 MainActivity.java

package com.xiaohe.jayyounger.myflashlight;
import android.content.res.Resources;
import android.hardware.Camera;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends ActionBarActivity {
    ImageButton imageButton;
    //开闭按钮
    boolean isOpen = false;
    //相机控件
    Camera camera;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        imageButton = (ImageButton)this.findViewById(R.id.on_off_button);
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isOpen){
                    //关闭状态
                    //打开相机
                    camera = Camera.open();
                    //获取相机参数
                    Camera.Parameters parameters = camera.getParameters();
                    //设置闪光灯属性(持续亮)
                    parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    //设置相机参数
                    camera.setParameters(parameters);
                    //打开灯光
                    camera.startPreview();
                    //改变背景图片
                    imageButton.setImageDrawable(getResources().getDrawable(R.drawable.on_button));
                    //打开属性为true
                    isOpen = true;
                }else{
                    //打开状态
                    //关闭灯光
                    camera.stopPreview();
                    //关闭相机
                    camera.release();
                    //改变背景图片
                    imageButton.setImageDrawable(getResources().getDrawable(R.drawable.off_button));
                    //打开发生为false
                    isOpen = false;
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

 AndroidManifest.xml  添加访问权限

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.xiaohe.jayyounger.myflashlight" >
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.FLASHLIGHT" />

</manifest>

背景图片就不传了,可以要把自己喜欢的背景,更换图片。

你可能感兴趣的:(Android 手电筒)