自己写的一个android小应用 手电筒

时间:2022-10-23 23:08:56

简洁 小巧 免费 无广告

代码奉上

active类

     private boolean isopent = false;

     private Camera camera;

     @Override
     protected void onCreate(Bundle savedInstanceState) {

         // TODO Auto-generated method stub

         super.onCreate(savedInstanceState);

         View view = View.inflate(this, R.layout.activity_main, null);

         setContentView(view);

         final RelativeLayout layout_main = (RelativeLayout) findViewById(R.id.mainactive);
         final ImageView img_but = (ImageView) findViewById(R.id.main_img);

         img_but.setOnClickListener(new View.OnClickListener() {

             @Override
             public void onClick(View v) {

                 if (!isopent) {
                     layout_main.setBackgroundColor(Color.parseColor("#FFFFFF"));
                     img_but.setBackgroundColor(Color.parseColor("#FFFFFF"));

                     camera = Camera.open();

                     Parameters params = camera.getParameters();

                     params.setFlashMode(Parameters.FLASH_MODE_TORCH);

                     camera.setParameters(params);

                     camera.startPreview(); // 开始亮灯

                     isopent = true;

                 } else {

                     layout_main.setBackgroundColor(Color.parseColor("#000000"));
                     img_but.setBackgroundColor(Color.parseColor("#000000"));

                     camera.stopPreview(); // 关掉亮灯

                     camera.release(); // 关掉照相机

                     isopent = false;
                 }
             }
         });

     }

     private long exitTime = 0;

     @Override
     public boolean onKeyDown(int keyCode, KeyEvent event) {

layout

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     xmlns:tools="http://schemas.android.com/tools"
     android:id="@+id/mainactive"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:background="#000000"
     tools:context=".MainActivity" >

     <ImageView
         android:id="@+id/main_img"
         android:layout_width="128dip"
         android:layout_height="128dip"
         android:src="@drawable/light"
         android:layout_centerInParent="true"
         android:background="#000000" />

 </RelativeLayout>

下载地址:http://url.cn/OpGm12

BTS工作室