implement the toggle button
This commit is contained in:
parent
9e679fafba
commit
8daface7d4
|
@ -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'
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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>
|
||||
|
|
Loading…
Reference in New Issue