Android自定义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 ?
  1. packagedemo.view.my;
  2. importandroid.content.Context;
  3. importandroid.content.res.TypedArray;
  4. importandroid.graphics.Canvas;
  5. importandroid.graphics.Color;
  6. importandroid.graphics.Paint;
  7. importandroid.graphics.Paint.Style;
  8. importandroid.util.AttributeSet;
  9. importandroid.view.View;
  10. /**
  11. *这个是自定义的TextView.
  12. *至少需要重载构造方法和onDraw方法
  13. *对于自定义的View如果没有自己独特的属性,可以直接在xml文件中使用就可以了
  14. *如果含有自己独特的属性,那么就需要在构造函数中获取属性文件attrs.xml中自定义属性的名称
  15. *并根据需要设定默认值,放在在xml文件中没有定义。
  16. *如果使用自定义属性,那么在应用xml文件中需要加上新的schemas,
  17. *比如这里是xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
  18. *其中xmlns后的“my”是自定义的属性的前缀,res后的是我们自定义View所在的包
  19. *@authorAdministrator
  20. *
  21. */
  22. publicclassMyViewextendsView{
  23. PaintmPaint;//画笔,包含了画几何图形、文本等的样式和颜色信息
  24. publicMyView(Contextcontext){
  25. super(context);
  26. }
  27. publicMyView(Contextcontext,AttributeSetattrs){
  28. super(context,attrs);
  29. mPaint=newPaint();
  30. //TypedArray是一个用来存放由context.obtainStyledAttributes获得的属性的数组
  31. //在使用完成后,一定要调用recycle方法
  32. //属性的名称是styleable中的名称+“_”+属性名称
  33. TypedArrayarray=context.obtainStyledAttributes(attrs,R.styleable.MyView);
  34. inttextColor=array.getColor(R.styleable.MyView_textColor,0XFF00FF00);//提供默认值,放置未指定
  35. floattextSize=array.getDimension(R.styleable.MyView_textSize,36);
  36. mPaint.setColor(textColor);
  37. mPaint.setTextSize(textSize);
  38. array.recycle();//一定要调用,否则这次的设定会对下次的使用造成影响
  39. }
  40. publicvoidonDraw(Canvascanvas){
  41. super.onDraw(canvas);
  42. //Canvas中含有很多画图的接口,利用这些接口,我们可以画出我们想要的图形
  43. //mPaint=newPaint();
  44. //mPaint.setColor(Color.RED);
  45. mPaint.setStyle(Style.FILL);//设置填充
  46. canvas.drawRect(10,10,100,100,mPaint);//绘制矩形
  47. mPaint.setColor(Color.BLUE);
  48. canvas.drawText("我是被画出来的",10,120,mPaint);
  49. }
  50. }

相应的属性文件:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <resources>
  3. <declare-styleablename="MyView">
  4. <attrname="textColor"format="color"/>
  5. <attrname="textSize"format="dimension"/>
  6. </declare-styleable>
  7. </resources>

在布局文件中的使用:

view plain copy to clipboard print ?
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:my="http://schemas.android.com/apk/res/demo.view.my"
  4. android:orientation="vertical"
  5. android:layout_width="fill_parent"
  6. android:layout_height="fill_parent"
  7. >
  8. <demo.view.my.MyView
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. my:textColor="#FFFFFFFF"
  12. my:textSize="22dp"
  13. />
  14. </LinearLayout>

更多相关文章

  1. android混淆(Obfuscate)
  2. Androidz之clickable
  3. Android知识要点整理(2)----- 应用资源
  4. Android(安卓)8.0 Adaptive Icon特性 for Unity适配教程
  5. android 4.0 全屏 屏蔽下面的status bar
  6. Android(安卓)为apk文件签名,增加修改系统时间等权限
  7. Android(安卓)UI美化之Shape Drawable的使用
  8. Andorid常用布局和常用属性介绍
  9. android中自定义service的详解(例子)

随机推荐

  1. mysql字符集浅谈
  2. 反驳"MySQL InnoDB (不行)的性能问题",千
  3. 在同一列上选择多个条件
  4. MYSQL必知必会-SQL语句查询
  5. 《高性能MySQL》学习笔记一
  6. mysql数据版本控制系统的最佳实践
  7. Log4j2记录日志到数据库(MySQL&MongoDB)
  8. php mysql有条件地将行插入到不同的表中
  9. MySQL架构:空列与联接
  10. CentOS 6.4 安装 JAVA + MYSQL + APACHE