package ztz.com.zhangtianzhu1104_rk; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.Toast; import com.xys.libzxing.zxing.activity.CaptureActivity; import ztz.com.customviewgroup.CustomTitle.CustomTitle; import ztz.com.zhangtianzhu1104_rk.view.CustomProgress; public class MainActivity extends AppCompatActivity implements CustomTitle.MyCallBack,CustomProgress.MyCallBack { private CustomProgress customProgress_id; private Button btn_qr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取title的id CustomTitle customTitle_id = findViewById(R.id.customTitle_id); //获取圆形进度条的id customProgress_id = findViewById(R.id.customProgress_id); btn_qr = findViewById(R.id.btn_QR); //设置接口setCallBack customTitle_id.setCallBack(this); //设置接口setMyCallBack customProgress_id.setMyCallBack(this); //扫描二维码点击事件 btn_qr.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //调执行圆形进度条的方法 customProgress_id.progressThread(); } }); } //点击事件 @Override public void backClick() { Toast.makeText(MainActivity.this,"back",Toast.LENGTH_SHORT).show(); } //点击事件 @Override public void settingClick() { Toast.makeText(MainActivity.this,"setting",Toast.LENGTH_SHORT).show(); } //接口回调的方法 @Override public void tiaozhuan() { Intent intent = new Intent(MainActivity.this, CaptureActivity.class); startActivityForResult(intent,1); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (resultCode == RESULT_OK) { Bundle bundle = data.getExtras(); String result = bundle.getString("result"); Toast.makeText(MainActivity.this,result,Toast.LENGTH_SHORT).show(); } else if (resultCode == RESULT_CANCELED) { Toast.makeText(MainActivity.this,"扫描失败",Toast.LENGTH_SHORT).show(); } } }
package ztz.com.customviewgroup.CustomTitle; import android.content.Context; import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.Button; import android.widget.LinearLayout; import ztz.com.customviewgroup.R; /** * Created by TR on 2017/11/4. */ public class CustomTitle extends LinearLayout { public CustomTitle(Context context) { super(context); } public CustomTitle(final Context context, AttributeSet attrs) { super(context, attrs); View view = LayoutInflater.from(context).inflate(R.layout.ctitlelayout, null); addView(view); Button btn_back =view.findViewById(R.id.btn_back); Button btn_setting =view.findViewById(R.id.btn_setting); //返回 btn_back.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { callBack.backClick(); } }); //设置 btn_setting.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { callBack.settingClick(); } }); } public CustomTitle(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } private MyCallBack callBack ; public void setCallBack(MyCallBack callBack){ this.callBack = callBack; } public interface MyCallBack { public void backClick(); public void settingClick(); } }
//自定义进度条
package ztz.com.zhangtianzhu1104_rk.view; import android.content.Context; 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.View; /** * 自定义视图_进度条 */ public class CustomProgress extends View { //定义一个画笔 private Paint mpaint; private int progress = 0; private Rect textrect; private Context context; private int text; private Paint cpaint; public CustomProgress(Context context, AttributeSet attrs) { super(context, attrs); this.context = context; //创建一个画笔 mpaint = new Paint();//弧 cpaint = new Paint();//圆 //设置抗锯齿 mpaint.setAntiAlias(true); cpaint.setAntiAlias(true); //画笔的颜色 mpaint.setColor(Color.BLUE); cpaint.setColor(Color.RED); //是否填充画笔 mpaint.setStyle(Paint.Style.STROKE); cpaint.setStyle(Paint.Style.STROKE); } public void progressThread() { new Thread(){ @Override public void run() { super.run(); while (true){ if(progress>=360){ //当超过360时,直接跳转到扫描二维码 text =(int)progress/360 *100; if(text == 100 ){ //页面跳转到扫描二维码的回调 callBack.tiaozhuan(); } progress = 0; return ; } //刷新 postInvalidate(); progress+=10; try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } }.start(); } public CustomProgress(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } public CustomProgress(Context context) { super(context); } //重写ondraw方法 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); //获取当前view的宽度 int x = getWidth() / 2; int y = getHeight() / 2; //半径 int radius = 100; //设置画笔的粗细 cpaint.setStrokeWidth(10); mpaint.setStrokeWidth(10); //定义view的区域 RectF rectF = new RectF(x-radius, y-radius, x+radius, y+radius); //画圆 canvas.drawCircle(x,y,radius,cpaint); //画弧 canvas.drawArc(rectF,-90,progress,false,mpaint); text = (int)((float)progress / 360 * 100); //字体的粗细 mpaint.setStrokeWidth(2); //获取字符串的宽度 int textWidth = (int) mpaint.measureText(text +"%"); //获取字符串的高度 textrect = new Rect(); //字号的大小 mpaint.setTextSize(30); mpaint.getTextBounds(text +"%",0,(text +"%").length(), textrect); //画文字 canvas.drawText(text +"%",x-textWidth/2,y+textrect.height() /2,mpaint); } private MyCallBack callBack; public void setMyCallBack(MyCallBack callBack){ this.callBack = callBack; } public interface MyCallBack{ public void tiaozhuan(); } }