相信不少人自定義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就是定義的大小