mirror of
https://github.com/SimpleMobileTools/Simple-Flashlight.git
synced 2025-02-16 20:00:40 +01:00
implement the toggle button
This commit is contained in:
parent
9e679fafba
commit
8daface7d4
@ -21,6 +21,7 @@ android {
|
|||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
compile fileTree(dir: 'libs', include: ['*.jar'])
|
compile fileTree(dir: 'libs', include: ['*.jar'])
|
||||||
testCompile 'junit:junit:4.12'
|
|
||||||
compile 'com.android.support:appcompat-v7:23.1.1'
|
compile 'com.android.support:appcompat-v7:23.1.1'
|
||||||
|
|
||||||
|
testCompile 'junit:junit:4.12'
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,38 @@
|
|||||||
package flashlight.simplemobiletools.com;
|
package flashlight.simplemobiletools.com;
|
||||||
|
|
||||||
|
import android.graphics.Color;
|
||||||
|
import android.graphics.PorterDuff;
|
||||||
import android.hardware.Camera;
|
import android.hardware.Camera;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.ImageView;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
private Camera camera;
|
private Camera camera;
|
||||||
private Camera.Parameters params;
|
private Camera.Parameters params;
|
||||||
|
private boolean isFlashlightOn;
|
||||||
|
private ImageView toggleBtn;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
setupCamera();
|
setupCamera();
|
||||||
|
|
||||||
|
toggleBtn = (ImageView) findViewById(R.id.toggle_btn);
|
||||||
|
toggleBtn.setOnClickListener(new View.OnClickListener() {
|
||||||
|
@Override
|
||||||
|
public void onClick(View v) {
|
||||||
|
isFlashlightOn = !isFlashlightOn;
|
||||||
|
|
||||||
|
if (isFlashlightOn) {
|
||||||
|
enableFlashlight();
|
||||||
|
} else {
|
||||||
|
disableFlashlight();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupCamera() {
|
private void setupCamera() {
|
||||||
@ -24,6 +44,18 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private void enableFlashlight() {
|
||||||
|
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
|
||||||
|
camera.setParameters(params);
|
||||||
|
toggleBtn.getDrawable().setColorFilter(Color.GREEN, PorterDuff.Mode.SRC_ATOP);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void disableFlashlight() {
|
||||||
|
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
|
||||||
|
camera.setParameters(params);
|
||||||
|
toggleBtn.getDrawable().setColorFilter(Color.RED, PorterDuff.Mode.SRC_ATOP);
|
||||||
|
}
|
||||||
|
|
||||||
private void releaseCamera() {
|
private void releaseCamera() {
|
||||||
if (camera != null) {
|
if (camera != null) {
|
||||||
camera.release();
|
camera.release();
|
||||||
|
@ -10,4 +10,9 @@
|
|||||||
android:paddingTop="@dimen/activity_vertical_margin"
|
android:paddingTop="@dimen/activity_vertical_margin"
|
||||||
tools:context="flashlight.simplemobiletools.com.MainActivity">
|
tools:context="flashlight.simplemobiletools.com.MainActivity">
|
||||||
|
|
||||||
|
<ImageView
|
||||||
|
android:id="@+id/toggle_btn"
|
||||||
|
android:layout_width="match_parent"
|
||||||
|
android:layout_height="match_parent"
|
||||||
|
android:src="@mipmap/ic_launcher"/>
|
||||||
</RelativeLayout>
|
</RelativeLayout>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user