Convenient Super-Class for custom view.

自定义View对于新手来说是道坎儿。基本来说有三种自定义View:

  • 继承自View,覆写onDraw方法,绘制需要的内容。
  • 继承自View的子类,如Button、RadioButton等,扩展其功能。
  • 继承自ViewGroup或XxxLayout,inflate布局,在自定义ViewGroup中控制子View。
    对于继承自View的方式,一般需要关心onMeasure,onDraw,onSizeChanged。而onMeasure基本有一套通用的处理方式,下面就来引入一个文件,一方面提供了这个通用模板,另一方面也可以直接使用作为自定义View的基类。

    更多关于SavedState的信息,转到另一篇博客查看。

RayView

也可以直接下载

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.kyleduo.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;

/**
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*
* Convenient class for custom views, providing easy way to control
* width and height for custom views.
*
* Sub-Classes override getMinimumMeasureWidth and getMinimumMeasureHeight
* to provide minimum width and height excluding paddings, the sizes will
* be used for calculate real size for measure.
*
* mWidth and mHeight are protected properties for sub-Classes. If
* you want to maintain some other values related to the size. You can
* override onSizeRealChanged() method to deal with thees staff. This
* method is invoked only the size was REALLY changed in onSizeChanged().
*
* @author kyle
*
* Created by kyle on 15/11/21.
*/

public class RayView extends View {
protected int mWidth, mHeight;

public RayView(Context context) {
super(context);
init(null);
}

public RayView(Context context, AttributeSet attrs) {
super(context, attrs);
init(attrs);
}

public RayView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}

protected void init(AttributeSet attrs) {

}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
if (customOnMeasure()) {
setMeasuredDimension(measure(widthMeasureSpec, true), measure(heightMeasureSpec, false));
} else {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
}

protected int measure(int measureSpec, boolean WOH) {
int size = MeasureSpec.getSize(measureSpec);
int mode = MeasureSpec.getMode(measureSpec);
int measured;

int measureMinimum = WOH ? getMinimumMeasureWidth() : getMinimumMeasureHeight();

measureMinimum = Math.max(measureMinimum, measureMinimum + getPaddingLeft() + getPaddingRight());
measureMinimum = Math.max(measureMinimum, getSuggestedMinimumWidth());

if (mode == MeasureSpec.EXACTLY) {
measured = Math.max(measureMinimum, size);
} else {
measured = measureMinimum;
if (mode == MeasureSpec.AT_MOST) {
measured = Math.min(measured, size);
}
}

return measured;
}

protected int getMinimumMeasureWidth() {
return getSuggestedMinimumWidth();
}

protected int getMinimumMeasureHeight() {
return getSuggestedMinimumHeight();
}

protected boolean customOnMeasure() {
return true;
}

@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
if (w != oldw || h != oldh) {
mWidth = w;
mHeight = h;
onSizeRealChanged(w, h);
}
}

protected void onSizeRealChanged(int w, int h) {

}
}