Androidでプログラムでフラッシュライトをオンにする方法は?
この例は、Androidでプログラムでフラッシュライトをオンにする方法を示しています。
ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。
ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <ToggleButton android:id="@+id/onOffFlashlight" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textOn="Turn Off" android:textOff="Turn On" android:checked="false" android:text="Turn On/Off Camera LED/ Flashlight Android" /> </LinearLayout>
ステップ3 −次のコードをsrc / MainActivity.java
に追加しますimport android.content.Context;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
public class MainActivity extends AppCompatActivity {
private CameraManager mCameraManager;
private String mCameraId;
private ToggleButton toggleButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boolean isFlashAvailable = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT);
if (!isFlashAvailable) {
showNoFlashError();
}
mCameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
try {
mCameraId = mCameraManager.getCameraIdList()[0];
} catch (CameraAccessException e) {
e.printStackTrace();
}
toggleButton = findViewById(R.id.onOffFlashlight);
toggleButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switchFlashLight(isChecked);
}
});
}
public void showNoFlashError() {
AlertDialog alert = new AlertDialog.Builder(this)
.create();
alert.setTitle("Oops!");
alert.setMessage("Flash not available in this device...");
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
alert.show();
}
public void switchFlashLight(boolean status) {
try {
mCameraManager.setTorchMode(mCameraId, status);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
} ステップ4 -次のコードをandroidManifest.xmlに追加します
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample"> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera.flash" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
アプリケーションを実行してみましょう。実際のAndroidモバイルデバイスをコンピューターに接続していると思います。 android studioからアプリを実行するには、プロジェクトのアクティビティファイルの1つを開き、ツールバーの[アイコンの実行]をクリックします。オプションとしてモバイルデバイスを選択し、デフォルトの画面を表示するモバイルデバイスを確認します–
-
プログラムでアプリケーションを終了する方法は?
この例は、Androidでプログラムによってアプリケーションを終了する方法を示しています。 ステップ1 − Android Studioで新しいプロジェクトを作成し、[ファイル]⇒[新しいプロジェクト]に移動して、新しいプロジェクトを作成するために必要なすべての詳細を入力します。 ステップ2 −次のコードをres / layout/activity_main.xmlに追加します。 <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="ht
-
Android でカメラのフラッシュをオンまたはオフにする方法
ほとんどすべての Android スマートフォンには、カメラがより良い写真を撮るのに役立つフラッシュが付属しています。フラッシュの目的は、追加の光を提供して、画像が明るく見えるようにすることです。自然光が十分でない場合や、夜間に屋外で写真を撮る場合に非常に便利です。 フラッシュは写真の重要な要素です。これは、写真撮影において照明が重要な役割を果たしているためです。実際、それが良い写真と悪い写真を区別するものです。ただし、フラッシュを常に使用またはオンにしておく必要があるわけではありません。場合によっては、前景に光を追加しすぎて、写真の美学が損なわれることがあります。被写体の特徴を洗い流すか、