欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

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

程序员文章站 2024-03-01 08:09:52
主要实现两个步骤: 1、实现打开和关闭闪光灯;而实现操作闪光灯主要通过camera类 camera camera = camera.open(); pa...

主要实现两个步骤:

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)  

效果如下:

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

源码如下:

<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; 
    } 
  } 
 
}