The main implementation of two steps:
1, the realization turns on and closes the flash lamp, but realizes the operation Flash mainly through camera class
Camera Camera = Camera.open ();
Parameters mparameters = Camera.getparameters ();
Mparameters.setflashmode (Camera.Parameters.FLASH_MODE_TORCH);/Open Camera.Parameters.FLASH_MODE_OFF
is closed
Amera.setparameters (Mparameters)
2, custom flash button; A custom control is primarily set to the size of the set view
onmeasure (int widthmeasurespec, int heightmeasurespec)
The effect is as follows:
The source code is as follows:
<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 Andro Id: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);
Define Click event
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 ();
Define the Painted mask Paint Paint = new Paint ();
Paint paint1 = new Paint ();
int x = 0;
int y = 0;
Open Flash Boolean islight;
Public Lightbkview (context, AttributeSet set) {Super (context, set);
@Override protected void OnDraw (Canvas Canvas) {//Get the width and height of the control int width = This.getwidth ();
int heigth = This.getheight ();
The coordinates of the dot x = WIDTH/2;
y = HEIGTH/2;
Replace the switch background if (!islight) {paint.setcolor (Color.Blue);
Canvas.drawcircle (x, y, n, paint); PainT1.setcolor (color.red);
Paint1.settextsize (20);
Canvas.drawtext ("Open flash", x-50, Y, paint1);
Invalidate ();
}else{Paint.setcolor (Color.White);
Canvas.drawcircle (x, y, n, paint);
Paint1.setcolor (color.red);
Paint1.settextsize (20);
Canvas.drawtext ("Turn off Flash", x-50, Y, paint1);
Invalidate (); }//define view size @Override protected void onmeasure (int widthmeasurespec, int heightmeasurespec) {Setm
Easureddimension (GetWidth (Widthmeasurespec), GetHeight (Heightmeasurespec));
}//Defines the width of 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; }//Define view's height 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; //implementation of the Flash switch @Override public void OnClick (View v) {if (!islight) {Parameters mparameters = Cam
Era.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;
}
}
}