自定义ViewProgress
来源:互联网 发布:淘宝信誉度怎么说 编辑:程序博客网 时间:2024/04/27 15:17
转载http://blog.csdn.net/lmj623565791/article/details/24500107
首先在values文件下穿件一个attrs.xml文件
<?xml version="1.0" encoding="utf-8"?><resources> <attr name="firstColor" format="color"/> <attr name="secondColor" format="color"/> <attr name="circleWidth" format="dimension"/> <attr name="speed" format="integer"/> <declare-styleable name="CustomProgressBar" > <attr name="firstColor" /> <attr name="secondColor" /> <attr name="circleWidth" /> <attr name="speed" /> </declare-styleable></resources>
自定义一个CustomView
public class CustomProgressBar extends View { private int mCircleWidth; private int mFirstCoclor; private int mSecondColor; private int mSpeed; private Paint mPaint; int mProgress; boolean isNext; public CustomProgressBar(Context context) { this(context,null); } public CustomProgressBar(Context context, AttributeSet attrs) { this(context, attrs,0); } public CustomProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initView(context, attrs, defStyleAttr); } private void initView(Context context, AttributeSet attrs, int defStyleAttr) { TypedArray typedArray = context.getTheme().obtainStyledAttributes(attrs,R.styleable.CustomProgressBar,defStyleAttr,0); int indexCount = typedArray.getIndexCount(); for (int i = 0; i < indexCount; i++) { int attr = typedArray.getIndex(i); switch (attr){ case R.styleable.CustomProgressBar_circleWidth: mCircleWidth = typedArray.getDimensionPixelSize(attr, (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 20, getResources().getDisplayMetrics())); break; case R.styleable.CustomProgressBar_firstColor: mFirstCoclor = typedArray.getColor(attr, Color.GREEN); break; case R.styleable.CustomProgressBar_secondColor: mSecondColor = typedArray.getColor(attr, Color.RED); break; case R.styleable.CustomProgressBar_speed: mSpeed = typedArray.getInt(attr, 20); break; } } typedArray.recycle();; mPaint = new Paint(); new Thread(){ @Override public void run() { while (true){ mProgress++; if(mProgress == 360){ mProgress = 0; if(!isNext){ isNext = true; }else { isNext = false; } } postInvalidate(); try{ Thread.sleep(mSpeed); }catch (InterruptedException e){ e.printStackTrace(); } } } }.start(); } @Override protected void onDraw(Canvas canvas) { int center = getWidth()/2; int radius = center-mCircleWidth/2; mPaint.setStrokeWidth(mCircleWidth); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.STROKE); RectF oval = new RectF(center-radius,center-radius,center+radius,center+radius); if(!isNext){ mPaint.setColor(mFirstCoclor); canvas.drawCircle(center,center,radius,mPaint); mPaint.setColor(mSecondColor); canvas.drawArc(oval,-90,mProgress,false,mPaint); }else { mPaint.setColor(mSecondColor); canvas.drawCircle(center,center,radius,mPaint); mPaint.setColor(mFirstCoclor); canvas.drawArc(oval,-90,mProgress,false,mPaint); } }}
在布局中展示
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:custom="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent"><com.example.administrator.testapplication.CustomProgressBar android:layout_width="80dp" android:layout_height="80dp" android:layout_centerInParent="true" custom:circleWidth="10dp" custom:firstColor="#D4F668" custom:secondColor="#2F9DD2" custom:speed="2" ></com.example.administrator.testapplication.CustomProgressBar></RelativeLayout>
运行结果:
0 0
- 自定义ViewProgress
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义
- 自定义TexBox,自定义ComboBox
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义View自定义属性
- 自定义一个自定义对话框
- 自定义控件-自定义吐司
- 自定义checkbox,自定义button
- 自定义控件--自定义对话框
- 自定义控件----自定义属性
- 自定义控件:自定义Toast
- 自定义View自定义属性
- 浮点数的二进制表示
- centos gdb 不可用Missing separate debuginfos, use: debuginfo-install glibc-2.12-1.166.el6_7.3
- iOS开发之网络检测
- hibernate两个实体类映射数据库中的同一张表,保错
- [C#] 操作数据库时检验性别、年龄、邮箱、手机号的输入
- 自定义ViewProgress
- 嵌入式Linux常用命令
- Java程序优化细节
- TIME_WAIT状态
- iOS--蓝牙通讯/blutooth
- Error:SDK location not found. Define location with sdk.dir in the local.properties file or with an A
- tomcat 调优案例
- 【PS4开发】如何通过unity3d发布ps4应用
- 编译内核的保存中间文件