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