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

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

概述主要实现两个步骤:1、实现打开和关闭闪光灯;而实现 *** 作闪光灯主要通过Camera类

主要实现两个步骤:

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)  

效果如下:

源码如下:

<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,paint1);     invalIDate();     }else{       paint.setcolor(color.WHITE);       canvas.drawCircle(x,paint);       paint1.setcolor(color.RED);       paint1.setTextSize(20);       canvas.drawText("关闭闪光灯",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中编写简单的手电筒小应用的实例教程所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1149436.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存