自定义PointerProgressBar--圆环进度条
来源:互联网 发布:去年淘宝双11销售额 编辑:程序博客网 时间:2024/04/29 15:19
1.attr.xml定义所需属性:
<resources> <declare-styleable name="RoundProgressBar"> <attr name="roundColor" format="color"></attr> <attr name="roundProgressColor" format="color"></attr> <attr name="textSize" format="integer"></attr> <attr name="roundWidth" format="integer"></attr> <attr name="max" format="integer"></attr> <attr name="innerColor" format="color"></attr> <attr name="innerRadius" format="integer"></attr> <attr name="pointerAngle" format="integer"> </attr> <attr name="textColor" format="color"></attr> </declare-styleable></resources>
2.布局文件--progressBar.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="45dp" android:paddingRight="45dp" android:paddingTop="25dp" tools:context=".Result" > <bzu.edu.cn.health.PointProgressBar android:id="@+id/MyCircleProcessBar" android:layout_width="300dp" android:layout_height="300dp" app:roundColor="@color/aqua" app:roundProgressColor="@color/black" app:textSize="15" app:roundWidth="50" app:max="50" app:innerColor="@color/blue" app:innerRadius="35" app:pointerAngle="30" /></RelativeLayout>
3.PointProgressBar类的实现:
public class PointProgressBar extends View { private int start; // 构造方法,带有attrs自定义属性 public PointProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); paint = new Paint(); TypedArray mTypeArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgressBar); // 获取自定义属性和默认值 roundColor = mTypeArray.getColor( R.styleable.RoundProgressBar_roundColor, Color.GRAY); roundProgressColor = mTypeArray.getColor( R.styleable.RoundProgressBar_roundProgressColor, Color.RED); textColor = mTypeArray.getColor(R.styleable.RoundProgressBar_textColor, Color.WHITE); textSize = mTypeArray.getInteger( R.styleable.RoundProgressBar_textSize, 15); roundWidth = mTypeArray.getInteger( R.styleable.RoundProgressBar_roundWidth, 5); max = mTypeArray.getInteger(R.styleable.RoundProgressBar_max, 102); innerRadius = mTypeArray.getInteger( R.styleable.RoundProgressBar_innerRadius, 30); innerColor = mTypeArray.getColor( R.styleable.RoundProgressBar_innerColor, Color.BLUE); pointerAngle = mTypeArray.getInteger( R.styleable.RoundProgressBar_pointerAngle, 15); mTypeArray.recycle(); } public PointProgressBar(Context context, AttributeSet attrs) { this(context, attrs, 0); } public PointProgressBar(Context context) { this(context, null); } /** * 画笔 */ private Paint paint; /** * 内圆半径 */ private float innerRadius; /** * 内圆颜色 */ private int innerColor; /** * 指针偏角 */ private int pointerAngle; /** * 圆环的颜色 */ private int roundColor; /** * 圆环进度的颜色 */ private int roundProgressColor; /** * 中间进度百分比的字符串颜色 */ private int textColor; /** * 中间进度百分比的字符串大小 */ private float textSize; /** * 圆环的宽度 */ private float roundWidth; /** * 最大进度 */ private int max; /** * 当前进度 */ private int progress; @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); start++; /** * 画最外层的空心半圆弧 */ int center = getWidth() / 2; int radius = (int) (center - roundWidth / 2);// 半径 paint.setColor(roundColor); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(roundWidth);// 圆弧边线宽度 paint.setAntiAlias(true); // 圆弧抗锯齿 RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);// 指定圆弧的外轮廓矩形区域 canvas.drawArc(oval, -180, 180, false, paint); /** * 画中间的圆心圆 */ paint.setStrokeWidth(0); paint.setStyle(Paint.Style.FILL); paint.setColor(innerColor); canvas.drawCircle(center, center, innerRadius, paint); /** * 计算各点坐标 */ float angle = (float) (start * 18 / 10);// 需求是从-90~90度刚好旋转了180度 float cos = (float) Math.cos(Math.PI * angle / 180), sin = (float) Math .sin(Math.PI * angle / 180); float startX = center - center * cos, startY = center - center * sin; float cos1 = (float) Math.cos(Math.PI * (angle + pointerAngle) / 180); float sin1 = (float) Math.sin(Math.PI * (angle + pointerAngle) / 180); float end1X = center - innerRadius * cos1, end1Y = center - innerRadius * sin1; float cos2 = (float) Math.cos(Math.PI * (angle - pointerAngle) / 180); float sin2 = (float) Math.sin(Math.PI * (angle - pointerAngle) / 180); float end2X = center - innerRadius * cos2, end2Y = center - innerRadius * sin2; Log.e("start ", "angle =" + angle + ",sin = " + sin + ",cos = " + cos + ",x=" + startX + ",y=" + startY); /** * 画圆环进度 */ paint.setColor(roundColor); paint.setStyle(Paint.Style.STROKE); paint.setStrokeWidth(roundWidth); paint.setColor(roundProgressColor); canvas.drawArc(oval, -180, angle, false, paint); /** * 画指针 */ paint.setColor(innerColor); paint.setStrokeWidth(0); paint.setStyle(Paint.Style.FILL); Path path = new Path(); path.moveTo(startX, startY); path.lineTo(end1X, end1Y); path.lineTo(end2X, end2Y); path.close(); canvas.drawPath(path, paint); /** * 画文本进度 */ paint.setStrokeWidth(0); paint.setColor(textColor); paint.setTextSize(textSize); paint.setTypeface(Typeface.DEFAULT_BOLD); float textWidth = paint.measureText(start + ""); canvas.drawText(start + "", center - textWidth / 2, center + textSize / 2, paint); if (start < (int) (((float) progress / (float) max) * 100)) invalidate();// 不断做刷新操作 } public float getInnerRadius() { return innerRadius; } public void setInnerRadius(float innerRadius) { this.innerRadius = innerRadius; } public int getRoundColor() { return roundColor; } public void setRoundColor(int roundColor) { this.roundColor = roundColor; } public int getRoundProgressColor() { return roundProgressColor; } public void setRoundProgressColor(int roundProgressColor) { this.roundProgressColor = roundProgressColor; } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public float getTextSize() { return textSize; } public void setTextSize(float textSize) { this.textSize = textSize; } public float getRoundWidth() { return roundWidth; } public void setRoundWidth(float roundWidth) { this.roundWidth = roundWidth; } public synchronized int getMax() { return max; } public synchronized void setMax(int max) { if (max < 0) throw new IllegalArgumentException("max not less than 0"); this.max = max; } public synchronized int getProgress() { return progress; } public synchronized void setProgress(int progress) { if (max < 0) throw new IllegalArgumentException("progress not less than 0"); if (progress > max) progress = max; if (progress <= max) { this.progress = progress; postInvalidate(); } } public int getInnerColor() { return innerColor; } public void setInnerColor(int innerColor) { this.innerColor = innerColor; }}
4.MainActivity类的实现:
public class MainActivity01 extends AppCompatActivity {private PointProgressBar pointProgressBar; private Button btnResult1; private TextView tvResult; private Toast toast; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progress_result); btnResult1 = (Button) findViewById(R.id.btnResult1); tvResult = (TextView) findViewById(R.id.tv_result); //getWellcometips(); pointProgressBar = (PointProgressBar) findViewById(R.id.MyCircleProcessBar); new ProgressAnimation().execute(); Resources res =getResources(); Random random=new Random(); String[] sports=res.getStringArray(R.array.sports); sports=this.getResources().getStringArray(R.array.sports); int id =(int)(Math.random()*(sports.length-1)); tvResult.setText(sports[id]); tvResult.setVisibility(View.INVISIBLE); //Toast.makeText(getApplicationContext(),) //toast.setText(); Toast.makeText(getApplicationContext(),tvResult.getText(),Toast.LENGTH_LONG).show(); } class ProgressAnimation extends AsyncTask<Void, Integer, Void> { @Override protected Void doInBackground(Void... params) { //进度值不断的变化 for (int i = 0; i <pointProgressBar.getMax(); i++) { try { publishProgress(i); Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } return null; } // 获得欢迎语资源 @Override protected void onProgressUpdate(Integer... values) { //更新进度值 pointProgressBar.setProgress(values[0]); pointProgressBar.invalidate(); super.onProgressUpdate(values); } }}
阅读全文
0 0
- 自定义PointerProgressBar--圆环进度条
- 自定义圆环进度条
- 自定义圆环进度条
- 自定义View圆环进度条
- 自定义view圆环进度条
- 自定义圆环进度条
- 自定义圆环进度条
- 自定义view圆环进度条
- 自定义圆环进度条
- 自定义圆环进度条+自定义属性
- 自定义控件Android圆环进度条
- 自定义View之圆环进度条
- Android自定义view 圆环进度条
- 自定义View————圆环和圆环进度条
- android,自定义进度条样式,圆环,圆弧
- Android自定义View圆环带文字进度条
- Android 自定义圆环进度条 自适应显示百分比
- Android自定义控件之百分比圆环进度条
- PostgreSQL 创建和删除角色
- 被微软收购后,领英发布最权威的全球VR人才报告
- 排序算法之选择排序
- Android_ExpandableListView_仿QQ好友列表
- Spring事务Transaction配置的五种注入方式详解
- 自定义PointerProgressBar--圆环进度条
- Windows内核模式和用户模式
- ARM开机自动获取ip及网络启动总结
- IC设计基础系列之CDC篇1:clock domain crossing(CDC) (一 CDC的基本概念)
- 一张图搞定-RxJava2的线程切换原理和内存泄露问题
- UESTC 1606 难喝的饮料 0-1背包+完全背包
- linux下Sqlit安装及应用
- 基于单链表以及环形队列的生产者消费者模型
- MyEclipse中出现Multiple annotations found at this line: