Android 自定义加载条和Zxing扫描二维码

本文介绍了如何在Android应用中实现自定义加载条并集成Zxing库进行二维码扫描。首先,需要在AndroidManifest.xml文件中声明相机、存储和网络权限。接着,在build.gradle文件中添加必要的ZXing依赖库。最后,配置主页面布局以展示加载条和集成扫描功能。
摘要由CSDN通过智能技术生成

在注册表里面注册权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<uses-permission android:name="android.permission.VIBRATE" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

<uses-permission android:name="android.permission.INTERNET" />

在build.gradle导入依赖
compile 'com.journeyapps:zxing-android-embedded:2.3.0@aar'
compile 'com.journeyapps:zxing-android-legacy:2.3.0@aar'
compile 'com.journeyapps:zxing-android-integration:2.3.0@aar'
compile 'com.google.zxing:core:3.2.0'

主页面布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.zhoukao1_1104.MainActivity"
    >
     <com.title.bar
         android:layout_width="match_parent"
         android:layout_height="80dp"
         android:id="@+id/title_bar"
         ></com.title.bar>
     <com.title.My_Progress
         android:id="@+id/my_pro"
         android:layout_width="150dp"
         android:layout_height="150dp"
         android:layout_below="@id/title_bar"
         app:textIsDisplayable="true"
         app:roundWidth="10dip"
         android:layout_marginTop="250dp"
         android:layout_centerHorizontal="true"
         >
     </com.title.My_Progress>
     <Button
         android:layout_width="250dp"
         android:layout_height="50dp"
         android:id="@+id/my_btn"
         android:text="扫描二维码"
         android:textColor="#f00"
         android:textSize="25dp"
         android:layout_below="@id/my_pro"
         android:background="#619F86"
         android:layout_marginTop="80dp"
         android:layout_centerHorizontal="true"
          />
</RelativeLayout>
在主页面内进行逻辑处理,点击Button进行加载进度条,跳转到扫描二维码页面,点击右上角的用户图标,将跳转到第二页面。
package com.example.zhoukao1_1104;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;
import com.title.My_Progress;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
    private static final int FLAG = 0x11;
    private TextView left_btn;
    private TextView right_btn;
    private Button progress_btn;
    private Handler ler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
              if(msg.what == FLAG) {
                  int progress = pv.getProgress();
                  progress += 10;
                  if(progress >pv.getMax()) {
                      IntentIntegrator integrator=new IntentIntegrator(MainActivity.this);
                      integrator.initiateScan();
//                      IntentIntegrator intentIntegrator = new IntentIntegrator(MainActivity.this);
//                      intentIntegrator
//                              .setDesiredBarcodeFormats(IntentIntegrator.ALL_CODE_TYPES)
//                              .setPrompt("将二维码/条码放入框内,即可自动扫描")//写那句提示的话
//                              .setOrientationLocked(false)//扫描方向固定
                              .setCaptureActivity(CustomScanAct.class) // 设置自定义的activity是CustomActivity
//                              .initiateScan(); // 初始化扫描
                  }
                  pv.setProgress(progress);
                  ler.sendEmptyMessageDelayed(FLAG, 1000);
              }
        }
    };
    private My_Progress pv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActionBar bar = getSupportActionBa
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值