Android中编写简单的手电筒小应用的实例教程
程序员文章站
2024-02-28 08:59:10
主要实现两个步骤:
1、实现打开和关闭闪光灯;而实现操作闪光灯主要通过camera类
camera camera = camera.open();
pa...
主要实现两个步骤:
1、实现打开和关闭闪光灯;而实现操作闪光灯主要通过camera类
camera camera = camera.open(); parameters mparameters = camera.getparameters(); mparameters.setflashmode(camera.parameters.flash_mode_torch);//打开camera.parameters.flash_mode_off
则为关闭
amera.setparameters(mparameters)
2、自定义闪光灯的按钮;自定义控件主要是设置设置view的大小
onmeasure(int widthmeasurespec, int heightmeasurespec)
效果如下:
源码如下:
<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:gravity="center" android:background="@drawable/light" tools:context=".mainactivity" > <com.android.xiong.xionglight.lightbkview android:id="@+id/light1" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </relativelayout> <uses-permission android:name="android.permission.camera" />
package com.android.xiong.xionglight; import android.app.activity; import android.os.bundle; import android.view.keyevent; import android.view.menu; public class mainactivity extends activity { private lightbkview light1; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); light1 = (lightbkview) findviewbyid(r.id.light1); //定义单击事件 light1.setonclicklistener(light1); } @override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.main, menu); return true; } }
package com.android.xiong.xionglight; import android.content.context; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.hardware.camera; import android.hardware.camera.parameters; import android.util.attributeset; import android.view.view; import android.view.view.onclicklistener; public class lightbkview extends view implements onclicklistener { camera camera = camera.open(); // 定义画皮 paint paint = new paint(); paint paint1 = new paint(); int x = 0; int y = 0; // 打开闪光灯 boolean islight; public lightbkview(context context, attributeset set) { super(context, set); } @override protected void ondraw(canvas canvas) { // 获取控件的宽度和高度 int width = this.getwidth(); int heigth = this.getheight(); // 圆点的坐标 x = width / 2; y = heigth / 2; //更换开关背景 if(!islight){ paint.setcolor(color.blue); canvas.drawcircle(x, y, 60, paint); paint1.setcolor(color.red); paint1.settextsize(20); canvas.drawtext("打开闪光灯", x-50, y, paint1); invalidate(); }else{ paint.setcolor(color.white); canvas.drawcircle(x, y, 60, paint); paint1.setcolor(color.red); paint1.settextsize(20); canvas.drawtext("关闭闪光灯", x-50, y, paint1); invalidate(); } } // 定义view的大小 @override protected void onmeasure(int widthmeasurespec, int heightmeasurespec) { setmeasureddimension(getwidth(widthmeasurespec), getheight(heightmeasurespec)); } //定义view的宽度 public int getwidth(int widthmeasurespec) { int reslut = 0; int widthmode = measurespec.getmode(widthmeasurespec); if (widthmode == measurespec.at_most) { reslut = 120; } if (widthmode == measurespec.exactly) { reslut = measurespec.getsize(widthmeasurespec); } return reslut; } //定义view的高度 public int getheight(int heightmeasurespec) { int reslut = 0; int heightmode = measurespec.getmode(heightmeasurespec); if (heightmode == measurespec.at_most) { reslut = 120; } if (heightmode == measurespec.exactly) { reslut = measurespec.getsize(heightmeasurespec); } return reslut; } // 实现闪光灯的的开关 @override public void onclick(view v) { if (!islight) { parameters mparameters = camera.getparameters(); mparameters.setflashmode(camera.parameters.flash_mode_torch); camera.setparameters(mparameters); islight = true; } else { parameters mparameters = camera.getparameters(); mparameters.setflashmode(camera.parameters.flash_mode_off); camera.setparameters(mparameters); islight = false; } } }
推荐阅读
-
Android中编写简单的手电筒小应用的实例教程
-
详解Android应用中preference首选项的编写方法
-
详解Android应用中ListView列表选项栏的编写方法
-
Android应用开发中Action bar编写的入门教程
-
使用python编写批量卸载手机中安装的android应用脚本
-
Android应用开发中Action bar编写的入门教程
-
Android应用开发中Action bar编写的入门教程
-
使用python编写批量卸载手机中安装的android应用脚本
-
使用python编写批量卸载手机中安装的android应用脚本
-
使用python编写批量卸载手机中安装的android应用脚本