网站索引量,怎么做网站小图标,网站数据分析,c2c电商平台有哪些家本节介绍自定义view-圆形进度条思路#xff1a;根据前面介绍的自定义view内容可拓展得之#xff1b;1#xff1a;新建类继承自View2#xff1a;添加自定义view属性3#xff1a;重写onDraw(Canvas canvas)4#xff1a;实现功能下面上代码1.自定义view代码#xff1a; pub…本节介绍自定义view-圆形进度条 思路 根据前面介绍的自定义view内容可拓展得之 1新建类继承自View 2添加自定义view属性 3重写onDraw(Canvas canvas) 4实现功能 下面上代码1.自定义view代码 public class CustomView extends View {//背景圆环颜色private int circleColor;//进度条颜色字体颜色(为了美观所以设计字体颜色和进度条颜色值一致)private int secondCircleColor;//进度条背景圆环宽度private float stroke_width;//进度值private float progress;//总进度值默认为100private float totalProgress;//字体大小private float textSize;//填充模式private int style_type;public CustomView(Context context) {super(context);}public CustomView(Context context, AttributeSet attrs) {super(context, attrs);TypedArray arraycontext.obtainStyledAttributes(attrs, R.styleable.CustomView);circleColorarray.getColor(R.styleable.CustomView_circleColor, Color.BLACK);secondCircleColorarray.getColor(R.styleable.CustomView_secondCircleColor, Color.RED);stroke_widtharray.getDimension(R.styleable.CustomView_stroke_width, 2);progressarray.getFloat(R.styleable.CustomView_progress, 0);totalProgressarray.getFloat(R.styleable.CustomView_totalProgress, 100);textSizearray.getDimension(R.styleable.CustomView_textSize, 16);style_typearray.getInt(R.styleable.CustomView_style_Type, 0);}public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);}public void setCircleColor(int color){circleColorcolor;}public int getCircleColor(){return circleColor;}public void setSecondCircleColor(int color){secondCircleColorcolor;}public int getSecondColor(){return secondCircleColor;}public void setStrokeWidth(float width){stroke_widthwidth;}public float getStrokeWidth(){return stroke_width;}public void setProgress(float progress){this.progressprogress;postInvalidate();//刷新界面}public float getProgress(){return this.progress;}public void setTotalProgress(float totalProgress){this.totalProgresstotalProgress;}public float getTotalProgress(){return this.totalProgress;}public void setTextSize(float textSize){this.textSizetextSize;}public float getTextSize(){return this.textSize;}Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//第一进度圆final Paint paint_backgroundnew Paint();paint_background.setAntiAlias(true);paint_background.setStrokeWidth(stroke_width);paint_background.setStyle(Style.STROKE);paint_background.setColor(circleColor);//第二进度圆final Paint paint_progressnew Paint();paint_progress.setAntiAlias(true);paint_progress.setStrokeWidth(stroke_width);if(style_type0){paint_progress.setStyle(Style.STROKE);}else if(style_type1){paint_progress.setStyle(Style.FILL_AND_STROKE);}paint_progress.setColor(secondCircleColor);//画textfinal Paint paint_textnew Paint();paint_text.setAntiAlias(true);if(style_type0){paint_text.setColor(secondCircleColor);}else if(style_type1){paint_text.setColor(circleColor);}paint_text.setTextSize(textSize);paint_text.setTextAlign(Align.CENTER);if(getWidth()!getHeight()){throw new IllegalArgumentException(高度和宽度必须相等);//控制宽度和高度}else{RectF circle_backgroundnew RectF();circle_background.leftgetLeft()stroke_width;circle_background.rightgetRight()-stroke_width;circle_background.topgetTop()stroke_width;circle_background.bottomgetBottom()-stroke_width;canvas.drawArc(circle_background, -90, 360, false, paint_background);RectF circle_progressnew RectF();circle_progress.leftgetLeft()stroke_width;circle_progress.rightgetRight()-stroke_width;circle_progress.topgetTop()stroke_width;circle_progress.bottomgetBottom()-stroke_width;if(progresstotalProgress){throw new IllegalArgumentException(当前进度值不能大于总进度值);}else{if(style_type0){canvas.drawArc(circle_progress, -90, progress/totalProgress*360, false, paint_progress);}else if(style_type1){canvas.drawArc(circle_progress, -90, progress/totalProgress*360, true, paint_progress);}}canvas.drawText((int)progress/(int)totalProgress, getLeft()getWidth()/2, getTop()getHeight()/2textSize/4, paint_text);}}} 2attr属性 ?xml version1.0 encodingutf-8?
resources!--declare-styleable:声明样式类型;attr name声明属性名;format属性的类型 --declare-styleable nameCustomEditTextattr namelineColor formatcolor /attr namelineHeight formatdimension//declare-styleabledeclare-styleable nameCustomViewattr namestroke_width formatdimension/attr namecircleColor formatcolor/attr namesecondCircleColor formatcolor/attr nameprogress formatfloat/attr nametotalProgress formatfloat/attr nametextSize formatdimension/attr namestyle_Typeenum namestroke value0/enum namestroke_and_fill value1//attr/declare-styleable/resources3xml布局文件 RelativeLayout xmlns:androidhttp://schemas.android.com/apk/res/androidxmlns:toolshttp://schemas.android.com/toolsandroid:layout_widthwrap_contentandroid:layout_heightwrap_content com.anqiansong.views.CustomViewxmlns:circlehttp://schemas.android.com/apk/res/com.anqiansong.androidcustomviewandroid:idid/customviewandroid:layout_width50dpandroid:layout_height50dpandroid:layout_centerInParenttruecircle:circleColor#000000circle:secondCircleColor#ff0000circle:stroke_width2dpcircle:totalProgress100 circle:progress10circle:style_Typestroke//RelativeLayout 当xml文件中circle:style_Typestroke时 当xml文件中circle:style_Typestroke_and_fill时 4activity中调用 public class MainActivity extends ActionBarActivity {CustomView customView;private float progress0;Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);customView(CustomView) findViewById(R.id.customview);handler.sendEmptyMessageDelayed(0, 1000);}Handler handlernew Handler(){public void handleMessage(android.os.Message msg) {if(msg.what0){if(progress100){return;}else{customView.setProgress(progress);progress2;handler.sendEmptyMessageDelayed(0, 100);}}};};} 当xml文件中circle:style_Typestroke_and_fill时