Android ေဆာ့ဖ္၀ဲတစ္ခုစေရးၾကမယ္။ Flash Light Apk(ေလ့က်င့္ခန္း-၁၃)
AIDE Apk စတင္ဖြင့္ပါ။ၿပီးရင္ res>>>>layout>>>>main.xmlကိုဖြင့္ပါ။
ေအာက္က code ကို ထည့္ပါ။
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Torch" />
src >>> MainActivity.java ကိုဖြင့္ပါ။ ေအာက္က code ကို ထည့္ပါ။
import(အပိုင္း)
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
//flag to detect flash is on or off
private boolean isLighOn = false;
private Camera camera;
private Button button;
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
Source(အပိုင္း)
button = (Button) findViewById(R.id.button);
Context context = this;
PackageManager pm = context.getPackageManager();
// if device support camera?
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
Log.e("err", "Device has no camera!");
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if (isLighOn) {
Log.i("info", "torch is turn off!");
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();
isLighOn = false;
} else {
Log.i("info", "torch is turn on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();
isLighOn = true;}}});
AndroidManifest.xml(အပိုင္း)
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
အဆင္ေျပပါေစ။
Credit >> ေဇယ်ာလင္း
အခုလိုအားေပးတ့ဲအတြက္ အထူးေက်းဇူးတင္ပါေၾကာင္း ေျပာျပရပါေစ........ IT Learner & Sharer မွနည္းပညာမ်ားအား မည္သူမဆိုလြတ္လပ္စြာကူးယူသုံးစြဲႏိုင္ပါ ေၾကာင္း.....ကြ်န္ေတာ္ ပိုင္ဆိုင္ေသာေနရာေလးသို႔ လာလည္တဲ့အတြက္ေက်းဇူးတင္ပါသည္။
No comments:
Post a Comment