implement the toggle button

This commit is contained in:
tibbi 2016-01-06 15:04:16 +01:00
parent 9e679fafba
commit 8daface7d4
3 changed files with 39 additions and 1 deletions

View File

@ -21,6 +21,7 @@ android {
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:23.1.1'
testCompile 'junit:junit:4.12'
}

View File

@ -1,18 +1,38 @@
package flashlight.simplemobiletools.com;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private Camera camera;
private Camera.Parameters params;
private boolean isFlashlightOn;
private ImageView toggleBtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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() {
@ -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() {
if (camera != null) {
camera.release();

View File

@ -10,4 +10,9 @@
android:paddingTop="@dimen/activity_vertical_margin"
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>