Android之实现手电筒实例

jopen 11年前

主要实现两个步骤:

       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)  
</div> </div>

        2、自定义闪光灯的按钮;自定义控件主要是设置设置view的大小

 
    onMeasure(int widthMeasureSpec, int heightMeasureSpec)   
这个方法:
     除非你总是需要一个100×100像素的控件,否则,你必须要重写onMeasure。                onMeasure方法在控件的父元素正要放置它的子控件时调用。它会问一个问题,“你想要用多大地方啊?”,然后传入两个参数——widthMeasureSpec和heightMeasureSpec。        它们指明控件可获得的空间以及关于这个空间描述的元数据。                 比返回一个结果要好的方法是你传递View的高度和宽度到setMeasuredDimension方法里。        接下来的代码片段给出了如何重写onMeasure。注意,调用的本地空方法是来计算高度和宽度的。它们会译解widthHeightSpec和heightMeasureSpec值,并计算出合适的高度和宽度值。                                 @Override                protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {                int measuredHeight = measureHeight(heightMeasureSpec);                int measuredWidth = measureWidth(widthMeasureSpec);                setMeasuredDimension(measuredHeight, measuredWidth);                }                                 private int measureHeight(int measureSpec) {                // Return measured widget height.                }                                 private int measureWidth(int measureSpec) {                // Return measured widget width.                }                                 边界参数——widthMeasureSpec和heightMeasureSpec ,效率的原因以整数的方式传入。在它们使用之前,首先要做的是使用MeasureSpec类的静态方法getMode和getSize来译解,如下面的片段所示:                                 int specMode = MeasureSpec.getMode(measureSpec);                int specSize = MeasureSpec.getSize(measureSpec);                                 依据specMode的值,如果是AT_MOST,specSize 代表的是最大可获得的空间;如果是EXACTLY,specSize 代表的是精确的尺寸;如果是UNSPECIFIED,对于控件尺寸来说,没有任何参考意义。                当以EXACT方式标记测量尺寸,父元素会坚持在一个指定的精确尺寸区域放置View。在父元素问子元素要多大空间时,AT_MOST指示者会说给我最大的范围。在很多情况下,你得到的值都是相同的。                在两种情况下,你必须绝对的处理这些限制。在一些情况下,它可能会返回超出这些限制的尺寸,在这种情况下,你可以让父元素选择如何对待超出的View,使用裁剪还是滚动等技术。                 接下来的框架代码给出了处理View测量的典型实现:                                 @Override                protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {                int measuredHeight = measureHeight(heightMeasureSpec);                int measuredWidth = measureWidth(widthMeasureSpec);                setMeasuredDimension(measuredHeight, measuredWidth);                }                                 private int measureHeight(int measureSpec) {                int specMode = MeasureSpec.getMode(measureSpec);                int specSize = MeasureSpec.getSize(measureSpec);                                 // Default size if no limits are specified.                int result = 500;                if (specMode == MeasureSpec.AT_MOST)                 {                // Calculate the ideal size of your                // control within this maximum size.                // If your control fills the available                // space return the outer bound.                result = specSize;                }                 else if (specMode == MeasureSpec.EXACTLY)                 {                // If your control can fit within these bounds return that value.                result = specSize;                }                return result;                }                                 private int measureWidth(int measureSpec) {                int specMode = MeasureSpec.getMode(measureSpec);                int specSize = MeasureSpec.getSize(measureSpec);                                 // Default size if no limits are specified.                int result = 500;                if (specMode == MeasureSpec.AT_MOST)                {                // Calculate the ideal size of your control                // within this maximum size.                // If your control fills the available space                // return the outer bound.                result = specSize;                }                 else if (specMode == MeasureSpec.EXACTLY)                 {                // If your control can fit within these bounds return that value.                result = specSize;                }                return result;                }          

效果如下:

20131226151756625.png

源码如下:

    <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>  
</div> </div>
 
    <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;            }                        }  
</div> </div>
    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;                }            }                }  
</div> </div> 来自:http://blog.csdn.net/x605940745