android 自定义view

来源:互联网 发布:淘宝购物券是什么 编辑:程序博客网 时间:2024/06/01 10:45

看了鸿洋大大的自定义view,自己也写个,有图有真相,先上张图:
这里写图片描述

根据自定义的步骤:
1.自定义view的属性
2.获取view的属性
3.设置view的高和宽
4.重绘view

1、根据上面步骤,自定义的view属性,在values文件夹里创建attr_table.xml,内容:

<declare-styleable name="tableView">    <attr name="divider" format="dimension"/>    <attr name="row" format="integer"/>     <attr name="column" format="integer"/></declare-styleable>

2、获取view的属性:

/**默认行和列分隔线之间距离*/    private final int DEFAULT_DIVIDER = 10;    /**默认行数*/    private final int DEFAULT_ROW = 10;    /**默认列数*/    private final int DEFAULT_COLUMN = 10;    /**自定义属性分隔线距离*/    private int mDivider;    /**自定义属性行数*/    private int mRow;    /**自定义属性列数*/    private int mColumn;    /**画笔*/    private Paint mPaint;    /**每一行的固定起始坐标*/    private LinePosition mRowLine;    /**每一列的固定超始坐标*/    private LinePosition mColumnLine;    /**可移动行的起始坐标*/    private LinePosition mMoveRowLine;    /**可移动列的起始坐标*/    private LinePosition mMoveColumnLine;    /**TableView 的宽*/    private int mWidth;    /**TableView 的高*/    private int mHeight;    /**当前移动的行*/    private int mMoveCurrentRow = 0;    /**当前移动的列*/    private int mMoveCurrentColumn = 0;    public TableView(Context context) {        this(context,null);    }    public TableView(Context context, AttributeSet attrs) {        this(context, attrs,0);    }    public TableView(Context context,                     AttributeSet attrs,                     int defStyleAttr) {        super(context, attrs, defStyleAttr);        initView(context,attrs,defStyleAttr);    }    private void initView(Context context,                           AttributeSet attrs,                           int defStyleAttr) {        TypedArray a = context.obtainStyledAttributes                (attrs,R.styleable.tableView,defStyleAttr,0);        int n = a.getIndexCount();        for (int i = 0; i < n; i++){            int attr = a.getIndex(i);            switch (attr){                case R.styleable.tableView_divider:                    mDivider = a.getDimensionPixelSize(                        attr,                        (int)TypedValue.applyDimension(                        TypedValue.COMPLEX_UNIT_DIP,                        DEFAULT_DIVIDER,                        getResources().getDisplayMetrics()));                    break;                case R.styleable.tableView_row:                    mRow = a.getInt(attr, DEFAULT_ROW);                    break;                case R.styleable.tableView_column:                    mColumn = a.getInt(attr, DEFAULT_COLUMN);                    break;            }        }        a.recycle();        mPaint = new Paint();        mRowLine = new LinePosition();        mColumnLine = new LinePosition();        mMoveRowLine = new LinePosition();        mMoveColumnLine = new LinePosition();    }

类成员都有注译,initView()这个函数主要初始化类成员。

3、onMeasure()计算宽和高

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    mWidth = (mColumn + 1) * mDivider;    mHeight = (mRow + 1) * mDivider;    setMeasuredDimension(mWidth, mHeight);}

4.重绘view

 @Override    protected void onDraw(Canvas canvas) {        mPaint.setAntiAlias(true);        mPaint.setColor(Color.WHITE);        mPaint.setStrokeWidth((float) 1.0);        /**画行*/        for (int i = 0; i < mRow; i++){            setRowPostion(i,mRowLine);            canvas.drawLine(mRowLine.startX,mRowLine.startY,mRowLine.stopX,mRowLine.stopY,mPaint);        }        /**画列*/        for (int i = 0; i < mColumn; i++){            setColumnPostion(i,mColumnLine);            canvas.drawLine(mColumnLine.startX,mColumnLine.startY,mColumnLine.stopX,mColumnLine.stopY,mPaint);        }        /**画可移动的行*/        mPaint.setColor(Color.YELLOW);        mPaint.setStrokeWidth((float) 3.0);        setRowPostion(mMoveCurrentRow,mMoveRowLine);        canvas.drawLine(mMoveRowLine.startX,mMoveRowLine.startY,mMoveRowLine.stopX,mMoveRowLine.stopY,mPaint);        /**画可移动的列*/        setColumnPostion(mMoveCurrentColumn,mMoveColumnLine);        canvas.drawLine(mMoveColumnLine.startX,mMoveColumnLine.startY,mMoveColumnLine.stopX,mMoveColumnLine.stopY,mPaint);    }

5、布局

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:custom="http://schemas.android.com/apk/res-auto"              android:orientation="vertical"              android:layout_width="fill_parent"              android:layout_height="fill_parent"        >        <com.example.CustomValueView.TableView                android:id="@+id/mTableView"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:layout_centerInParent="true"                custom:divider="16dp"                custom:row="15"                custom:column="17"                />        <Button                android:id="@+id/mUpBtn"                android:layout_width="wrap_content"                android:layout_height="wrap_content"                android:background="@drawable/up_selector"                android:layout_above="@id/mTableView"                android:layout_centerHorizontal="true"                />        <Button            android:id="@+id/mDownBtn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/down_selector"            android:layout_below="@id/mTableView"            android:layout_centerHorizontal="true"            />        <Button            android:id="@+id/mLeftBtn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/left_selector"            android:layout_toLeftOf="@id/mTableView"            android:layout_centerVertical="true"            />        <Button            android:id="@+id/mRightBtn"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:background="@drawable/right_selector"            android:layout_toRightOf="@id/mTableView"            android:layout_centerVertical="true"            /></RelativeLayout>

源代码点击下载:http://download.csdn.net/detail/ooppcool/8907205

0 0
原创粉丝点击