手电筒
来源:互联网 发布:1password 破解 mac 编辑:程序博客网 时间:2024/04/29 05:09
- Android之实现手电筒实例
- 2013-12-27 我来说两句 来源:剑萧舞蝶的专栏
- 收藏我要投稿
主要实现两个步骤:
1、实现打开和关闭闪光灯;而实现操作闪光灯主要通过Camera类
1234Camera camera = Camera.open();
Parameters mParameters = camera.getParameters();
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
//打开Camera.Parameters.FLASH_MODE_OFF则为关闭
camera.setParameters(mParameters)
2、自定义闪光灯的按钮;自定义控件主要是设置设置view的大小
这个方法介绍http://blog.csdn.net/x605940745/article/details/175836091onMeasure(
int
widthMeasureSpec,
int
heightMeasureSpec)
效果如下:
源码如下:
123456<RELATIVELAYOUT xmlns:tools=
"http://schemas.android.com/tools"
xmlns:android=
"http://schemas.android.com/apk/res/android"
tools:context=
".MainActivity"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:gravity=
"center"
android:background=
"@drawable/light"
>
<COM.ANDROID.XIONG.XIONGLIGHT.LIGHTBKVIEW android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content/"
android:id=
"@+id/light1"
>
</COM.ANDROID.XIONG.XIONGLIGHT.LIGHTBKVIEW></RELATIVELAYOUT>
1<USES-PERMISSION android:name=
"android.permission.CAMERA"
></USES-PERMISSION>
1234567891011121314151617181920212223242526272829package
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
;
}
}
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102package
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
;
}
}
}
0 0
- 手电筒
- Forms 手电筒
- LED手电筒
- android手电筒
- 手电筒源码
- Android手电筒
- Android手电筒
- 手电筒功能
- iphone手电筒
- 手电筒开发
- android 手电筒
- android手电筒
- 制作手电筒
- android手电筒
- iOS 手电筒
- android 手电筒
- 手机手电筒
- Android手电筒
- 第四个驱动程序-tq2440
- iOS之UIButton
- 学习网站开发建设方案能获得很多非常突出的推广效果!
- HTTP 文件上传的基本原理
- Oracle PL/SQL Articles
- 手电筒
- git学习笔记--远程仓库
- js获取项目根目录
- UVA 10869 - Brownie Points II(树状数组)
- Miscellaneous Articles
- 关于Debian中SSH中文的设定
- LeetCode Convert Sorted Array to Binary Search Tree
- MySQL存储过程
- myeclipse 项目易名404