100字范文,内容丰富有趣,生活中的好帮手!
100字范文 > 扫二维码+阶梯流式布局+自定义画圆+组合view

扫二维码+阶梯流式布局+自定义画圆+组合view

时间:2020-08-17 03:56:38

相关推荐

扫二维码+阶梯流式布局+自定义画圆+组合view

组合viewpackage com.bawei.zhouyilianxi;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.widget.ImageView;import android.widget.LinearLayout;/*** Created by zhangyueyi on /11/4.*/public class Zuheview extends LinearLayout {private ImageView dayuhao_img;private ImageView ren_img;//点击接口private Ondianjijieko ondianjijieko;interface Ondianjijieko{void onDayuClick();void onRenClick();}public void setOndianjijieko(Ondianjijieko ondianjijieko){if(ondianjijieko!=null){this.ondianjijieko=ondianjijieko;}}public Zuheview(Context context) {this(context,null);}public Zuheview(Context context, AttributeSet attrs) {this(context, attrs,0);}public Zuheview(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context,attrs,defStyleAttr);}private void initView(Context context, AttributeSet attrs, int defStyleAttr) {View.inflate(context,R.layout.zuheview_layout,this);ImageView dyh = findViewById(R.id.img_dyh);ImageView ren = findViewById(R.id.img_ren);dyh.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {ondianjijieko.onDayuClick();}

自定义圆package com.bawei.zhouyilianxi;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.util.AttributeSet;import android.util.Log;import android.view.View;/*** Created by han on /11/4.*/public class TupianView extends View {/*** 画笔对象的引用*/private Paint paint;/*** 圆环的颜色*/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;public TupianView(Context context) {this(context, null);}public TupianView(Context context, AttributeSet attrs) {this(context, attrs, 0);}public TupianView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);paint = new Paint();TypedArray mTypedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);//获取自定义属性和默认值roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);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);// setProgress(20);// findViewById(R.id.but_ewm);mTypedArray.recycle();}@Overrideprotected 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); //画出圆环Log.e("log", centre + "");/*** 画进度百分比*/paint.setStrokeWidth(0);paint.setColor(textColor);paint.setTextSize(textSize);paint.setTypeface(Typeface.DEFAULT_BOLD); //设置字体int percent = (int) (((float) progress / (float) max) * 100); //中间的进度百分比,先转换成float在进行除法运算,不然都为0float 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;

