Android Zxing二维码自定义界面(继承Capture类)

先来看效果图

首先我们引入Zxing开源框架,来搭建二维码扫描环境:

dependencies {
    implementation 'cn.yipianfengye.android:zxing-library:2.2'
}

跳转到二维码扫描界面:

//跳转到扫描二维码
public void forwardZXing(){
    Intent intent = new Intent(this, CaptureActivity.class);
    startActivityForResult(intent, Constants.ZXING_CODE);    //ZING_CODE为自定义的一个整数
}

接下来我们来分析一下如何对这个框架做些手脚,从而能让我们自定义一些东西。

如果我们想要改变布局,应该想到的一个办法是重写,那重写哪儿呢?自然是CaptureActivity中的onCreate方法。因为onCreate方法中,包含了它对布局文件的设置。

所有,我们只需要做的是:自定义一个Activity来继承CaptureActivity,并重写它的onCreate方法来改变布局。

但是我们发现CaptureActivity类中的布局文件我们是无法直接打开的,例如下面这样:

所以无法直接得知它内部布局是什么样的,经过我几番的查询资料,终于摸索到了自定义的方法。下面我就分享给大家:

1、首先编写我们的自定义布局文件(activtiy_myzxing):

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <FrameLayout
        android:id="@+id/fl_zxing_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/relative">
        <SurfaceView
            android:id="@+id/preview_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <com.uuzuche.lib_zxing.view.ViewfinderView
            android:id="@+id/viewfinder_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

    <RelativeLayout
        android:id="@+id/relative"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:paddingBottom="5dp"
        android:paddingLeft="5dp"
        android:paddingRight="5dp">
        <EditText
            android:id="@+id/et_search"
            android:layout_toLeftOf="@id/btn_search"
            android:layout_width="match_parent"
            android:layout_height="45dp"
            android:hint="输入查询内容"
            android:textSize="14sp"
            android:textColor="@color/black"
            android:layout_centerVertical="true"
            android:gravity="center"
            android:singleLine="true"/>
        <Button
            android:id="@+id/btn_search"
            android:layout_width="90dp"
            android:layout_height="35dp"
            android:text="查询"
            android:textSize="14sp"
            android:textColor="@color/white"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:background="@drawable/corner_green_service_progress"/>
    </RelativeLayout>
</RelativeLayout>
  • 这里要注意的是,在下面这一块代码中,控件的id是固定的,不要去改它!(这一块代码一直绑在一块就OK)
    <FrameLayout
        android:id="@+id/fl_zxing_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/relative">
        <SurfaceView
            android:id="@+id/preview_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>

        <com.uuzuche.lib_zxing.view.ViewfinderView
            android:id="@+id/viewfinder_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"/>
    </FrameLayout>

2、我们新建一个MyZxingActivity类来继承CaptureActivity,并重写onCreate():

public class MyZxingActivity extends CaptureActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activtiy_myzxing);
    }
}
  • 注意这里要把setContentView写在super后面,不然会被super中的setContentView覆盖(这个类也要在Manifest中注册)

3、最后,我们跳转的时候,把CaptureActivity.class改成MyZxingActivity.class

//跳转到扫描二维码
public void forwardZXing(){
    Intent intent = new Intent(this, MyZxingActivity.class);
    startActivityForResult(intent, Constants.ZXING_CODE);    //ZING_CODE为自定义的一个整数
}

之后的一系列控件绑定,监听,跳转等操作,只需要在MyZxingActivity中完成就可以了。

猜你喜欢

转载自blog.csdn.net/zz51233273/article/details/107316984
今日推荐