圆环进度
//开启线程
new Thread(cv).start();
public class CritleView extends View implements Runnable{
private Paint innerPaint;
private int innerRadius=170;
private RectF rectF;
private Paint outPaint;
private int outRadiusWidth=20;
private int progress=0;
private int totalProgress=100;
private Paint textPaint;
private int cricley=300;
private int centerX;
private int centerY;
private int outRingRadius;
public CritleView(Context context) {
this(context,null);
}
public CritleView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public CritleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private void init() {
innerPaint = new Paint();
innerPaint.setColor(Color.WHITE);
innerPaint.setAntiAlias(true);
outPaint=new Paint();
//将画笔设置为
outPaint.setStyle(Paint.Style.STROKE);
outPaint.setStrokeWidth(outRadiusWidth);
outPaint.setStrokeCap(Paint.Cap.ROUND);
//去齿轮
outPaint.setAntiAlias(true);
outPaint.setColor(Color.GREEN);
textPaint=new Paint();
textPaint.setTextSize(30);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//得到测量模式
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
//得到控件的宽高
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
//定义空间的宽和高
int width=0;
int height=0;
switch(widthMode){
case MeasureSpec.AT_MOST:{
width=innerRadius*2+outRadiusWidth*2;
}
break;
case MeasureSpec.EXACTLY:{
width=widthSize;
}
break;
}
switch(heightMode){
case MeasureSpec.AT_MOST:{
height=innerRadius*2+outRadiusWidth*2;
}
break;
case MeasureSpec.EXACTLY:{
height=heightSize;
}
break;
}
//测量宽高
setMeasuredDimension(width,height);
}
//获取到最后的宽和高
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
//得到中心点
centerX=w/2;
centerY=h/2;
//获取到圆环的半径
outRingRadius=w/2-outRadiusWidth;
rectF = new RectF();
rectF.left=getWidth()/2-innerRadius;
rectF.top=cricley-innerRadius;
rectF.right=getWidth()/2+innerRadius;
rectF.bottom=cricley+innerRadius;
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
innerCircleDraw(canvas);
OutCircleDraw(canvas);
textDraw(canvas);
}
private void textDraw(Canvas canvas) {
String str=progress+"%";
Paint.FontMetrics fm = new Paint.FontMetrics();
//文字高度
int height = (int) Math.ceil(fm.descent - fm.ascent);
//文字宽度
int width=(int)textPaint.measureText(str,0,str.length());
canvas.drawText(str,getWidth()/2-(width/2),cricley-(height/2),textPaint);
}
private void OutCircleDraw(Canvas canvas) {
canvas.drawArc(rectF,-90,(progress*360)/totalProgress,false,outPaint);
}
private void innerCircleDraw(Canvas canvas) {
canvas.drawCircle(getWidth()/2,cricley,innerRadius,innerPaint);
}
@Override
public void run() {
while(true){
if (progress<100){
progress++;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
postInvalidate();
}
}
}
}
组合控件
public class myview extends LinearLayout {
private final View view;
public myview(Context context) {
this(context,null);
}
public myview(Context context, @Nullable AttributeSet attrs) {
this(context, attrs,0);
}
public myview(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
view=View.inflate(context, R.layout.biaotilan,this);
}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#D3DAEC"
>
<Button
android:id="@+id/fan"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@mipmap/jian"
android:layout_weight="1"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="4"
android:layout_gravity="center"
android:layout_marginLeft="90dp"
android:textSize="28dp"
android:text="那些花儿"
/>
<Button
android:id="@+id/tiao"
android:layout_weight="1"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="@mipmap/ic_launcher_round"/>
</LinearLayout>
流式布局,继承ViewGroup
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//获取宽度和高度
measureChildren(widthMeasureSpec,heightMeasureSpec);
}
/**
* 用来确定子View的位置
* @param changed
* @param l lef 相对于父容器
* @param t top
* @param r right
* @param b bottom
*/
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int currentWidth=0;
int currentHeight=0;
//得到子view的个数
int childCount = getChildCount();
for (int i=0;i<childCount;++i){
//得到这个ViewGroup里面的每一个子View
View childView = getChildAt(i);
//得到每一个子view的宽高
int width = childView.getMeasuredWidth();
int height = childView.getMeasuredHeight();
//确定子view的高度
childView.layout(currentWidth,currentHeight,currentWidth+width,currentHeight+height);
//累加所有子view的宽高
currentWidth+=width;
currentHeight+=height;
}
}
二维码
常用依赖
compile 'cn.yipianfengye.android:zxing-library:2.2'
Application做准备工作
public class App extends Application{
@Override
public void onCreate() {
super.onCreate();
ZXingLibrary.initDisplayOpinion(this);
}
}
不要忘记在清单文件中注册
需要加的权限
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
代码
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 (requestCode == 1) {
//处理扫描结果(在界面上显示)
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();
}
}
}
}