[Android][自定义进度条]①--获取自定义属性

为什么要自定义控件
1.特定的显示风格
2.处理特有的用户交互
3.优化我们的布局
4.封装等。。。

如何自定义控件
1.自定义属性的声明与获取
2.测量onMeasure
3.布局onLayout(ViewGroup)
4.绘制onDraw
5.onTouchEvent
6.onInterceptTouchEvent(ViewGroup)
7.状态的恢复与保存

本例子需用到1 2 4 继承自ProgressBar

[Android][自定义进度条]①--获取自定义属性_第1张图片
Paste_Image.png

attrs.xml



    
    
    
    
    
    
    

    
        
        
        
        
        
        
        
    

HorizontalProgressbarWithProgress.java

package myapplication4.xt.com.myapplication4;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.ProgressBar;

/**
 * Created by TONG on 2017/3/23.
 */

public class HorizontalProgressbarWithProgress extends ProgressBar{

    private static final int DEFAULT_TEXT_SIZE=10;//sp
    private static final int DEFAULT_TEXT_COLOR=0xFFFC00D1;
    private static final int DEFAULT_COLOR_UNREACH=0xFFD3D6DA;
    private static final int DEFAULT_HEIGHT_UNREACH=2;
    private static final int DEFAULT_COLOR_REACH=DEFAULT_TEXT_COLOR;
    private static final int DEFAULT_HEIGHT_REACH=2;//dp
    private static final int DEFAULT_TEXT_OFFSET=10;//dp

    private int mTextSize=sp2px(DEFAULT_TEXT_SIZE);
    private int mTextColor=DEFAULT_TEXT_COLOR;
    private int mUnReachColor=DEFAULT_COLOR_UNREACH;
    private int mUnReachHeight=dp2px(DEFAULT_HEIGHT_UNREACH);
    private int mReachColor=DEFAULT_COLOR_REACH;
    private int mReachHeight=dp2px(DEFAULT_HEIGHT_REACH);
    private int mTextOffset=dp2px(DEFAULT_TEXT_OFFSET);

    private Paint mPaint=new Paint();

    private int mRealWidth;

    public HorizontalProgressbarWithProgress(Context context) {
        this(context,null);
    }

    public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public HorizontalProgressbarWithProgress(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        obtainStyledAttrs(attrs);

    }

    /**
     * 获取自定义属性
     * @param attrs
     */
    private void obtainStyledAttrs(AttributeSet attrs) {
        TypedArray ta=getContext().obtainStyledAttributes
                (attrs,R.styleable.HorizontalProgressbarWithProgress);

        mTextSize= (int) ta.getDimension
                (R.styleable.HorizontalProgressbarWithProgress_progress_text_size,mTextSize);

        mTextColor=ta.getColor
                (R.styleable.HorizontalProgressbarWithProgress_progress_text_color,mTextColor);

        mTextOffset= (int) ta.getDimension
                (R.styleable.HorizontalProgressbarWithProgress_progress_text_offset,mTextOffset);

        mUnReachColor=ta.getColor
                (R.styleable.HorizontalProgressbarWithProgress_progress_unreach_color,mUnReachColor);

        mUnReachHeight= (int) ta.getDimension
                (R.styleable.HorizontalProgressbarWithProgress_progress_unreach_height,mUnReachHeight);

        mReachColor=ta.getColor
                (R.styleable.HorizontalProgressbarWithProgress_progress_reach_color,mReachColor);

        mReachHeight= (int) ta.getDimension
                (R.styleable.HorizontalProgressbarWithProgress_progress_reach_height,mReachHeight);
        
        ta.recycle();
    }

    private int dp2px(int dpval){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dpval,
                getResources().getDisplayMetrics());
    }

    private int sp2px(int spValue){
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,spValue,
                getResources().getDisplayMetrics());
    }
}

你可能感兴趣的:([Android][自定义进度条]①--获取自定义属性)