自定义控件_画圆
来源:互联网 发布:淘宝页面怎么设计 编辑:程序博客网 时间:2024/05/16 05:00
public class MyProgressRound extends View{ Paint paint; private int mProgress = 0; private int mCountProgress = 0; private float mRadiuSize = 0; private float mRingSize = 0; private float mTextSize = 0; private int mProgressColor = 0; public MyProgressRound(Context context) { super(context); init(); } /** * 所有在xml布局文件中 标签里面声明的属性 都可以在AttributeSet类的对象中获取出来 * @param context * @param attrs */ public MyProgressRound(Context context, @Nullable AttributeSet attrs) { //在该构造方法中可以获取到 所有参数 //把参数传出去 在onDraw方法中可以操作 onMeasure中也可以操作 super(context, attrs); getCustomAttr(context, attrs); init(); } private void getCustomAttr(Context context, AttributeSet attrs) { TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyProgressRound); mRadiuSize = array.getDimension(R.styleable.MyProgressRound_radiuSize, 100); mRingSize = array.getDimension(R.styleable.MyProgressRound_ringSize, 10); mTextSize = array.getDimension(R.styleable.MyProgressRound_textSize, 10); mProgressColor = array.getColor(R.styleable.MyProgressRound_progressColor, Color.BLACK); } public MyProgressRound(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } public void init(){ paint = new Paint(); paint.setAntiAlias(true); } //widthMeasureSpec/heightMeasureSpec 是一个32为的int类型 //01000000 00000000 00000000 00000000 //高两位 代表类型 //warpcontent类型 MeasureSpec.AT_MOST //matchparent类型 MeasureSpec.EXACTLY 或者具体的长度 100dp 200dp // 其他类型 // @Override protected 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); int width = 0; int height = 0; if(widthMode == MeasureSpec.AT_MOST){ width = (int) (mRadiuSize * 2); }else{ width = Math.max(widthSize, (int) (mRadiuSize * 2)); } if(heightMode == MeasureSpec.AT_MOST){ height = (int) (mRadiuSize * 2); }else{ height = Math.max(heightSize, (int) (mRadiuSize * 2)); } setMeasuredDimension(width, height);// switch (widthMode){// case MeasureSpec.AT_MOST:// //如果宽度使用的是warp_content那么 我们需要手动设置控件的宽值, 标准是宽是半径的2倍// Log.i("=============widthMode", "onMeasure: " + "AT_MOST---> warp_content");// //确定了宽高后,修改控件的宽高// setMeasuredDimension((int)(mRadiuSize * 2), (int)(mRadiuSize * 2));// break;// case MeasureSpec.EXACTLY:// Log.i("=============widthMode", "onMeasure: " + "Exactly---> match_parent");// break;// }//// switch (heightMode){// case MeasureSpec.AT_MOST:// Log.i("=============heightMode", "onMeasure: " + "AT_MOST---> warp_content");// break;// case MeasureSpec.EXACTLY:// Log.i("=============heightMode", "onMeasure: " + "Exactly---> match_parent");// break;// } } @Override protected void onDraw(Canvas canvas) { //在布局文件中设置的圆环半径大小就可以不用写死 paint.setStrokeWidth(0); paint.setColor(Color.BLACK); paint.setStyle(Paint.Style.STROKE); canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, mRadiuSize, paint); canvas.drawCircle(getMeasuredWidth()/2, getMeasuredHeight()/2, mRadiuSize - mRingSize, paint); paint.setTextSize(mTextSize); String text = mCountProgress + "%"; float textWidth = paint.measureText(text); canvas.drawText(text, getMeasuredWidth()/2-textWidth/2 , getMeasuredWidth()/2 + mTextSize/2, paint); RectF rectF = new RectF(getMeasuredWidth()/2 - mRadiuSize + mRingSize/2,getMeasuredHeight()/2 - mRadiuSize + mRingSize/2,getMeasuredWidth()/2 + mRadiuSize - mRingSize/2,getMeasuredHeight()/2 + mRadiuSize - mRingSize/2); paint.setStrokeWidth(mRingSize); paint.setColor(mProgressColor); canvas.drawArc(rectF, 0, mProgress, false, paint); } public void setProgress(int progress){ mProgress = progress; mCountProgress = progress * 100 / 360; invalidate(); }}
======================================================================public class MainActivity extends AppCompatActivity { MyProgressRound mpr; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mpr = (MyProgressRound) findViewById(R.id.mpr); new AsyncTask<String, Integer, String>(){ @Override protected String doInBackground(String... params) { for (int i = 0; i <= 360; i++) { SystemClock.sleep(10); publishProgress(i); } return null; } //该方法的调用条件是 publishProgress @Override protected void onProgressUpdate(Integer... values) { mpr.setProgress(values[0]); } }.execute(); }}
阅读全文
0 0
- 自定义控件_画圆
- 自定义控件_自定义日历
- 自定义控件_属性介绍
- Android控件_自定义组合控件
- Android_自定义控件_倒计时控件
- 自定义控件之_自定义圆形进度条
- 13_自定义组合控件&自定义控件属性
- Android自定义控件之自定义组合控件_
- Android 自定义组件_开关控件
- 自定义控件之_验证码
- Android_组合式控件_自定义属性
- 自定义控件_测滑菜单栏
- 安卓自定义控件_视差特效
- 自定义View之_日历控件
- 自定义控件(详解_待完善)
- Android开发_自定义圆形头像控件
- 自定义控件_绘制太极(拖动)
- Android自定义控件_基础(五)
- [MIPS汇编语言]输入输出演示(数组)
- recyclerview瀑布流_图片二次采样
- RecyclerView(多条目)
- Objective-C对象之类对象和元类对象
- AngularJS1.5 以上版本
- 自定义控件_画圆
- 算法:输出华氏和摄氏的温度对照表
- 头部为横向滑动,下面Fragment和ViewPager
- 个人怎么申请微信公众号-微信公众号使用教程33
- Android侧滑菜单DrawerLayout
- huffman编码
- 属性动画的使用
- 《深入理解Nginx》读书笔记1:架构设计
- 064 不定积分之分部积分法 case 1-6 总结