activity_main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <com.day05.TitleView android:id="@+id/titleview_id" android:layout_width="match_parent" android:layout_height="wrap_content"></com.day05.TitleView> </LinearLayout> <com.day05.JdtView android:id="@+id/jdt_view" android:layout_width="match_parent" android:layout_height="300dp" /> <Button android:id="@+id/sm" android:layout_width="150dp" android:layout_height="50dp" android:layout_gravity="center_horizontal" android:text="扫描二维码" /> </LinearLayout>
activity_main2.xml
<?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:background="#D1EEEE" android:orientation="horizontal"> <ImageButton android:id="@+id/left_btn" android:layout_width="50dp" android:layout_height="wrap_content" android:src="@drawable/left_title" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center" android:text="那些花儿" android:textSize="30dp" /> <ImageButton android:id="@+id/right_btn" android:layout_width="50dp" android:layout_height="wrap_content" android:src="@drawable/right_title" /> </LinearLayout> <com.day05.TiXingView android:layout_width="match_parent" android:layout_height="wrap_content" /> </LinearLayout>
title_layout.xml
<?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="60dp" android:background="#D1EEEE" android:orientation="horizontal"> <ImageButton android:id="@+id/left_btn" android:layout_width="50dp" android:layout_height="wrap_content" android:src="@drawable/left_title" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_gravity="center_vertical" android:layout_weight="1" android:gravity="center" android:text="那些花儿" android:textSize="30dp" /> <ImageButton android:id="@+id/right_btn" android:layout_width="50dp" android:layout_height="wrap_content" android:src="@drawable/right_title" /> </LinearLayout> </LinearLayout>
MainActivity.java
package com.day05; import android.app.Activity; import android.content.Intent; import android.os.Bundle;; import android.view.View; public class MainActivity extends Activity implements TitleView.LeftCallBack { public TitleView titleView; private JdtView jdtView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); jdtView = findViewById(R.id.jdt_view); findViewById(R.id.sm).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { jdtView.start(); } }); titleView = (TitleView) findViewById(R.id.titleview_id); titleView.setCallBack(this); } @Override public void leftClick() { } @Override public void rightClick() { Intent intent = new Intent(MainActivity.this, MainActivity2.class); startActivity(intent); } }
MainActivity2.java
package com.day05; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ImageButton; /** * Created by lenovo on 2017/11/04. */ public class MainActivity2 extends Activity { private ImageButton back; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); back = findViewById(R.id.left_btn); back.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Intent intent = new Intent(MainActivity2.this, MainActivity.class); startActivity(intent); } }); } }
TitleView.java
package com.day05; /** * Created by lenovo on 2017/11/04. */ import android.content.Context; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageButton; import android.widget.LinearLayout; public class TitleView extends LinearLayout { public TitleView(Context context) { this(context, null); } public TitleView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public TitleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // setContentView View inflate = LayoutInflater.from(context).inflate(R.layout.title_layout, this); ImageButton left = inflate.findViewById(R.id.left_btn); ImageButton right = inflate.findViewById(R.id.right_btn); left.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { callBack.leftClick(); } }); right.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { callBack.rightClick(); } }); } public LeftCallBack callBack; public void setCallBack(LeftCallBack callBack) { this.callBack = callBack; } interface LeftCallBack { public void leftClick(); public void rightClick(); } }
TiXingView.java
package com.day05; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; /** * Created by lenovo on 2017/11/04. */ public class TiXingView extends View { private Paint paint = new Paint(); public TiXingView(Context context) { super(context); } public TiXingView(Context context, AttributeSet attrs) { super(context, attrs); } public TiXingView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int x = getWidth(); int y = getHeight(); paint.setColor(Color.YELLOW); canvas.drawRect(0, 0, x/3, 125, paint); paint.setColor(Color.GREEN); canvas.drawRect(x/3, 125, x/3*2, 250, paint); paint.setColor(Color.YELLOW); canvas.drawRect(x/3*2, 250, x, 375, paint); } }
JdtView.java
package com.day05; import android.content.Context; import android.content.Intent; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.yzq.zxinglibrary.android.CaptureActivity; /** * Created by lenovo on 2017/11/04. */ public class JdtView extends View { private Paint paint = new Paint(); private boolean runing = true; private int p = 0; Context context; public JdtView(Context context) { super(context); } public JdtView(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; paint.setAntiAlias(true);//抗锯齿 paint.setStyle(Paint.Style.STROKE);//设置画笔 填充是空心的 } public void start() { new Thread(new Runnable() { @Override public void run() { while (runing) { if (p >= 360) { runing = false; return; } System.out.println("p = " + p); p += 10; postInvalidate(); //子线程刷新 系统调用onDraw() 方法 try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } } }).start(); } public JdtView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int x = getWidth() / 2; int y = getHeight() / 2; paint.setColor(Color.GRAY);//设置画笔的颜色 paint.setStrokeWidth(30);//设置画笔的粗细 canvas.drawCircle(x, y, 200, paint); paint.setColor(Color.GREEN);//设置画笔的颜色 int radius = 200;//设置园的大小 //定义一个区域 RectF rectF = new RectF(x - radius, y - radius, x + radius, y + radius); //画弧 // useCentor true 从中心点开始画 false 中心点不现实 canvas.drawArc(rectF, -0, p, false, paint); int text = (int) ((float) p / 360 * 100); // measureText 测量字符串的宽度 float textWidth = paint.measureText(text + "%"); Rect rextText = new Rect(); // rextText.height() 获取字符串的高度 paint.getTextBounds(text + "%", 0, (text + "%").length(), rextText); paint.setTextSize(30); paint.setStrokeWidth(1); //画文字 canvas.drawText(text + "%", x - textWidth / 2, y + rextText.height() / 2, paint); if (p == 360) { Intent intent = new Intent(context, CaptureActivity.class); context.startActivity(intent); } } }
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="panjiangang.bwie.com.day05"> <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" /> <!--Manifest.permission.CAMERA--> <!--Manifest.permission.READ_EXTERNAL_STORAGE--> <uses-permission android:name="android.permission.CAMERA"/> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".MainActivity2"></activity> </application> </manifest>
apply plugin: 'com.android.application' android { compileSdkVersion 26 buildToolsVersion "26.0.1" defaultConfig { applicationId "com.day05" minSdkVersion 16 targetSdkVersion 26 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:26.0.0-alpha1' testCompile 'junit:junit:4.12' /*添加依赖*/ compile 'com.github.yuzhiqiang1993:zxing:2.0.0' }
/// Top-level build file where you can add configuration options common to all sub-projects/modules. buildscript { repositories { jcenter() } dependencies { classpath 'com.android.tools.build:gradle:2.2.2' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files } } allprojects { repositories { jcenter() maven { url 'https://jitpack.io' } } } task clean(type: Delete) { delete rootProject.buildDir }