【发布时间】:2015-07-17 19:44:50
【问题描述】:
我正在为 2.3.3 及更高版本的 android 版本开发一个应用程序,其中我提供了使用手电筒的选项。
我的代码是:
public void buttonClick(View view){
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
flashlightSwitchImg.setImageResource(R.drawable.on);//to change button image
}
Manifest.xml 权限:
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:background="@drawable/background"
tools:context=".MainActivity">
<ImageButton
android:id="@+id/bSwitchOnOff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/off"
android:padding="0dp"
android:onClick="buttonClick"
android:contentDescription="@string/onOffButton" />
</RelativeLayout>
代码正在运行但问题是手电筒响应缓慢。它在 1-2 秒延迟后开始,有时它不起作用。请帮我解决这个问题。
【问题讨论】:
标签: android performance android-camera flashlight