您的位置:首页 > 移动开发 > Android开发

Android自定义View(一)

2015-05-15 08:52 260 查看
笔者刚刚搞Android开发的时候对自定义View是比较恐惧,那时通常都是去网上找别人的资源修改,当要想成为高手就必须学会自定义View,所以决定花点时间边学变分享。先总结下自定义View的步骤:

1、自定义View的属性

2、在View的构造方法中获得我们自定义的属性

3、[ 重写onMesure ]----计算显示的高和宽

4、重写onDraw
----绘制View
(1) 自定义View的属性,首先在res/values/  下建立一个MyView.xml , 在里面定义我们的属性和声明我们的整个样式。

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="MyView">
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
</declare-styleable>
</resources>
我们定义了字体颜色,字体大小2个属性,format是值该属性的取值格式。

格式有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;

2、在View的构造方法中,获得我们的自定义的样式并初始化 Paint 

 private Paint mPaint; //画笔,包含了画几何图形、文本等的样式和颜色信息   
    private float textSize;
    private int textColor;
    public MyView(Context context) {   
        super(context);   
           
    }   
       
    public MyView(Context context, AttributeSet attrs){   
        super(context, attrs);   
        mPaint = new Paint();   
        //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组   
        //在使用完成后,一定要调用recycle方法   
        //属性的名称是styleable中的名称+“_”+属性名称   
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MyView);   
        textColor = array.getColor(R.styleable.MyView_textColor, 0XFF00FF00); //提供默认值,放置未指定   
        textSize = array.getDimension(R.styleable.MyView_textSize, 36);   
        mPaint.setColor(textColor);   
        mPaint.setTextSize(textSize);   
           
        array.recycle(); //一定要调用,否则这次的设定会对下次的使用造成影响   
    } 
    
3、我们重写onDraw,onMesure方法:

@Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    // TODO 自动生成的方法存根
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
 
    } 
    
    public void onDraw(Canvas canvas){   
        super.onDraw(canvas);   
        //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形   
        //mPaint = new Paint();   
        //mPaint.setColor(Color.RED);   
        mPaint.setStyle(Style.FILL); //设置填充   
        canvas.drawRect(10, 10, 100, 100, mPaint); //绘制矩形   
           
        mPaint.setColor(textColor);   
        canvas.drawText("我是被画出来的", 10, 100+textSize, mPaint);   
    }   
这里重新onMesure方法并未做什么操作

4、使用自定义View

(1)在布局中添加自定义view

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:custom="http://schemas.android.com/apk/res/com.example.myview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

<com.example.myview.MyView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
custom:textColor="#0099cc"
custom:textSize="16dp"
android:text="@string/hello_world" />

</RelativeLayout>


 * 使用自定义属性,那么在应用xml文件中需要加上新的schemas,  

 * 比如这里是 xmlns:custom="http://schemas.android.com/apk/res/com.example.myview"

 * 其中xmlns后的“custom”是自定义的属性的前缀,res后的是我们自定义View所在的包 

附上效果图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息