Android应用层(View体系)三

来源:互联网 发布:apache官网下载 编辑:程序博客网 时间:2024/05/29 07:53

自定义View

继承系统控件的自定义View

这种自定义View在系统控件的基础上进行拓展,一般是添加新的功能或者修改显示的效果,一般情况下我们在onDraw()方法中进行处理

Step 1 : InvalidTextView.java

public class InvalidTextView extends TextView {    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    public InvalidTextView(Context context) {        super(context);        initDraw();    }    public InvalidTextView(Context context, AttributeSet attrs) {        super(context, attrs);        initDraw();    }    public InvalidTextView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initDraw();    }    private void initDraw() {        mPaint.setColor(Color.RED);        mPaint.setStrokeWidth((float) 1.5);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int width = getWidth();        int height = getHeight();        canvas.drawLine(0, height / 2, width, height / 2, mPaint);    }}

Step 2 :XML中引用自定义View

<com.example.liuwangshu.mooncustomview.InvalidTextView    android:id="@+id/iv_text"    android:layout_width="200dp"    android:layout_height="100dp"    android:background="@android:color/holo_blue_light"    android:gravity="center"    android:textSize="16sp"    android:layout_centerHorizontal="true"    />

继承View的自定义View

Step 1 : RectView.java

public class RectView extends View {    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    private int mColor=Color.RED;    public RectView(Context context) {        super(context);        initDraw();    }    public RectView(Context context, AttributeSet attrs) {        super(context, attrs);        initDraw();    }    public RectView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initDraw();    }    private void initDraw() {        mPaint.setColor(mColor);        mPaint.setStrokeWidth((float) 1.5);    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int width = getWidth();        int height = getHeight();        canvas.drawRect(0, 0, width, height, mPaint);    }}

Step 2 : 布局中引用RectView

<com.example.liuwangshu.mooncustomview.RectView      android:id="@+id/rv_rect"      android:layout_width="200dp"      android:layout_height="200dp"      android:layout_below="@id/iv_text"      android:layout_marginTop="50dp"      android:layout_centerHorizontal="true"/>

Step 3 : 对padding属性进行处理

如果我在布局文件中设置pading属性,发现没有任何的作用,看来还得对padding属性进行处理,只需要在onDraw()方法中稍加修改就可以了,在绘制正方形的时候考虑到padding属性就可以了:

@Override   protected void onDraw(Canvas canvas) {       super.onDraw(canvas);       int paddingLeft=getPaddingLeft();       int paddingRight=getPaddingRight();       int paddingTop=getPaddingTop();       int paddingBottom=getPaddingBottom();       int width = getWidth()-paddingLeft-paddingRight;       int height = getHeight()-paddingTop-paddingBottom;       canvas.drawRect(0+paddingLeft, 0+paddingTop, width+paddingRight, height+paddingBottom, mPaint);   }

修改布局文件加入padding属性:

<com.example.liuwangshu.mooncustomview.RectView    android:id="@+id/rv_rect"    android:layout_width="200dp"    android:layout_height="200dp"    android:layout_below="@id/iv_text"    android:layout_marginTop="50dp"    android:layout_centerHorizontal="true"    android:padding="10dp"/>

Step 4 : 对wrap_content属性进行处理

在onMeasure()方法中指定一个默认的宽和高,在设置wrap_content属性时设置此默认的宽和高就可以了:

@Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {      super.onMeasure(widthMeasureSpec, heightMeasureSpec);      int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);      int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);      int widthSpecSize=MeasureSpec.getSize(widthMeasureSpec);      int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec);      if(widthSpecMode==MeasureSpec.AT_MOST&&heightSpecMode==MeasureSpec.AT_MOST){          setMeasuredDimension(400,400);      }else if(widthSpecMode==MeasureSpec.AT_MOST){          setMeasuredDimension(400,heightSpecSize);      }else if(heightSpecMode==MeasureSpec.AT_MOST){          setMeasuredDimension(widthSpecSize,400);      }  }

Step 5 : 自定义属性

自定义view的完整代码

import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.util.AttributeSet;import android.view.View;public class RectView extends View {    private Paint mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);    private int mColor=Color.RED;    public RectView(Context context) {        super(context);        initDraw();    }    public RectView(Context context, AttributeSet attrs) {        super(context, attrs);        TypedArray mTypedArray=context.obtainStyledAttributes(attrs,R.styleable.RectView);        //提取RectView属性集合的rect_color属性,如果没设置默认值为Color.RED        mColor=mTypedArray.getColor(R.styleable.RectView_rect_color,Color.RED);        //获取资源后要及时回收        mTypedArray.recycle();        initDraw();    }    public RectView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        initDraw();    }    private void initDraw() {        mPaint.setColor(mColor);        mPaint.setStrokeWidth((float) 1.5);    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        super.onMeasure(widthMeasureSpec, heightMeasureSpec);        int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);        int heightSpecMode = MeasureSpec.getMode(heightMeasureSpec);        int widthSpecSize=MeasureSpec.getSize(widthMeasureSpec);        int heightSpecSize=MeasureSpec.getSize(heightMeasureSpec);        if(widthSpecMode==MeasureSpec.AT_MOST&&heightSpecMode==MeasureSpec.AT_MOST){            setMeasuredDimension(400,400);        }else if(widthSpecMode==MeasureSpec.AT_MOST){            setMeasuredDimension(400,heightSpecSize);        }else if(heightSpecMode==MeasureSpec.AT_MOST){            setMeasuredDimension(widthSpecSize,400);        }    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        int paddingLeft = getPaddingLeft();        int paddingRight = getPaddingRight();        int paddingTop = getPaddingTop();        int paddingBottom = getPaddingBottom();        int width = getWidth() - paddingLeft - paddingRight;        int height = getHeight() - paddingTop - paddingBottom;        canvas.drawRect(0 + paddingLeft, 0 + paddingTop, width + paddingRight, height + paddingBottom, mPaint);    }}
原创粉丝点击