android进阶-Android控件架构与自定义控件详解(一)

来源:互联网 发布:淘宝描述不符处罚 编辑:程序博客网 时间:2024/06/14 23:47

**

[demo下载地址]

**(http://download.csdn.net/download/qq_20967339/10010439)
这里写图片描述

**

1.Android控件架构

**
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

**

2.View的测量

**
android系统在绘制view前,要知道需要绘制View的大小,就需要进行测量。测量在onMeasure()方法中进行。
这里写图片描述
这里写图片描述
这里写图片描述

public class TeachingView extends View {    public TeachingView(Context context) {        super(context);    }    public TeachingView(Context context, AttributeSet attrs) {        super(context, attrs);    }    public TeachingView(Context context, AttributeSet attrs,                        int defStyleAttr) {        super(context, attrs, defStyleAttr);    }    /**     * 重写onMeasure方法     * @param widthMeasureSpec     * @param heightMeasureSpec     */    @Override    protected void onMeasure(int widthMeasureSpec,                             int heightMeasureSpec) {        setMeasuredDimension(                measureWidth(widthMeasureSpec),                measureHeight(heightMeasureSpec));    }    private int measureWidth(int measureSpec) {        int result = 0;        //第一步:从measureSpec对象中提取出具体的测量模式和大小        int specMode = MeasureSpec.getMode(measureSpec);        int specSize = MeasureSpec.getSize(measureSpec);        //第二步:通过判断测量的模式,给出不同的测量值。        //当specMode为EXACTLY时,直接指定的specSize即可,        //当specMode为其他模式时,需要给它一个默认的大小        if (specMode == MeasureSpec.EXACTLY) {            result = specSize;        } else {            result = 200;            if (specMode == MeasureSpec.AT_MOST) {                result = Math.min(result, specSize);            }        }        return result;    }    private int measureHeight(int measureSpec) {        int result = 0;        int specMode = MeasureSpec.getMode(measureSpec);        int specSize = MeasureSpec.getSize(measureSpec);        if (specMode == MeasureSpec.EXACTLY) {            result = specSize;        } else {            result = 200;            if (specMode == MeasureSpec.AT_MOST) {                result = Math.min(result, specSize);            }        }        return result;    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        canvas.drawColor(Color.GRAY);        int width = getWidth();        int height = getHeight();        Log.d("xys", "width : " + width + " height : " + height);    }}

**

3.View的绘制

**
这里写图片描述
这里写图片描述

**

4.ViewGroup的测量

**
这里写图片描述

**

5.ViewGroup的绘制

**
这里写图片描述

**

6.自定义View

**
这里写图片描述

6.1 对现有控件扩展

public class ShineTextView extends TextView {    private LinearGradient mLinearGradient;    private Matrix mGradientMatrix;    private Paint mPaint;    private int mViewWidth = 0;    private int mTranslate = 0;    public ShineTextView(Context context, AttributeSet attrs) {        super(context, attrs);    }    /**     * 进行一些对象的初始化工作,并根据View的宽设置一个LinearGradient渐变渲染器     * @param w     * @param h     * @param oldw     * @param oldh     */    @Override    protected void onSizeChanged(int w, int h, int oldw, int oldh) {        super.onSizeChanged(w, h, oldw, oldh);        if (mViewWidth == 0) {            mViewWidth = getMeasuredWidth();            if (mViewWidth > 0) {                mPaint = getPaint();                mLinearGradient = new LinearGradient(                        0,                        0,                        mViewWidth,                        0,                        new int[]{                                Color.BLUE, 0xffffffff,                                Color.BLUE},                        null,                        Shader.TileMode.CLAMP);                mPaint.setShader(mLinearGradient);                mGradientMatrix = new Matrix();            }        }    }    /**     * 最关键给Paint对象设置原生TextView没有的LinearGradient属性     * 通过矩阵的方式来不断平移渐变效果     * @param canvas     */    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        if (mGradientMatrix != null) {            mTranslate += mViewWidth / 5;            if (mTranslate > 2 * mViewWidth) {                mTranslate = -mViewWidth;            }            mGradientMatrix.setTranslate(mTranslate, 0);            mLinearGradient.setLocalMatrix(mGradientMatrix);            postInvalidateDelayed(100);        }    }}

**

6.2 创建复合控件

**
6.2.1 定义属性

<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="TopBar">        <attr name="title" format="string" />        <attr name="titleTextSize" format="dimension" />        <attr name="titleTextColor" format="color" />        <attr name="leftTextColor" format="color" />        <attr name="leftBackground" format="reference|color" />        <attr name="leftText" format="string" />        <attr name="rightTextColor" format="color" />        <attr name="rightBackground" format="reference|color" />        <attr name="rightText" format="string" />    </declare-styleable></resources>

这里写图片描述

这里写图片描述

6.2.2 组合控件
这里写图片描述

 // 为组件元素设置相应的布局元素        mLeftParams = new LayoutParams(                LayoutParams.WRAP_CONTENT,                LayoutParams.MATCH_PARENT);        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);        // 添加到ViewGroup        addView(mLeftButton, mLeftParams);        mRightParams = new LayoutParams(                LayoutParams.WRAP_CONTENT,                LayoutParams.MATCH_PARENT);        mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);        addView(mRightButton, mRightParams);        mTitlepParams = new LayoutParams(                LayoutParams.WRAP_CONTENT,                LayoutParams.MATCH_PARENT);        mTitlepParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);        addView(mTitleView, mTitlepParams);

这里写图片描述
这里写图片描述

6.2.3 引用UI模版
这里写图片描述
这里写图片描述

public class TopBar extends RelativeLayout {    // 包含topbar上的元素:左按钮、右按钮、标题    private Button mLeftButton, mRightButton;    private TextView mTitleView;    // 布局属性,用来控制组件元素在ViewGroup中的位置    private LayoutParams mLeftParams, mTitlepParams, mRightParams;    // 左按钮的属性值,即我们在atts.xml文件中定义的属性    private int mLeftTextColor;    private Drawable mLeftBackground;    private String mLeftText;    // 右按钮的属性值,即我们在atts.xml文件中定义的属性    private int mRightTextColor;    private Drawable mRightBackground;    private String mRightText;    // 标题的属性值,即我们在atts.xml文件中定义的属性    private float mTitleTextSize;    private int mTitleTextColor;    private String mTitle;    // 映射传入的接口对象    private topbarClickListener mListener;    public TopBar(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);    }    public TopBar(Context context) {        super(context);    }    public TopBar(Context context, AttributeSet attrs) {        super(context, attrs);        // 设置topbar的背景        setBackgroundColor(0xFFF59563);        // 通过这个方法,将你在atts.xml中定义的declare-styleable        // 的所有属性的值存储到TypedArray中        TypedArray ta = context.obtainStyledAttributes(attrs,                R.styleable.TopBar);        // 从TypedArray中取出对应的值来为要设置的属性赋值        mLeftTextColor = ta.getColor(                R.styleable.TopBar_leftTextColor, 0);        mLeftBackground = ta.getDrawable(                R.styleable.TopBar_leftBackground);        mLeftText = ta.getString(R.styleable.TopBar_leftText);        mRightTextColor = ta.getColor(                R.styleable.TopBar_rightTextColor, 0);        mRightBackground = ta.getDrawable(                R.styleable.TopBar_rightBackground);        mRightText = ta.getString(R.styleable.TopBar_rightText);        mTitleTextSize = ta.getDimension(                R.styleable.TopBar_titleTextSize, 10);        mTitleTextColor = ta.getColor(                R.styleable.TopBar_titleTextColor, 0);        mTitle = ta.getString(R.styleable.TopBar_title);        // 获取完TypedArray的值后,一般要调用        // recyle方法来避免重新创建的时候的错误        ta.recycle();        mLeftButton = new Button(context);        mRightButton = new Button(context);        mTitleView = new TextView(context);        // 为创建的组件元素赋值        // 值就来源于我们在引用的xml文件中给对应属性的赋值        mLeftButton.setTextColor(mLeftTextColor);        mLeftButton.setBackground(mLeftBackground);        mLeftButton.setText(mLeftText);        mRightButton.setTextColor(mRightTextColor);        mRightButton.setBackground(mRightBackground);        mRightButton.setText(mRightText);        mTitleView.setText(mTitle);        mTitleView.setTextColor(mTitleTextColor);        mTitleView.setTextSize(mTitleTextSize);        mTitleView.setGravity(Gravity.CENTER);        // 为组件元素设置相应的布局元素        mLeftParams = new LayoutParams(                LayoutParams.WRAP_CONTENT,                LayoutParams.MATCH_PARENT);        mLeftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, TRUE);        // 添加到ViewGroup        addView(mLeftButton, mLeftParams);        mRightParams = new LayoutParams(                LayoutParams.WRAP_CONTENT,                LayoutParams.MATCH_PARENT);        mRightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, TRUE);        addView(mRightButton, mRightParams);        mTitlepParams = new LayoutParams(                LayoutParams.WRAP_CONTENT,                LayoutParams.MATCH_PARENT);        mTitlepParams.addRule(RelativeLayout.CENTER_IN_PARENT, TRUE);        addView(mTitleView, mTitlepParams);        // 按钮的点击事件,不需要具体的实现,        // 只需调用接口的方法,回调的时候,会有具体的实现        mRightButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                mListener.rightClick();            }        });        mLeftButton.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                mListener.leftClick();            }        });    }    // 暴露一个方法给调用者来注册接口回调    // 通过接口来获得回调者对接口方法的实现    public void setOnTopbarClickListener(topbarClickListener mListener) {        this.mListener = mListener;    }    /**     * 设置按钮的显示与否 通过id区分按钮,flag区分是否显示     *     * @param id   id     * @param flag 是否显示     */    public void setButtonVisable(int id, boolean flag) {        if (flag) {            if (id == 0) {                mLeftButton.setVisibility(View.VISIBLE);            } else {                mRightButton.setVisibility(View.VISIBLE);            }        } else {            if (id == 0) {                mLeftButton.setVisibility(View.GONE);            } else {                mRightButton.setVisibility(View.GONE);            }        }    }    // 接口对象,实现回调机制,在回调方法中    // 通过映射的接口对象调用接口中的方法    // 而不用去考虑如何实现,具体的实现由调用者去创建    public interface topbarClickListener {        // 左按钮点击事件        void leftClick();        // 右按钮点击事件        void rightClick();    }}
public class TopBarTest extends Activity {    private TopBar mTopbar;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.topbar_test);        // 获得我们创建的topbar        mTopbar = (TopBar) findViewById(R.id.topBar);        // 为topbar注册监听事件,传入定义的接口        // 并以匿名类的方式实现接口内的方法        mTopbar.setOnTopbarClickListener(                new TopBar.topbarClickListener() {                    @Override                    public void rightClick() {                        Log.e("xyz","1111111111");                        Toast.makeText(TopBarTest.this,                                "right", Toast.LENGTH_SHORT)                                .show();                    }                    @Override                    public void leftClick() {                        Log.e("xyz","2222222222");                        Toast.makeText(TopBarTest.this,                                "left", Toast.LENGTH_SHORT)                                .show();                    }                });        // 控制topbar上组件的状态        mTopbar.setButtonVisable(0, true);        mTopbar.setButtonVisable(1, true);    }}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:custom="http://schemas.android.com/apk/res-auto"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:padding="5dp"    tools:context=".MainActivity">    <!-- <include layout="@layout/topbar" /> -->    <com.imooc.systemwidget.TopBar        android:id="@+id/topBar"        android:layout_width="match_parent"        android:layout_height="40dp"        custom:leftBackground="@drawable/blue_button"        custom:leftText="Back"        custom:leftTextColor="#FFFFFF"        custom:rightBackground="@drawable/blue_button"        custom:rightText="More"        custom:rightTextColor="#FFFFFF"        custom:title="自定义标题"        custom:titleTextColor="#123412"        custom:titleTextSize="10sp"/></RelativeLayout>
<?xml version="1.0" encoding="utf-8"?><resources>    <declare-styleable name="TopBar">        <attr name="title" format="string" />        <attr name="titleTextSize" format="dimension" />        <attr name="titleTextColor" format="color" />        <attr name="leftTextColor" format="color" />        <attr name="leftBackground" format="reference|color" />        <attr name="leftText" format="string" />        <attr name="rightTextColor" format="color" />        <attr name="rightBackground" format="reference|color" />        <attr name="rightText" format="string" />    </declare-styleable></resources>

这里写图片描述
这里写图片描述

阅读全文
0 0
原创粉丝点击