編輯:關於 Android 程式設計
一個簡單又實用的小項目——手電筒,主要實現兩個步驟:控制閃光燈的開關,以及自定義閃光燈按鈕。
操作閃光燈主要通過 Camera 類,設置閃光燈模式為 FLASH_MODE_TORCH 即可打開:
Camera camera = Camera.open(); Parameters mParameters = camera.getParameters(); mParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);// 打開
設為 Camera.Parameters.FLASH_MODE_OFF 則為關閉,最後調用 camera.setParameters(mParameters) 生效。
自定義控件主要是通過 onMeasure(int widthMeasureSpec, int heightMeasureSpec) 來設置 View 的大小。
效果如下:


布局文件:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://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" />
MainActivity:
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; } }
LightBkView:
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)); } 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; } 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; } } }
以上就是手電筒小應用的完整實現,希望對大家有所幫助。
自訂 AlertDialog 對話框:做一個有 username / password 欄位的登入視窗
Android 開發中經常需要在畫面上彈一個對話框,例如詢問使用者、讓使用者做選擇,這類元件統稱為 Dialog,而 AlertDialog 採用的是建造者模式(Buil
Android 學習之路:數據的使用(二)SQLite 篇
下午學習了 SQLite,並用它做了一個非常簡單的信息管理系統。系統完整做出來之後,我感覺單機版的各類管理系統應該都能拿下來了!下面就開始講講 SQLite 的用法(增刪
ListView 搭配 CheckBox:全選、新增與刪除實作(含範例)
ListView 說複雜也確實複雜,但它在專案裡實在太常用了。於是寫了個小 Demo 把「列表 + 多選 + 全選 + 批次刪除」這條常見需求走一遍,其中自定義 Adap
Android 事件分發機制(十三):dispatchTouchEvent 源碼逐段解析
事件分發的本質是一種委託:上層委託下層、父容器委託子元件去處理。要理解它,只需盯住一個類——MotionEvent,以及三個方法。 三個核心方法 dispatchTo