在注册表里面注册权限
在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'
主页面布局:
在主页面内进行逻辑处理,点击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 = getSupportActionBar();
if(bar != null) {
bar.hide();
}
initView();
setOnClick();
}
private void setOnClick() {
left_btn.setOnClickListener(this);
right_btn.setOnClickListener(this);
progress_btn.setOnClickListener(this);
}
private void initView() {
left_btn = (TextView) findViewById(R.id.left_btn);
right_btn = (TextView) findViewById(R.id.right_btn);
progress_btn = (Button) findViewById(R.id.my_btn);
pv = (My_Progress) findViewById(R.id.my_pro);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.left_btn:
Toast.makeText(this,"已经是首页了",Toast.LENGTH_SHORT).show();
break;
case R.id.right_btn:
Intent intent = new Intent(MainActivity.this,Main2.class);
startActivity(intent);
break;
case R.id.my_btn:
ler.sendEmptyMessageDelayed(FLAG,1000);
}
}
@Override
protected void onStop() {
super.onStop();
ler.removeMessages(FLAG);
}
//获取扫描的结果
// @Override
// public void onActivityResult(int requestCode, int resultCode, Intent data) {
// IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
// if (intentResult != null) {
// if (intentResult.getContents() == null) {
//
//
// } else {
// // ScanResult 为获取到的字符串
// String ScanResult = intentResult.getContents();
// Toast.makeText(this,ScanResult.toString(),Toast.LENGTH_SHORT).show();
//// tvResult.setText(ScanResult);
// }
// } else {
// super.onActivityResult(requestCode, resultCode, data);
// }
// }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
IntentResult scanResult =IntentIntegrator.parseActivityResult(requestCode,resultCode,data);
if (scanResult !=null){
String result=scanResult.getContents();
Toast.makeText(this,result,Toast.LENGTH_LONG).show();
}
}
}
第二个页面的布局,是一个自定义一个Viewgroup,进行了一个简单的流式布局
package com.example.zhoukao1_1104;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* Created by ZhangTAO on 2017/11/4.
*/
public class Main2 extends AppCompatActivity implements View.OnClickListener{
private Button btn1;
private Button btn2;
private Button btn3;
private Button btn4;
private Button btn5;
private Button btn6;
private Button btn7;
private Button btn8;
private Button btn9;
private TextView left_btn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2_activity);
initView();
setOnClick();
}
private void setOnClick() {
btn1.setOnClickListener(this);
btn2.setOnClickListener(this);
btn3.setOnClickListener(this);
btn4.setOnClickListener(this);
btn5.setOnClickListener(this);
btn6.setOnClickListener(this);
btn7.setOnClickListener(this);
btn8.setOnClickListener(this);
btn9.setOnClickListener(this);
left_btn.setOnClickListener(this);
}
private void initView() {
btn1 = (Button) findViewById(R.id.txt1);
btn2 = (Button) findViewById(R.id.txt2);
btn3 = (Button) findViewById(R.id.txt3);
btn4 = (Button) findViewById(R.id.txt4);
btn5 = (Button) findViewById(R.id.txt5);
btn6 = (Button) findViewById(R.id.txt6);
btn7 = (Button) findViewById(R.id.txt7);
btn8 = (Button) findViewById(R.id.txt8);
btn9 = (Button) findViewById(R.id.txt9);
left_btn = (TextView) findViewById(R.id.left_btn);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.txt1:
String btn1_txt = btn1.getText().toString().trim();
Toast.makeText(this, btn1_txt, Toast.LENGTH_SHORT).show();
break;
case R.id.txt2:
String btn2_txt = btn2.getText().toString().trim();
Toast.makeText(this, btn2_txt, Toast.LENGTH_SHORT).show();
break;
case R.id.txt3:
String btn3_txt = btn3.getText().toString().trim();
Toast.makeText(this, btn3_txt, Toast.LENGTH_SHORT).show();
break;
case R.id.txt4:
String btn4_txt = btn4.getText().toString().trim();
Toast.makeText(this, btn4_txt, Toast.LENGTH_SHORT).show();
break;
case R.id.txt5:
String btn5_txt = btn5.getText().toString().trim();
Toast.makeText(this,btn5_txt,Toast.LENGTH_SHORT).show();
break;
case R.id.txt6:
String btn6_txt = btn6.getText().toString().trim();
Toast.makeText(this,btn6_txt,Toast.LENGTH_SHORT).show();
break;
case R.id.txt7:
String btn7_txt = btn7.getText().toString().trim();
Toast.makeText(this,btn7_txt,Toast.LENGTH_SHORT).show();
break;
case R.id.txt8:
String btn8_txt = btn8.getText().toString().trim();
Toast.makeText(this,btn8_txt,Toast.LENGTH_SHORT).show();
break;
case R.id.txt9:
String btn9_txt = btn9.getText().toString().trim();
Toast.makeText(this,btn9_txt,Toast.LENGTH_SHORT).show();
break;
case R.id.left_btn:
Intent intent = new Intent(Main2.this,MainActivity.class);
startActivity(intent);
}
}
}
自定义标题栏,布局
package com.title;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.RelativeLayout;
import com.example.zhoukao1_1104.R;
/**
* Created by ZhangTAO on 2017/11/4.
*/
public class bar extends RelativeLayout{
public bar(Context context) {
this(context,null);
}
public bar(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public bar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs,0);
}
private void initView(Context context, AttributeSet attrs, int i) {
LayoutInflater.from(context).inflate(R.layout.title_bar_demo,this);
}
}
package com.title;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by ZhangTAO on 2017/11/2.
*/
public class My_ViewGroup extends ViewGroup{
private int width_size;
public My_ViewGroup(Context context) {
this(context,null);
}
public My_ViewGroup(Context context, AttributeSet attrs) {
this(context, attrs,0);
}
public My_ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView();
}
private void initView() {
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
measureChildren(widthMeasureSpec,heightMeasureSpec);
width_size = MeasureSpec.getSize(widthMeasureSpec);
// setMeasuredDimension(DensityUtil.dip2px(getContext(),200),
// DensityUtil.dip2px(getContext(),200));
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
//拿到子控件的个数
int childCount = getChildCount();
//定义一个临时高度变量
int startHeight = 0;
int startWidth = 0;
for(int i=0;iwidth_size) {
startWidth=0;
startHeight += v.getMeasuredHeight();
startHeight += v.getMeasuredHeight();
v.layout(startWidth+5,startHeight+3,startWidth+v.getMeasuredWidth(),startHeight+v.getMeasuredHeight());
}else {
//给每一个子View设置自己的位置
v.layout(startWidth+5,startHeight+3,startWidth+v.getMeasuredWidth(),startHeight+v.getMeasuredHeight());
startHeight += v.getMeasuredHeight();
startWidth += v.getMeasuredWidth();
}
}
}
}
package com.title;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import com.example.zhoukao1_1104.R;
/**
* Created by ZhangTAO on 2017/11/4.
*/
public class My_Progress extends View{
/**
* 圆环的颜色
*/
private int roundColor;
/**
* 圆环进度的颜色
*/
private int roundProgressColor;
/**
* 中间进度百分比的字符串的颜色
*/
private int textColor;
/**
* 中间进度百分比的字符串的字体
*/
private float textSize;
/**
* 圆环的宽度
*/
private float roundWidth;
/**
* 最大进度
*/
private int max;
/**
* 当前进度
*/
private int progress;
/**
* 是否显示中间的进度
*/
private boolean textIsDisplayable;
/**
* 进度的风格,实心或者空心
*/
private int style;
public static final int STROKE = 0;
public static final int FILL = 1;
private Paint paint;
public My_Progress(Context context) {
this(context,null);
}
public My_Progress(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public My_Progress(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initView(context, attrs,0);
}
private void initView(Context context, AttributeSet attrs, int i) {
paint = new Paint();
TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
R.styleable.RoundProgressBar);
//获取自定义属性和默认值
roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.GREEN);
roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.RED);
textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 25);
roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
mTypedArray.recycle();
}
/**
* 绘制进度条
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
/**
* 画最外层的大圆环
*/
int centre = getWidth()/2; //获取圆心的x坐标
int radius = (int) (centre - roundWidth/2); //圆环的半径
paint.setColor(roundColor); //设置圆环的颜色
paint.setStyle(Paint.Style.STROKE); //设置空心
paint.setStrokeWidth(roundWidth); //设置圆环的宽度
paint.setAntiAlias(true); //消除锯齿
canvas.drawCircle(centre, centre, radius, paint); //画出圆环
/**
* 画进度百分比
*/
paint.setStrokeWidth(0);
paint.setColor(textColor);
paint.setTextSize(textSize);
paint.setTypeface(Typeface.DEFAULT_BOLD); //设置字体
int percent = (int)(((float)progress / (float)max) * 100); //中间的进度百分比,先转换成float在进行除法运算,不然都为0
float textWidth = paint.measureText(percent + "%"); //测量字体宽度,我们需要根据字体的宽度设置在圆环中间
if(textIsDisplayable && percent != 0 && style == STROKE) {
canvas.drawText(percent + "%", centre - textWidth / 2, centre + textSize / 2, paint); //画出进度百分比
}
/**
* 画圆弧 ,画圆环的进度
*/
//设置进度是实心还是空心
paint.setStrokeWidth(roundWidth); //设置圆环的宽度
paint.setColor(roundProgressColor); //设置进度的颜色
RectF oval = new RectF(centre - radius, centre - radius, centre
+ radius, centre + radius); //用于定义的圆弧的形状和大小的界限
switch (style) {
case STROKE:{
paint.setStyle(Paint.Style.STROKE);
canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //根据进度画圆弧
break;
}
case FILL:{
paint.setStyle(Paint.Style.FILL_AND_STROKE);
if(progress !=0)
canvas.drawArc(oval, 0, 360 * progress / max, true, paint); //根据进度画圆弧
break;
}
}
}
public synchronized int getMax() {
return max;
}
/**
* 设置进度的最大值
* @param max
*/
public synchronized void setMax(int max) {
if(max < 0){
throw new IllegalArgumentException("max not less than 0");
}
this.max = max;
}
/**
* 获取进度.需要同步
* @return
*/
public synchronized int getProgress() {
return progress;
}
/**
* 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步
* 刷新界面调用postInvalidate()能在非UI线程刷新
* @param progress
*/
public synchronized void setProgress(int progress) {
if(progress < 0){
throw new IllegalArgumentException("progress not less than 0");
}
if(progress > max){
progress = max;
}
if(progress <= max){
this.progress = progress;
postInvalidate();
}
}
public int getCricleColor() {
return roundColor;
}
public void setCricleColor(int cricleColor) {
this.roundColor = cricleColor;
}
public int getCricleProgressColor() {
return roundProgressColor;
}
public void setCricleProgressColor(int cricleProgressColor) {
this.roundProgressColor = cricleProgressColor;
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public float getTextSize() {
return textSize;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
}
public float getRoundWidth() {
return roundWidth;
}
public void setRoundWidth(float roundWidth) {
this.roundWidth = roundWidth;
}
}