【问题标题】:Faster and best way to ON flashlight in android在android中打开手电筒的更快和最好的方法
【发布时间】: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


    【解决方案1】:

    您的代码似乎完全没问题(假设当您按下闪光灯按钮打开它时,除此之外没有进行任何其他处理)。 但是正如您所说的手电筒响应有延迟,这似乎是您的 android 设备问题。尝试更改一些设置,例如打开/关闭省电模式等。 如果您遇到同样的问题,请尝试在其他设备上部署应用程序。

    【讨论】:

    • 这个应用手电筒在我的设备上工作得非常快link 但我的应用手电筒延迟了 1-2 秒。我目前只使用此代码和按钮图像更改开/关图像。
    • 您的其他代码导致延迟。尝试膨胀资源。
    • 我写下了按钮点击期间发生的所有代码,请查看并告诉我解决方案我应该怎么做
    • 可以给我看整个按钮的onClick方法和你的布局xml文件吗?
    • 我添加了整个按钮点击方法和布局xml文件请检查并告诉我@gRaWEty
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    相关资源
    最近更新 更多