进度条加载完成后跳转到扫描二维码页面
说明
在点击右侧的回调方法中跳转到图二所示页面,图二的页面标题栏显示上一个页面组合View的视图,通过自定义ViewGroup的方式实现梯形布局
点击扫描二维码按钮之后,进度条开始以每秒10%的进度进行,当进度条走到100%后,跳转到图三的扫描二维码页面,实现扫描二维码的功能
导入zxing依赖
compile 'cn.yipianfengye.android:zxing-library:2.2'
效果图
activity_main布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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:orientation="vertical" android:layout_height="match_parent" tools:context="com.samsung.zhoukaomoni.MainActivity"> <com.samsung.zhoukaomoni.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" /> <com.samsung.zhoukaomoni.ProgressView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/circleView" android:layout_gravity="center" android:layout_marginTop="30dp" /> <Button android:layout_gravity="center" android:layout_width="match_parent" android:layout_height="50dp" android:text="扫描二维码" android:id="@+id/btn_sm" android:layout_marginTop="30dp" android:onClick="start" /> </LinearLayout>
activity_trapezoid布局
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.samsung.zhoukaomoni.TitleLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorAccent" ></com.samsung.zhoukaomoni.TitleLayout> <com.samsung.zhoukaomoni.MyViewGroup android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="127dp" android:layout_height="50dp" android:background="#619ea0" /> <TextView android:layout_width="127dp" android:layout_height="50dp" android:background="#7561a0"/> <TextView android:layout_width="127dp" android:layout_height="50dp" android:background="#9fa061" /> </com.samsung.zhoukaomoni.MyViewGroup> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <ImageView android:layout_width="wrap_content" android:layout_height="50dp" android:src="@drawable/topbar_up" android:id="@+id/img" android:layout_weight="1" /> <TextView android:layout_width="wrap_content" android:layout_height="50dp" android:id="@+id/tv" android:text="那些花儿" android:layout_weight="2" android:gravity="center" /> <ImageView android:layout_width="wrap_content" android:layout_height="50dp" android:src="@mipmap/ic_launcher_round" android:layout_weight="1" android:id="@+id/img2" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.samsung.zhoukaomoni"> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.FLASHLIGHT" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> <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" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <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.samsung.zhoukaomoni.MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.samsung.zhoukaomoni.Trapezoid"></activity> </application> </manifest>
public class MainActivity extends AppCompatActivity{ private Button btn_tiao; private Button btn_sm; int REQUEST_CODE=1; private ProgressView circleView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getSupportActionBar().hide(); ZXingLibrary.initDisplayOpinion(this); circleView = (ProgressView) findViewById(R.id.circleView); btn_sm =(Button) findViewById(R.id.btn_sm); // btn_tiao= (Button) findViewById(R.id.btn_tiao); // btn_tiao.setOnClickListener(new View.OnClickListener() { // @Override // public void onClick(View view) { // Intent in =new Intent(MainActivity.this,Trapezoid.class); // startActivity(in); // } // }); } int progress = 0; public void start(View v) { circleView.setMax(100); progress=0; new Thread() { public void run() { while (true) { progress = progress + 1; String text = progress + "%"; circleView.setProgressAndText(progress, text); try { sleep(30); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (progress == 100) { Intent intent = new Intent(MainActivity.this, CaptureActivity.class); startActivityForResult(intent, REQUEST_CODE); break; } } }; }.start(); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == REQUEST_CODE) { //处理扫描结果(在界面上显示) if (null != data) { Bundle bundle = data.getExtras(); if (bundle == null) { return; } if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_SUCCESS) { String result = bundle.getString(CodeUtils.RESULT_STRING); Toast.makeText(this, "解析结果:" + result, Toast.LENGTH_LONG).show(); } else if (bundle.getInt(CodeUtils.RESULT_TYPE) == CodeUtils.RESULT_FAILED) { Toast.makeText(MainActivity.this, "解析二维码失败", Toast.LENGTH_LONG).show(); } } } } } 主包下MyViewGroup类
public class MyViewGroup extends ViewGroup { public MyViewGroup(Context context) { super(context); } public MyViewGroup(Context context, AttributeSet attrs) { super(context, attrs); } public MyViewGroup(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); measureChildren(widthMeasureSpec,heightMeasureSpec); } @Override protected void onLayout(boolean changed, int l, int t, int r, int b) { int count = getChildCount(); int startWidth = 0; int startHeight = 0; for (int i=0; i<count; i++){ View v = getChildAt(i); v.layout(startWidth,startHeight,startWidth+v.getMeasuredWidth(),startHeight+v.getMeasuredHeight()); startWidth += v.getMeasuredWidth(); startHeight += v.getMeasuredHeight(); } } }
public class ProgressView extends View { int progress = 0; private String text="0%"; private int max = 100; public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public ProgressView(Context context, AttributeSet attrs) { super(context, attrs); } public ProgressView(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 对于画笔 Paint paint = new Paint(); // 设置抗锯齿 paint.setAntiAlias(true); // 设置画笔颜色 // 三种样式--Stroke 只要描边 Fill 填充 FILL_AND_STROKE和既有描边又有填充 paint.setStyle(Style.STROKE); //设置描边宽度 paint.setStrokeWidth(2); //定义外圈员的颜色 paint.setColor(Color.RED); //绘制圆形进度条--获取当前控件多大,正好让进度条在这个控件区间内 canvas.drawCircle(getMeasuredWidth()/2, getMeasuredWidth()/2, getMeasuredWidth()/2, paint); //重新设置描边宽度,这个宽度最好能完全盖过圆形 paint.setStrokeWidth(3); //定义限制圆弧的矩形,当前这样定义正好让圆弧和圆重合 RectF oval = new RectF(0, 0, getMeasuredWidth(), getMeasuredWidth()); //设置进度条(圆弧的颜色) paint.setColor(Color.GREEN); //绘制,设置进度条的度数从0开始,结束值是个变量,可以自己自由设置,来设置进度 //true和false 代表是否使用中心点,如果true,代表连接中心点,会出现扇形的效果 canvas.drawArc(oval, 0, 360 * progress / max, false, paint); //文字的绘制 paint.setTextSize(40); //设置文字宽度 paint.setStrokeWidth(1.0f); //测量文字大小-提前准备个矩形 Rect bounds = new Rect(); //测量文字的宽和高,测量的值可以根据矩形获取 paint.getTextBounds(text, 0, text.length(), bounds); paint.setColor(Color.BLACK); paint.setStyle(Style.FILL); //绘制文字,计算文字的宽高进行设置 canvas.drawText(text, getMeasuredWidth()/2 - bounds.width() / 2, getMeasuredWidth()/2 + bounds.height() / 2, paint); } /** * 初始设置当前进度的最大值-默认100 * @param max */ public void setMax(int max) { this.max = max; } /** * 更新进度和文字 * @param progress * @param text */ public void setProgressAndText(int progress, String text) { this.progress = progress; this.text = text; //重新绘制 postInvalidate(); } }
public class TitleLayout extends LinearLayout { private ImageView img; private TextView tv; private ImageView img2; public TitleLayout(Context context, AttributeSet attrs) { super(context, attrs); //动态加载标题栏布局 LayoutInflater.from(context).inflate(R.layout.custom_layout, this); initView(); img.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"嘻嘻",Toast.LENGTH_SHORT).show(); } }); tv.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(getContext(),"哈哈",Toast.LENGTH_SHORT).show(); } }); img2.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent =new Intent(getContext(),Trapezoid.class); getContext().startActivity(intent); } }); } private void initView() {//初始化控件 img= findViewById(R.id.img); img2=findViewById(R.id.img2); tv =findViewById(R.id.tv); } }
public class Trapezoid extends Activity { @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_trapezoid); } }