梯形流式布局package com.bawei.zhouyilianxi;import android.content.Context;import android.util.AttributeSet;import android.view.View;import android.view.ViewGroup;/*** Created by zhangyueyi on /11/4.*/public class TixingView extends ViewGroup {private int mScreenWidth;private int horizontalSpace, verticalSpace;private float mDensity;//设备密度,用于将dp转为pxpublic TixingView(Context context) {this(context, null);}public TixingView(Context context, AttributeSet attrs) {super(context, attrs);//获取屏幕宽高、设备密度mScreenWidth = context.getResources().getDisplayMetrics().widthPixels;mDensity = context.getResources().getDisplayMetrics().density;}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);//确定此容器的宽高int widthMode = MeasureSpec.getMode(widthMeasureSpec);int widthSize = MeasureSpec.getSize(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);int heightSize = MeasureSpec.getSize(heightMeasureSpec);//测量子View的宽高int childCount = getChildCount();View child = null;//子view摆放的起始位置int left = getPaddingLeft();//一行view中将最大的高度存于此变量,用于子view进行换行时高度的计算int maxHeightInLine = 0;//存储所有行的高度相加,用于确定此容器的高度int allHeight = 0;for (int i = 0; i < childCount; i++) {child = getChildAt(i);//测量子View宽高measureChild(child, widthMeasureSpec, heightMeasureSpec);//两两对比,取得一行中最大的高度if (child.getMeasuredHeight() + child.getPaddingTop() + child.getPaddingBottom() > maxHeightInLine) {maxHeightInLine = child.getMeasuredHeight() + child.getPaddingTop() + child.getPaddingBottom();}left += child.getMeasuredWidth() + dip2px(horizontalSpace) + child.getPaddingLeft() + child.getPaddingRight();if (left >= widthSize - getPaddingRight() - getPaddingLeft()) {//换行left = getPaddingLeft();//累积行的总高度allHeight += maxHeightInLine + dip2px(verticalSpace);//因为换行了,所以每行的最大高度置0maxHeightInLine = 0;}}//再加上最后一行的高度,因为之前的高度累积条件是换行//最后一行没有换行操作,所以高度应该再加上allHeight += maxHeightInLine;if (widthMode != MeasureSpec.EXACTLY) {widthSize = mScreenWidth;//如果没有指定宽,则默认为屏幕宽}if (heightMode != MeasureSpec.EXACTLY) {//如果没有指定高度heightSize = allHeight + getPaddingBottom() + getPaddingTop();}setMeasuredDimension(widthSize, heightSize);}@Overrideprotected void onLayout(boolean changed, int l, int t, int r, int b) {if (changed) {//摆放子viewView child = null;//初始子view摆放的左上位置int left = getPaddingLeft();int top = getPaddingTop();//一行view中将最大的高度存于此变量,用于子view进行换行时高度的计算int maxHeightInLine = 0;for (int i = 0, len = getChildCount(); i < len; i++) {child = getChildAt(i);//从第二个子view开始算起//因为第一个子view默认从头开始摆放if (i > 0) {//两两对比,取得一行中最大的高度if (getChildAt(i - 1).getMeasuredHeight() > maxHeightInLine) {maxHeightInLine = getChildAt(i - 1).getMeasuredHeight();}//当前子view的起始left为 上一个子view的宽度+水平间距left += getChildAt(i - 1).getMeasuredWidth() + dip2px(horizontalSpace);if (left + child.getMeasuredWidth() >= getWidth() - getPaddingRight() - getPaddingLeft()) {//这一行所有子view相加的宽度大于容器的宽度,需要换行//换行的首个子view,起始left应该为0+容器的paddingLeftleft = getPaddingLeft();//top的位置为上一行中拥有最大高度的某个View的高度+垂直间距top += maxHeightInLine + dip2px(verticalSpace);//将上一行View的最大高度置0maxHeightInLine = 0;}}//摆放子viewchild.layout(left, top, left + child.getMeasuredWidth(), top + child.getMeasuredHeight());top+=child.getMeasuredHeight();}}}/*** dp转为px** @param dpValue* @return*/private int dip2px(float dpValue) {return (int) (dpValue * mDensity + 0.5f);}/*** 设置子view间的水平间距 单位dp** @param horizontalSpace*/public void setHorizontalSpace(int horizontalSpace) {this.horizontalSpace = horizontalSpace;}/*** 设置子view间的垂直间距 单位dp** @param verticalSpace*/public void setVerticalSpace(int verticalSpace) {this.verticalSpace = verticalSpace;}}

第二个activity(点击画圆)package com.bawei.zhouyilianxi;import android.content.Intent;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.support.v7.app.AppCompatActivity;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;import com.uuzuche.lib_zxing.activity.CaptureActivity;import com.uuzuche.lib_zxing.activity.CodeUtils;import com.uuzuche.lib_zxing.activity.ZXingLibrary;public class Main2Activity extends AppCompatActivity {private Zuheview zuhe;private Button but_ewm;private TupianView tupianView;private TextView tvPerencetValue;private int REQUEST_CODE;private int count=0;private Handler handler=new Handler(){@Overridepublic void handleMessage(Message msg) {super.handleMessage(msg);if(count<101){count+=10;tupianView.setProgress(count);handler.sendEmptyMessageDelayed(1,1000);}if(count==100){Intent intent = new Intent(Main2Activity.this, CaptureActivity.class);startActivityForResult(intent, REQUEST_CODE);}}};@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);ZXingLibrary.initDisplayOpinion(this);setContentView(R.layout.activity_main2);zuhe = (Zuheview) findViewById(R.id.zuhe_main);zuhe.setOndianjijieko(new Zuheview.Ondianjijieko() {@Overridepublic void onDayuClick() {Toast.makeText(Main2Activity.this,"已经是本页面不需要再次点击",Toast.LENGTH_LONG).show();}@Overridepublic void onRenClick() {Intent intent=new Intent(Main2Activity.this,Main22Activity.class);startActivity(intent);}});but_ewm = (Button) findViewById(R.id.but_ewm);tupianView = (TupianView) findViewById(R.id.m_circleSeekBar_set_perencet);// tupianView = (TupianView) findViewById(R.id.m_circleSeekBar_set_perencet);//开始but_ewm.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {handler.sendEmptyMessageDelayed(1,100);}});}@Overrideprotected 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(Main2Activity.this, "解析二维码失败", Toast.LENGTH_LONG).show();}}}}}

} public void setTextSize(float textSize) { this.textSize = textSize; } public float getRoundWidth() { return roundWidth; } public void setRoundWidth(float roundWidth) { this.roundWidth = roundWidth; }}

ren.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { ondianjijieko.onRenClick(); } }); }}

第一个activitypackage com.bawei.zhouyilianxi;import android.content.Intent;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;public class MainActivity extends AppCompatActivity {private Zuheview zuhe;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);zuhe = (Zuheview) findViewById(R.id.zuhe_main);zuhe.setOndianjijieko(new Zuheview.Ondianjijieko() {@Overridepublic void onDayuClick() {Intent intent=new Intent(MainActivity.this,Main2Activity.class);startActivity(intent);}@Overridepublic void onRenClick() {Intent intent=new Intent(MainActivity.this,Main22Activity.class);startActivity(intent);}});}}

第三个activitypackage com.bawei.zhouyilianxi;import android.content.Intent;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.Toast;public class Main22Activity extends AppCompatActivity {private Zuheview zuhe;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main22);zuhe = (Zuheview) findViewById(R.id.zuhe_main);zuhe.setOndianjijieko(new Zuheview.Ondianjijieko() {@Overridepublic void onDayuClick() {Intent intent=new Intent(Main22Activity.this,Main2Activity.class);startActivity(intent);}@Overridepublic void onRenClick() {Toast.makeText(Main22Activity.this,"已经是本页面不需要再次点击",Toast.LENGTH_LONG).show();;}});TixingView flexBoxLayout = (TixingView) findViewById(R.id.tixing_view);}}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。