Android中編寫簡(jiǎn)單的手電筒小應(yīng)用的實(shí)例教程
主要實(shí)現(xiàn)兩個(gè)步驟:
1、實(shí)現(xiàn)打開和關(guān)閉閃光燈;而實(shí)現(xiàn)操作閃光燈主要通過Camera類
Camera camera = Camera.open(); Parameters mParameters = camera.getParameters(); mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);//打開Camera.Parameters.FLASH_MODE_OFF
則為關(guān)閉
amera.setParameters(mParameters)
2、自定義閃光燈的按鈕;自定義控件主要是設(shè)置設(shè)置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();
// 圓點(diǎn)的坐標(biāo)
x = width / 2;
y = heigth / 2;
//更換開關(guān)背景
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("關(guān)閉閃光燈", 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;
}
// 實(shí)現(xiàn)閃光燈的的開關(guān)
@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;
}
}
}
相關(guān)文章
Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤旋轉(zhuǎn)菜單效果的方法詳解【附demo源碼下載】
這篇文章主要介紹了Android編程實(shí)現(xiàn)仿優(yōu)酷圓盤旋轉(zhuǎn)菜單效果的方法,涉及Android界面布局及事件響應(yīng)相關(guān)操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-08-08
Android使用AndroidUtilCode實(shí)現(xiàn)多語(yǔ)言
這篇文章主要為大家介紹了Android使用AndroidUtilCode實(shí)現(xiàn)多語(yǔ)言示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
詳解Android?Flutter如何自定義動(dòng)畫路由
flutter中有默認(rèn)的Route組件,叫做MaterialPageRoute,但是MaterialPageRoute太普通了,如果我們想要做點(diǎn)不同的跳轉(zhuǎn)特效應(yīng)該如何處理呢?一起來(lái)看看吧2023-04-04
Android動(dòng)態(tài)更換應(yīng)用圖標(biāo)詳情
這篇文章主要介紹了Android動(dòng)態(tài)更換應(yīng)用圖標(biāo)詳情,文章圍繞主題展開詳細(xì)的介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-07-07
詳解Flutter自定義應(yīng)用程序內(nèi)鍵盤的實(shí)現(xiàn)方法
本文將展示如何利用Flutter創(chuàng)建自定義鍵盤小部件,用于在自己的應(yīng)用程序中的Flutter TextField中輸入文本,感興趣的小伙伴可以了解一下2022-06-06
Android開發(fā)中TextView各種常見使用方法小結(jié)
這篇文章主要介紹了Android開發(fā)中TextView各種常見使用方法,結(jié)合實(shí)例形式總結(jié)分析了Android開發(fā)中TextView各種常見布局與功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-04-04

