Implementation of flashlight for Android

Source: Internet
Author: User
Tags drawtext

There are two main steps:

1. Enable and disable the flashlight.

 

Camera camera = Camera. open (); Parameters mParameters = camera. getParameters (); mParameters. setFlashMode (Camera. parameters. FLASH_MODE_TORCH); // open Camera. parameters. FLASH_MODE_OFF indicates that camera is disabled. setParameters (mParameters)

 

2. Customize the flashlight button. The custom control mainly sets the size of the view.

 

 onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
This method introduces http://blog.csdn.net/x605940745/article/details/17583609

 

The effect is as follows:

The source code is as follows:

 

    
  
 

 

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; @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); light1 = (LightBkView) findViewById (R. id. light1); // defines the Click Event light1.setOnClickListener (light1) ;}@ Overridepublic 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 {Came Ra camera = Camera. open (); // define the painting skin Paint = new paint (); Paint paint1 = new Paint (); int x = 0; int y = 0; // enable the flashlight boolean islight; public LightBkView (Context context, AttributeSet set) {super (context, set) ;}@ Overrideprotected void onDraw (Canvas canvas) {// obtain the width and height of the control int width = this. getWidth (); int heigth = this. getHeight (); // coordinates of the dot x = width/2; y = heigth/2; // switch the background if (! Islight) {paint. setColor (Color. BLUE); canvas. drawCircle (x, y, 60, paint); paint1.setColor (Color. RED); paint1.setTextSize (20); canvas. drawText (turn on the flashlight, 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 (turn off the flashlight, x-50, y, paint1); invalidate () ;}// define the size of the View @ Overrideprotected void onMeasure (int wid ThMeasureSpec, int heightMeasureSpec) {evaluate (getWidth (widthMeasureSpec), getHeight (heightMeasureSpec);} // define the view width 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;} // Definition View 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;} // enable the flashlight switch @ Overridepublic 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 ;}}}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.