国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

自定義屬性步驟

2019-11-09 19:02:28
字體:
來源:轉載
供稿:網友

1、values目錄下,創建attrs.xml文件

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="RoundPRogress">        <attr name="roundColor" format="color"/>        <attr name="roundProgressColor" format="color"/>        <attr name="roundWidth" format="dimension"></attr>        <attr name="textColor" format="color"/>        <attr name="textSize" format="dimension"/>    </declare-styleable></resources>

2、在布局文件中使用時,添加命名空間,AS可以直接alt+enter自動生成

然后就可以在布局中使用了

 xmlns:app="http://schemas.android.com/apk/res-auto"

 <cn.wade.RoundProgress                    android:id="@+id/p_progresss"                    android:layout_width="120dp"                    android:layout_height="120dp"                    app:roundColor="@android:color/darker_gray"                    app:roundProgressColor="@android:color/holo_red_dark"                    app:roundWidth="10dp"                    app:textColor="#18b4ed"                    app:textSize="20sp"> </cn.wade.RoundProgress>

3、在自定義view的三個參數的構造方法中使用

public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);        //圓環的顏色        roundColor = mTypedArray.getColor(R.styleable.RoundProgress_roundColor, Color.RED);        //圓環進度的顏色        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN);        //中間進度百分比文字字符串的顏色        textColor = mTypedArray.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);        //中間進度百分比的字符串的字體大小        textSize = mTypedArray.getDimension(R.styleable.RoundProgress_textSize, 15);        //圓環的寬度        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgress_roundWidth, 5);        mTypedArray.recycle();    }

用context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);來獲取自定義的屬性數組,然后根據情況獲取具體的屬性,如定義的時color就通過getColor()獲取

4、附上自定義的一個圓弧進度圈代碼

public class RoundProgress extends View {    private Paint paint = new Paint();    private int roundColor;    private int roundProgressColor;    private int textColor;    private float textSize;    private float roundWidth;    private int max = 100;    private int progress = 50;    public RoundProgress(Context context) {        this(context, null);    }    public RoundProgress(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public RoundProgress(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.RoundProgress);        //圓環的顏色        roundColor = mTypedArray.getColor(R.styleable.RoundProgress_roundColor, Color.RED);        //圓環進度的顏色        roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgress_roundProgressColor, Color.GREEN);        //中間進度百分比文字字符串的顏色        textColor = mTypedArray.getColor(R.styleable.RoundProgress_textColor, Color.GREEN);        //中間進度百分比的字符串的字體大小        textSize = mTypedArray.getDimension(R.styleable.RoundProgress_textSize, 15);        //圓環的寬度        roundWidth = mTypedArray.getDimension(R.styleable.RoundProgress_roundWidth, 5);        mTypedArray.recycle();    }    @Override    protected void onDraw(Canvas canvas) {        //第一步:繪制一個最外層的圓        paint.setColor(roundColor);        paint.setStrokeWidth(roundWidth);        paint.setStyle(Paint.Style.STROKE);        paint.setAntiAlias(true);        int center = getWidth() / 2;        int radius = (int) (center - roundWidth / 2);        canvas.drawCircle(center, center, radius, paint);        //第二步:繪制正中間的文本        float textWidth = paint.measureText(progress + "%");        paint.setColor(textColor);        paint.setTextSize(textSize);        paint.setStrokeWidth(0);        canvas.drawText(progress + "%", center - textWidth / 2, center + textSize / 2, paint);        //第三步:        /**         * 參數解釋:         * oval:繪制弧形圈所包含的矩形范圍輪廓         * 0:開始的角度         * 360 * progress / max:掃描過的角度         * false:是否包含圓心         * paint:繪制弧形時候的畫筆         */        RectF oval = new RectF(center - radius, center - radius, center + radius, center + radius);        paint.setColor(roundProgressColor);        paint.setStrokeWidth(roundWidth);        paint.setStyle(Paint.Style.STROKE);        canvas.drawArc(oval, 0, 360 * progress / max, false, paint);    }    public void setProgress(int progress){        this.progress = progress;        if(progress>100){            this.progress = 100;        }        postInvalidate();    }}


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 德安县| 嘉义市| 静海县| 囊谦县| 和顺县| 襄汾县| 昌乐县| 政和县| 香港| 报价| 敦化市| 镇巴县| 正镶白旗| 南召县| 和平县| 青海省| 仁怀市| 巩义市| 伊宁县| 芜湖县| 湄潭县| 喀喇沁旗| 略阳县| 桃园县| 无极县| 周宁县| 建始县| 信宜市| 聂拉木县| 定西市| 射洪县| 个旧市| 淮滨县| 赣州市| 栾城县| 淅川县| 永兴县| 宜兰县| 靖边县| 威远县| 永川市|