自定义view


-----------------------vaules/attrs-------------------
<declare-styleable name="ProgressView"
        >
        <!--circleColor 设置圆形边框的颜色  sweepColor设置扇形变换的颜色
         startAngle 设置起始角度 sweepStep 设置变换的步长-->
                 <attr name="circleColor" format="color|reference"></attr>
                 <attr name="sweepColor" format="color|reference"></attr>
                 <attr name="startAngle" format="integer"></attr>
                 <attr name="sweepStep" format="integer"></attr>
                <attr name="padding" format="integer"></attr>
    </declare-styleable>
--------------------------------------------------------------

package com.exbawei.liteli.view;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.util.AttributeSet;import android.view.View;/** * Created by li te li on 2017/10/18. */public class ProgressView extends View { int sweepStep=10; int padding=40; int circleColor= Color.GRAY; int sweepColor=Color.BLUE; int startAngle=90; int storke=10; int sweepAngle=0; int DEFAULT_WIDTH=200; int DEFAULT_HEIGHT = 200; public ProgressView(Context context) { super(context); } public ProgressView(Context context, AttributeSet attrs) { super(context, attrs); TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.ProgressView); if (array != null) { //获取我们在xml中设置的各个自定义属性 sweepStep = array.getInteger(R.styleable.ProgressView_sweepStep, sweepStep); padding = array.getInteger(R.styleable.ProgressView_padding, padding); circleColor = array.getColor(R.styleable.ProgressView_circleColor, circleColor); sweepColor = array.getColor(R.styleable.ProgressView_sweepColor, sweepColor); startAngle = array.getInteger(R.styleable.ProgressView_startAngle, startAngle); //回收TypeArray资源 array.recycle(); } } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Paint mPaint = new Paint(); mPaint.setAntiAlias(true); mPaint.setColor(circleColor); mPaint.setStrokeWidth(storke); mPaint.setStyle(Paint.Style.STROKE); canvas.drawCircle(getWidth()/2,getWidth()/2,getWidth()/2-storke/2,mPaint); invalidate();//请求重新绘制view //绘制内部的扇形 mPaint.setStyle(Paint.Style.FILL_AND_STROKE); mPaint.setColor(sweepColor); RectF rectF = new RectF(padding + storke, padding + storke, getWidth() - padding - storke, getWidth() - padding - storke); canvas.drawArc(rectF, startAngle, sweepAngle, true, mPaint); sweepAngle += sweepStep;//根据步长更新扫过的角度 sweepAngle = sweepAngle > 360 ? 0 : sweepAngle; invalidate();//重绘view } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int wMode = MeasureSpec.getMode(widthMeasureSpec); int hMode = MeasureSpec.getMode(heightMeasureSpec); int wSize = MeasureSpec.getSize(widthMeasureSpec); int hSize = MeasureSpec.getSize(heightMeasureSpec); switch (wMode) { case MeasureSpec.AT_MOST://android:layout_width="warp_content" //获取屏幕像素 float density = getResources().getDisplayMetrics().density; wSize = (int) (DEFAULT_WIDTH * density); hSize = (int) (DEFAULT_HEIGHT * density); break; //当在xml中指定控件的宽高为match_parent或者指定数值的宽高时,回调以下代码 case MeasureSpec.EXACTLY://android:layout_width="match_parent" android:layout_width="40dp" wSize = hSize = Math.min(wSize, hSize); break; } //只要重写onMeasure()方法,一定要调用以下方法,不然会报错 setMeasuredDimension(wSize, hSize); } public ProgressView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); }// public ProgressView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {// super(context, attrs, defStyleAttr, defStyleRes);// }}
---------------------------------------------
<com.exbawei.liteli.view.ProgressView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pv"
    app:circleColor="#aa0000"
   app:sweepColor="#00aa00"
   app:sweepStep="1"
    app:startAngle="180"
    />

自定义view