1.创建一个自定义类组建
这个类是我们自定义view组件的类,它一定是继承了view类或view的子类。而且这个类必须实现一个带参数(Context context和AttributeSet Attributes)的构造方法。
定义自定义Attributes
例如:res/values/attrs.xml
<resources>
<declare-styleable name="">
<attr name="" format="" />
<attr name="" format="">
<enum name="" value=""/>
</attr>
</declare-styleable>
</resources>
它属于http://schemas.android.com/apk/res/[your package name]这个的命名空间。
下面是它的使用方法:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/your_custom_view">
<your_custom_view
custom:your_attrName="value"
/>
</LinearLayout>
运用自定义Attributes
通过调用 obtainStyledAttributes()方法,返回一个TypedArray对象来获取属性。用法举例如下:
public YourCustomViewClassName(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.YourCustomViewClassName,
0, 0);
try {
Your_attrName attr = a.getXXX(R.styleable.YourCustomViewClasst_Your_attrName, 0);
} catch(Exception e){
e.printStackTrace();
}finally {
a.recycle();
}
}
添加属性和事件
我们通过getter和setter方法对来对属性进行修改,例如:
public boolean isShowText() {
return mShowText;
}
public void setShowText(boolean showText) {
mShowText = showText;
invalidate();
requestLayout();
}
注意这里的Setter方法中有个invalidate()方法和requestLayout()方法,他们非常重要,invalidate告知程序重绘,在你改变了属性值,同样,你需要告知程序重绘布局。自定义组建的时间是通过OnCurrentItemChanged()方法告知监听其监听用户的操作。
2.进行自定义绘制
创建绘图对象
安卓将绘图分为Canvas和Paint两部分,我们使用Canvas定义形状,Paint定义颜色,风格,字体等等每个具体的绘制。而且我们是在init()方法中实例化对象并进行初始化,而且要在构造方法中调用创建。这比直接onDraw()方法里既创建又绘制要高效流畅。例如:
private void init(){
Paint mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.setColor(mTextColor);
}
注意:在使用Paint时经常使用ANTI_ALIAS_FLAG标志,以消除绘图时产生的锯齿痕迹,也可以通过Paint.setAntiAlias(true)来设置消除锯齿痕迹,这两者是一样的。
绘制的工作就由onDraw()来完成,例如:
protected void onDraw(Canvas canvas) {
// 绘制文本标签
canvas.drawText(mData.get(mCurrentItem).mLabel, mTextX, mTextY, mTextPaint);
}
处理布局活动
如果你不需要特别控制尺寸大小,你可以直接使用onSizeChanged()方法;如果你需要更好的控制布局的参数,你可以使用onMesure()方法。如果使用onMesure()方法,那么必须使用上setMeasuredDimension()方法,否则运行时会报异常。然后就可以使用onDraw()方法绘图。
3.使组件可交互
利用手势进行事件布局,通过传递一个实现了GestureDetector.OnGestureListener的类来构造一个GestureDetector,或继承GestureDetector.SimpleOnGestureListener,这个类的手势较少。这两个都需要实现onDown()方法,并返回true.例子:
class mListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
}
mDetector = new GestureDetector(PieChart.this.getContext(), new mListener());
note:可以使用ValueAnimator来设置过渡动画。
4.自定义组件在不同设备统一大小
我们通过使密度无关来避免自定义组件在不同屏幕设备显示不同,方法是对每个的参数值都乘密度值。
//获取密度值
private float density = getResources().getDisplayMetrics().density;
canvas.drawCircle(this.getWidth() / 2*density, this.getHeight() / 2*density, this.getWidth()*density, circlePaint);
此外关于绘图的细致操作,强烈推荐阅读博客:http://blog.csdn.net/harvic880925/article/details/39080931