Android之實現手電筒實例

jopen 10年前發布 | 33K 次閱讀 Android Android開發 移動開發

主要實現兩個步驟:

       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;  

}  
  </pre><a href="/misc/goto?guid=4959550756523499091" target="_blank"></a> <p>效果如下:</p>

20131226151756625.png

源碼如下:

    <RelativeLayout xmlns:android="
        xmlns: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>  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959550756616141749" target="_blank"></a></div>

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


}  </pre><a style="text-indent:0px;" title="派生到我的代碼片" href="/misc/goto?guid=4959550756616141749" target="_blank"></a></div>

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

}  </pre> <div class="dp-highlighter bg_java">

</div> </div> 來自:http://blog.csdn.net/x605940745

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!