相信不少人自定义View发现实现构造方法会有几个构造能够选择。。可是怎么选择构造方法才正确才符合需求?下面为你们详解 第一个构造方法参数只有一个参数Context context,这种表明直接在java代码中引用如setContentView(View) 第二个构造方法参数有两个参数Context context, AttributeSet attrs,这种就是你在MainActivity关联中的xml文件中当控件使用 第三个构造方法参数有三个参数Context context, AttributeSet attrs,int defStyleAttr这种就是你既要在xml引用,又要本身定义一些属性
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.View; /** * Created by Lenovo on 2015/11/27. //这是简单的实现的进度加载的效果 */ public class MyView extends View { private int mwidth; private int height; private int progress; private int progressing; //画笔 private Paint paint = new Paint(); //加载的颜色 private int completedColor; //未加载的颜色 private int waitingColor; //字体颜色 private int progressColor; //加载宽度 private int lineWidth; //字体大小 private int ProgessTextSize; public MyView(Context context) { this(context, null); } public MyView(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MyView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); progressing = 5; //TypedArray 本身定义的一些东西 TypedArray type = context.obtainStyledAttributes(attrs, R.styleable.ProgressLineView); lineWidth = (int) type.getDimension(R.styleable.ProgressLineView_linewidth, 1); ProgessTextSize = (int) type.getDimension(R.styleable.ProgressLineView_textSize, 12); progressColor = type.getColor(R.styleable.ProgressLineView_progressColor, 0xff515151); completedColor = type.getColor(R.styleable.ProgressLineView_progressColor, 0xff085cb2); waitingColor = type.getColor(R.styleable.ProgressLineView_waitingColor, 0xffa7f0f9); paint.setStrokeWidth(lineWidth); paint.setTextSize(ProgessTextSize); type.recycle(); } //供外部传入当前进度方法 public void updateProgress(int progress) { this.progress = progress; invalidate(); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); if (changed) { mwidth = right - left; height = bottom - top; } } // @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); String progressinfo = progress + "%"; float[] progressWidth = new float[progressinfo.length()]; paint.getTextWidths(progressinfo, progressWidth); float textToalWidth = caloToalwidth(progressWidth); float cellWidth = (mwidth - (textToalWidth + progress * 2)) / 100; paint.setColor(completedColor); //设置加载了的颜色 canvas.drawLine(0f, height / 2f, cellWidth * progress, height / 2f, paint); paint.setColor(progressColor); //加载多少 canvas.drawText(progressinfo, cellWidth * progress + progressing, (height + ProgessTextSize) / 2, paint); paint.setColor(waitingColor); //未加载的颜色 canvas.drawLine(mwidth - cellWidth * (100 - progress), height / 2f, mwidth, height / 2f, paint); } private float caloToalwidth(float[] arrfolat) { float ress = 0f; for (float f : arrfolat) { ress += f; } return ress; } } //valuse代码==styles <declare-styleable name="ProgressLineView"> <attr name="linewidth" format="dimension"></attr> <attr name="completedcolor" format="color"></attr> <attr name="waitingColor" format="color"></attr> <attr name="progressColor" format="color"></attr> <attr name="textSize" format="dimension"></attr> </declare-styleable> //扩展一些。。在values下的sytyles中咱们能够本身去定义一些东西。。颜色,大小。均可以去定义 而后在代码引用type.getDimension(R.styleable.ProgressLineView_linewidth, 1);后面的1就是定义的大小