Android自定义View之一:初探实例
http://blog.csdn.net/duguang77/article/details/19543693
http://blog.csdn.net/duguang77/article/details/19543693
http://blog.csdn.net/duguang77/article/details/19543693
http://blog.csdn.net/duguang77/article/details/19543693
Android自定义View之一:初探实例
分类:Android 2014-02-20 10:42 946人阅读 评论(2) 收藏 举报 安卓自定义控件 android自定义控件 自定义ViewAndroid自定义View实现很简单
继承View,重写构造函数、onDraw,(onMeasure)等函数。
如果自定义的View需要有自定义的属性,需要在values下建立attrs.xml。在其中定义你的属性。
在使用到自定义View的xml布局文件中需要加入xmlns:前缀="http://schemas.android.com/apk/res/你的自定义View所在的包路径".
在使用自定义属性的时候,使用前缀:属性名,如my:textColor="#FFFFFFF"。
实例:
view plain copy to clipboard print ?
- packagedemo.view.my;
- importandroid.content.Context;
- importandroid.content.res.TypedArray;
- importandroid.graphics.Canvas;
- importandroid.graphics.Color;
- importandroid.graphics.Paint;
- importandroid.graphics.Paint.Style;
- importandroid.util.AttributeSet;
- importandroid.view.View;
- /**
- *这个是自定义的TextView.
- *至少需要重载构造方法和onDraw方法
- *对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
- *如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
- *并根据需要设定默认值,放在在xml文件中没有定义。
- *如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
- *比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
- *其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包
- *@authorAdministrator
- *
- */
- publicclassMyViewextendsView{
- PaintmPaint;//画笔,包含了画几何图形、文本等的样式和颜色信息
- publicMyView(Contextcontext){
- super(context);
- }
- publicMyView(Contextcontext,AttributeSetattrs){
- super(context,attrs);
- mPaint=newPaint();
- //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
- //在使用完成后,一定要调用recycle方法
- //属性的名称是styleable中的名称+“_”+属性名称
- TypedArrayarray=context.obtainStyledAttributes(attrs,R.styleable.MyView);
- inttextColor=array.getColor(R.styleable.MyView_textColor,0XFF00FF00);//提供默认值,放置未指定
- floattextSize=array.getDimension(R.styleable.MyView_textSize,36);
- mPaint.setColor(textColor);
- mPaint.setTextSize(textSize);
- array.recycle();//一定要调用,否则这次的设定会对下次的使用造成影响
- }
- publicvoidonDraw(Canvascanvas){
- super.onDraw(canvas);
- //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形
- //mPaint=newPaint();
- //mPaint.setColor(Color.RED);
- mPaint.setStyle(Style.FILL);//设置填充
- canvas.drawRect(10,10,100,100,mPaint);//绘制矩形
- mPaint.setColor(Color.BLUE);
- canvas.drawText("我是被画出来的",10,120,mPaint);
- }
- }
相应的属性文件:
view plain copy to clipboard print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <resources>
- <declare-styleablename="MyView">
- <attrname="textColor"format="color"/>
- <attrname="textSize"format="dimension"/>
- </declare-styleable>
- </resources>
在布局文件中的使用:
view plain copy to clipboard print ?
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <demo.view.my.MyView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- my:textColor="#FFFFFFFF"
- my:textSize="22dp"
- />
- </LinearLayout>
//定义一个矩形
RectF rf1 = new RectF(10, 130, 110, 230);
//画弧顺时针
canvas.drawArc(rf1, 0, 45, true, paint);
//画线
canvas.drawLine(150, 10, 250, 110, paint);
//定义一个矩形
RectF rf2 = new RectF(150, 130, 250, 230);
//画圆
canvas.drawOval(rf2, paint);
更多相关文章
- Android启动过程的Zygote进程
- Android(安卓)RectF类的构造函数参数说明
- Android(安卓)- View的绘制流程一(measure)
- EditText属性详解
- 小记初学android过程中遇到的小问题(android 4.4)
- android:layout_weight的真实含义
- android中文api(89)――ViewManager
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- Python技巧匿名函数、回调函数和高阶函数