手电筒

来源:互联网 发布:1password 破解 mac 编辑:程序博客网 时间:2024/04/29 05:09
Android之实现手电筒实例
2013-12-27 我来说两句 来源:剑萧舞蝶的专栏
收藏我要投稿

主要实现两个步骤:

1、实现打开和关闭闪光灯;而实现操作闪光灯主要通过Camera类

?
1
2
3
4
Camera camera = Camera.open();
Parameters mParameters = camera.getParameters();
mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);//打开Camera.Parameters.FLASH_MODE_OFF则为关闭
camera.setParameters(mParameters)

2、自定义闪光灯的按钮;自定义控件主要是设置设置view的大小

?
1
onMeasure(intwidthMeasureSpec, intheightMeasureSpec)
这个方法介绍http://blog.csdn.net/x605940745/article/details/17583609

效果如下:

\\

源码如下:

?
1
2
3
4
5
6
<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>

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
packagecom.android.xiong.xionglight;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.KeyEvent;
importandroid.view.Menu;
publicclassMainActivity extendsActivity {
privateLightBkView light1;
@Override
protectedvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
light1 = (LightBkView) findViewById(R.id.light1);
//定义单击事件
light1.setOnClickListener(light1);
}
@Override
publicbooleanonCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
returntrue;
}
}

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
packagecom.android.xiong.xionglight;
importandroid.content.Context;
importandroid.graphics.Canvas;
importandroid.graphics.Color;
importandroid.graphics.Paint;
importandroid.hardware.Camera;
importandroid.hardware.Camera.Parameters;
importandroid.util.AttributeSet;
importandroid.view.View;
importandroid.view.View.OnClickListener;
publicclassLightBkView extendsView implementsOnClickListener {
Camera camera = Camera.open();
// 定义画皮
Paint paint =newPaint();
Paint paint1 =newPaint();
intx = 0;
inty = 0;
// 打开闪光灯
booleanislight;
publicLightBkView(Context context, AttributeSet set) {
super(context, set);
}
@Override
protectedvoidonDraw(Canvas canvas) {
// 获取控件的宽度和高度
intwidth = this.getWidth();
intheigth = 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
protectedvoidonMeasure(intwidthMeasureSpec, intheightMeasureSpec) {
setMeasuredDimension(getWidth(widthMeasureSpec),
getHeight(heightMeasureSpec));
}
//定义view的宽度
publicintgetWidth(intwidthMeasureSpec) {
intreslut = 0;
intwidthMode = MeasureSpec.getMode(widthMeasureSpec);
if(widthMode == MeasureSpec.AT_MOST) {
reslut =120;
}
if(widthMode == MeasureSpec.EXACTLY) {
reslut = MeasureSpec.getSize(widthMeasureSpec);
}
returnreslut;
}
//定义view的高度
publicintgetHeight(intheightMeasureSpec) {
intreslut = 0;
intheightMode = MeasureSpec.getMode(heightMeasureSpec);
if(heightMode == MeasureSpec.AT_MOST) {
reslut =120;
}
if(heightMode == MeasureSpec.EXACTLY) {
reslut = MeasureSpec.getSize(heightMeasureSpec);
}
returnreslut;
}
// 实现闪光灯的的开关
@Override
publicvoidonClick(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
原创粉丝点击