1. 云栖社区>
  2. >
  3. 正文

Android中编写简单的手电筒小应用的实例教程_Android

作者:用户 来源:互联网 时间:2016-04-15 17:17:24

android安卓闪光灯手电筒

Android中编写简单的手电筒小应用的实例教程_Android - 摘要: 本文讲的是Android中编写简单的手电筒小应用的实例教程_Android, 主要实现两个步骤: 1、实现打开和关闭闪光灯;而实现操作闪光灯主要通过Camera类 Camera camera = Camera.open(); Parameters mParameters = camera.getParamet

主要实现两个步骤:

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)  

效果如下:

2016415154352940.jpg2016415154422076.jpg

源码如下:

<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简单实例、android简单项目实例、android 简单app实例、android开发简单实例、android最简单实例,以便于您获取更多的相关知识。