关于android自定义view的步骤

关于android自定义view的方法
1.自定义view从哪里开始呢!从哪里开始画?View有几个方法。

其中一个大家都知晓的OnDraw(), 但是,如果想要随时调用随时刷新的的话,用什么呢?如果做过win api开发的话,就会知道inVidate()方法。这个是通知开始重绘的方法,执行了invaidate之后就会重新开始ondraw()。

2.如果需要添加自定义属性。
  对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
  如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
  并根据需要设定默认值,放在在xml文件中没有定义。
  如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
  比如这里是xmlns:my="http://schemas.android.com/apk/res/包名"
  其中xmlns后的“my”是自定义的属性的前缀,res后的是我们应用所在的包

自定义属性的用法如下:
第一步:attrs.xml添加一段你自定义的属性名,还有格式:
  
  <declare-styleable name = "TriangleViewAttr">
        <attr name = "tricolor" format = "color" />
    </declare-styleable>

第二步:xmlns:tri="http://schemas.android.com/apk/res/包名"  这句话需要加在最外层的layout属性中,或者加在view的属性里。

第三步:写进来添加的自定义属性
<demo.view.def.MyView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" 
        tri:tricolor="@color/ios_blue"
        />

第四步:在onDraw方法里,调用:
可以再context.obtainStyledAttributes(attrs, R.styleable.TriangleViewAttr);
我这里重新提出了一个方法:
 private void getAttr(Context context, AttributeSet attrs)
    {
        array = context.obtainStyledAttributes(attrs, R.styleable.TriangleViewAttr);
        maincolor = array.getColor(R.styleable.TriangleViewAttr_tricolor, maincolor); 
        //TriangleViewAttr_tricolor 就是TriangleViewAttr.tricolor
        array.recycle();
    }
 有一句:array.recycle();很重要,记得回收哦!

既然,已经获得指定的属性值。
那么,第五步:
根据你所获得的自定义属性值,进行如下操作吧!


OK!那么,我把我的代码贴上来吧!
1.首先是attr.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>

<!--xmlns:triattr="http://schemas.android.com/apk/res/com.jsdx.zqysypt"-->
<!--triattr:tricolor="@color/mainact_lefttopblue"-->

<declare-styleable name = "TriangleViewAttr">
<attr name = "tricolor" format = "color" />
</declare-styleable>

</resources>

2.TriangleView类的代码:

package com.commons.widget.views;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import com.jsdx.zqysypt.R;

/**
* Created by aa on 2015/1/12.
* 画一个三角形 三个点 1.最左上角 2.最左下角 3.右边的中间点; 如图:
* @*****
* *****@
* @*****
*
* 用法<TriangleView
* xmlns:tri="http://schemas.android.com/apk/res/包名"
* android:layout_width="12dp"
* android:layout_height="12dp"
* tri:tricolor="@color/ios_blue" />
*/
public class TriangleView extends View {
//默认颜色是透明的色彩
int maincolor=Color.TRANSPARENT;
//是否为等边三角
boolean isEquilateral=false;


//tri:tricolor="@color/ios_blue"
//TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
//在使用完成后,一定要调用recycle方法
//属性的名称是styleable中的名称+“_”+属性名称
TypedArray array = null;

public TriangleView(Context context) {
super(context);
}

public TriangleView(Context context, AttributeSet attrs) {
super(context, attrs);
getAttr(context, attrs);
}

public TriangleView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
getAttr(context, attrs);
}

private void getAttr(Context context, AttributeSet attrs)
{
array = context.obtainStyledAttributes(attrs, R.styleable.TriangleViewAttr);
maincolor = array.getColor(R.styleable.TriangleViewAttr_tricolor, maincolor); //TriangleViewAttr_tricolor 就是TriangleViewAttr.tricolor
array.recycle();
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 创建画笔
Paint p = new Paint();
p.setColor(maincolor);// 设置红色
p.setStyle(Paint.Style.FILL);//设置填满

//获得几个点的数值
int width=getWidth();
int height=getHeight();
int loc_x=getLeft();
int loc_y=getTop();
Log.d("TriangleView",width+" "+height);


// 绘制这个三角形,你可以绘制任意多边形
Path path = new Path();
path.moveTo(0, 0);// 此点为多边形的起点

if(isEquilateral)
path.lineTo((height/2)*1.73205f, height/2); ///这里使用*1.73205f 是因为如果要画一个等边三角形的话需要用这个比例根号三一条直角边是另外一条直角边的 1.73205f 倍。
else
path.lineTo(width, height/2);

path.lineTo(0, height);
path.close(); // 使这些点构成封闭的多边形
canvas.drawPath(path, p);

}


/**重新设置颜色
* @param color
* (0xe96f4a)无效 (0xffe96f4a)这样就可以了
*/
public void showColor(int color)
{
maincolor=color;
this.invalidate();
}

}

3.layout布局xml:

<com.commons.widget.views.TriangleView
xmlns:triattr="http://schemas.android.com/apk/res/com.jsdx.zqysypt"
android:layout_width="10dp"
android:layout_height="20dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
triattr:tricolor="@color/mainact_lefttopblue" />