【Android】MyTool 工具界面:手电筒



MyTool 工具界面:

♦ 回顾

通过上一节的学习,我们实现了工具界面百度、拨号功能的实现,如下图所示。今天,要在这个界面中实现一个新的功能手电筒。

在这里插入图片描述
返回顶部


♦ 设置 activity_my_tool.xml 界面

  • 添加一个新的按钮组件:手电筒
 <Button
     android:id="@+id/flashlight"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_alignParentStart="true"
     android:layout_alignParentLeft="true"
     android:layout_alignParentTop="true"
     android:layout_marginStart="39dp"
     android:layout_marginLeft="39dp"
     android:layout_marginTop="250dp"
     android:text="手电筒"
     android:textSize="16dp"/>

返回顶部


♦ 编写 MyToolActivity 实现功能界面跳转

public class MyToolActivity extends AppCompatActivity {
    
    

    TextView welcome;
    Button net, call, quick_call,flashlight;

    //权限请求:载入界面、特定情况触发
    //定义权限 --- 动态权限,申请哪一个就启动哪一个,通常为常量
    private static String[] PERMISSIONS_STORAGE = {
    
    
            Manifest.permission.CALL_PHONE
    };
    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_tool);

        .........................
        
        // 获取工具组件
        net = findViewById(R.id.net);
        call = findViewById(R.id.call);
        quick_call = findViewById(R.id.quick_call);
        flashlight = findViewById(R.id.flashlight);
        
        .........................
        
        // 手电筒界面跳转
        flashlight.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                // 创建页面跳转
                Intent intent = new Intent();
                intent.setClass(MyToolActivity.this,FlashLightActivity.class);
                startActivity(intent);
            }
        });
    }
}

返回顶部


♦ 创建 activity_flash_light.xml 手电筒界面

  • 基本布局、背景
    在这里插入图片描述

  • 添加 ImageButton 组件
    在这里插入图片描述
    在这里插入图片描述
    在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.myapplication.FlashLightActivity"
    android:background="@drawable/main_page">

    <TextView
        android:id="@+id/textView9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="76dp"
        android:text="欢迎使用手电筒!"
        android:textStyle="bold"
        android:textColor="@android:color/white"
        android:textSize="20dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.511"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="255dp"
        android:layout_height="247dp"
        android:layout_alignParentTop="true"
        android:layout_centerInParent="true"
        android:layout_marginTop="218dp"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.644"
        app:srcCompat="@drawable/off" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="195dp"
        android:text="已关闭!"
        android:textSize="20dp"
        android:textStyle="bold"
        android:textColor="@android:color/white"
        />
        <SurfaceView
        android:id="@+id/surfaceView"
        android:layout_width="149dp"
        android:layout_height="109dp"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="124dp"
        android:layout_marginBottom="7dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
  • 由于手电筒(闪光灯)常与照相机放在一起使用,不妨这里我们添加一个SurfaceView用于模拟照相功能

在这里插入图片描述

返回顶部


♦ 编写 FlashLightActivity 实现功能

package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import android.Manifest;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.os.Build;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import java.io.IOException;

public class FlashLightActivity extends AppCompatActivity {
    
    

    // 定义对象
    ImageButton flashlight;
    TextView showNow;
    boolean isFlag = false;
    
    // 创建相机对象
    Camera cc;
    Camera.Parameters pp;
    SurfaceView sv;
    SurfaceHolder sh;

    //权限请求:载入界面、特定情况触发
    //定义权限 --- 动态权限,申请哪一个就启动哪一个,通常为常量
    private static String[] PERMISSIONS_STORAGE = {
    
    
            Manifest.permission.CAMERA
    };
    //请求状态码
    private static int REQUEST_PERMISSION_CODE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
    
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flash_light);

        if(Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP){
    
    
            // 检查当前的权限
            if (ActivityCompat.checkSelfPermission(FlashLightActivity.this, Manifest.permission.CAMERA) !=
                    PackageManager.PERMISSION_GRANTED){
    
    
                ActivityCompat.requestPermissions(FlashLightActivity.this,PERMISSIONS_STORAGE,REQUEST_PERMISSION_CODE);
            }
        }

        flashlight = findViewById(R.id.imageButton);
        showNow = findViewById(R.id.textView8);
        sv = findViewById(R.id.surfaceView);

        cc = Camera.open();
        pp = cc.getParameters();
        sh = sv.getHolder();

        sh.addCallback(new SurfaceHolder.Callback() {
    
    
            @Override
            public void surfaceCreated(@NonNull SurfaceHolder holder) {
    
    
                try {
    
    
                    cc.setPreviewDisplay(sh);
                    cc.startPreview();
                } catch (IOException e) {
    
    
                    e.printStackTrace();
                    cc.release();
                }
            }
            @Override
            public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int width, int height) {
    
    

            }
            @Override
            public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
    
    

            }
        });

        flashlight.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    

                if(isFlag==false){
    
    
                    flashlight.setImageDrawable(getResources().getDrawable(R.drawable.on));
                    showNow.setText("已开启!");
                    pp.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                    cc.setParameters(pp);
                    isFlag = true;
                } else {
    
    
                    flashlight.setImageDrawable(getResources().getDrawable(R.drawable.off));
                    showNow.setText("已关闭!");
                    pp.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                    cc.setParameters(pp);
                    isFlag = false;
                }
            }
        });
    }
}

返回顶部


♦ 修改AndroidManifest.xml文件授权

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myapplication_test">
    <uses-permission android:name="android.permission.CALL_PHONE" />
    <uses-permission android:name="android.permission.CAMERA" /> 添加摄像权限
    <uses-permission android:name="android.permission.FLASHLIGHT" /> 添加电筒权限
    <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="com.example.myapplication.MyToolActivity"></activity>
        <activity android:name="com.example.myapplication.MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

返回顶部


♦ 测试

在这里插入图片描述

返回顶部


上一篇:MyTool工具界面百度、拨号、一键拨号                                  下一篇:MyTool 工具界面:秒表计时

猜你喜欢

转载自blog.csdn.net/qq_45797116/article/details/113359580