文章目录
  1. 1. MyActivity
  2. 2. main.xml
  3. 3. 添加权限
  4. 4. 查看源码

前几天晚上在外面需要照明,记得以前朋友手机上都有个手电筒的小工具,就决定在自己的android机上安装一个。本来还想去去应用市场的,但是想来自己也会一点android开发,只是好久不写了,可以趁机自己开发一个手电筒。

网上稍微查了一下资料,发现做一个手电筒实在是太简单了,主要是使用android系统的Camera闪光灯,下面就直接上代码啦。

MyActivity

public class MyActivity extends Activity implements View.OnClickListener {
    private Camera camera;
    private ImageView imageView;
    private boolean isLightOn = false;

    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        this.initView();

        //add shortcut
        ShortcutUtil.handleWithShortcut(this, R.drawable.icon);
    }

    /**
     * initial the view.
     */
    private void initView() {
        imageView = (ImageView) findViewById(R.id.imageview);
        imageView.setOnClickListener(this);
    }

    @Override
    public void onBackPressed() {
        if (camera != null) {
            camera.release();
        }
        super.onBackPressed();
    }

    @Override
    public void onClick(View v) {
        if (isLightOn) {
            imageView.setImageResource(R.drawable.off);
            handler.sendEmptyMessage(1);
        } else {
            imageView.setImageResource(R.drawable.on);
            handler.sendEmptyMessage(0);
        }
    }

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    camera = Camera.open();
                    Camera.Parameters params = camera.getParameters();
                    params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); // continue light,FLASH_MODE_ON
                    camera.setParameters(params);
                    camera.startPreview(); // start to  light
                    isLightOn = true;
                    break;
                case 1:
                    camera.stopPreview(); // close light
                    camera.release(); // close camera
                    isLightOn = false;
                    break;
            }

        }
    };
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:orientation="vertical"
              android:layout_width="fill_parent"
              android:layout_height="fill_parent"
        >
    <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"/>
    <ImageView
            android:id="@+id/imageview"
            android:layout_width="320px"
            android:layout_height="480px"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:src="@drawable/off"/>
</LinearLayout>

添加权限

<uses-permission android:name="android.permission.CAMERA" />

查看源码

flash_light

文章目录
  1. 1. MyActivity
  2. 2. main.xml
  3. 3. 添加权限
  4. 4. 查看源码