600字范文,内容丰富有趣,生活中的好帮手!
600字范文 > Android中如何使用自定义view 自定义控件属性及动态自定义控件

Android中如何使用自定义view 自定义控件属性及动态自定义控件

时间:2022-09-03 03:12:03

相关推荐

Android中如何使用自定义view 自定义控件属性及动态自定义控件

如何 自定义View

好处:特殊的效果,满足个性的需求

流程:

1)创建一个类,继承View或它的子类

2)添加构造方法

一个参数:在代码中创建对象

两个参数:在布局文件中使用

3)重写onDraw()方法一个矩形区域,画布Canvas画笔Paint

设置画笔属性

//创建画笔

Paintpaint=newPaint();

paint.setColor(Color.RED);

paint.setAntiAlias(true);

paint.setTextSize(30);

4)使用canvas绘制

例如:canvas.rotate(90);

canvas.drawText("VerticalTextView",20,0,paint);

如何使用自定义控件

在布局文件中拖拽自定义控件

自定义控件属性的步骤:

1)在values目录中创建attrs.xml

(从ApiDemos中拷贝)

2)定义属性

例如:

<declare-styleablename="VerticalTextView">

<attrname="content"format="string"/>

3)在自定义控件代码中读取布局中配置的属性,进行设置

参考ApiDemos中的LabelView

使用自定义属性:

1)在布局文件中添加自定义控件的命名空间

例如:xmlns:yuchen="/apk/res/org.yuchen.templete"

2)配置属性,例如:yuchen:content="sdf"

动态:

1)修改重新绘制的内容

2)触发系统重新调用onDraw()方法

调用invalidate()方法

例如:

view source print ?01.// 动态自定义控件02.postDelayed(newRunnable()03.{04.@Override05.publicvoidrun()06.{07.text =newDate().toLocaleString();08.invalidate();09.postDelayed(this,1000);10.}11.},1000);

运行效果如下:(动态生成)

代码如下:

view source print ?01.publicclassMytextviewextendsView02.{03.String content ="abc";04.privateintcolor;05.privatefloatdimension;06.privatePaint paint;07.publicMytextview(Context context)08.{//一个参数的构造方法,适用在,代码中new自定义的view类09.super(context);10.}11.publicMytextview(Context context, AttributeSet attrs)12.{//两个参数的构造方法,适用在,使用布局xml里拖拉自定义的控件方法13.//14.super(context, attrs);15.Log.e("Mytextview","Mytextview(2)");16.// 读取控件里使用的属性attrs17.TypedArray a = context.obtainStyledAttributes(attrs,18.R.styleable.mytextview);19.CharSequence s = a.getString(R.styleable.mytextview_text);//静态读取自定义控件的文字内容20.color = a.getColor(R.styleable.mytextview_textColor, Color.BLACK);21.dimension = a.getDimension(R.styleable.mytextview_textSize,20);22.// if (s != null) {23.// content = s.toString();24.// }25.postDelayed(newRunnable()26.{27.// 动态获得自定义控件的文字内容28.@Override29.publicvoidrun()30.{31.content =newDate().toLocaleString();32.invalidate();//调用ondraw()方法,重绘33.postDelayed(this,1000);34.}35.},1000);36.}37.@Override38.protectedvoidonDraw(Canvas canvas)39.{//canvas画布40.super.onDraw(canvas);41.Log.e("onDraw","onDraw");42.paint =newPaint();43.paint.setColor(color);44.paint.setTextSize(dimension);45.paint.setAntiAlias(true);//消除锯齿46.canvas.rotate(90);//画布旋转90度47.canvas.drawText(content ,60, -60, paint);//画文本48.}49.}50.publicclassMytextviewextendsView51.{52.String content ="abc";53.privateintcolor;54.privatefloatdimension;55.privatePaint paint;56.publicMytextview(Context context)57.{//一个参数的构造方法,适用在,代码中new自定义的view类58.super(context);59.}60.publicMytextview(Context context, AttributeSet attrs)61.{//两个参数的构造方法,适用在,使用布局xml里拖拉自定义的控件方法62.//63.super(context, attrs);64.Log.e("Mytextview","Mytextview(2)");65.// 读取控件里使用的属性attrs66.TypedArray a = context.obtainStyledAttributes(attrs,67.R.styleable.mytextview);68.CharSequence s = a.getString(R.styleable.mytextview_text);//静态读取自定义控件的文字内容69.color = a.getColor(R.styleable.mytextview_textColor, Color.BLACK);70.dimension = a.getDimension(R.styleable.mytextview_textSize,20);71.// if (s != null) {72.// content = s.toString();73.// }74.postDelayed(newRunnable()75.{76.// 动态获得自定义控件的文字内容77.@Override78.publicvoidrun()79.{80.content =newDate().toLocaleString();81.invalidate();//调用ondraw()方法,重绘82.postDelayed(this,1000);83.}84.},1000);85.}86.@Override87.protectedvoidonDraw(Canvas canvas)88.{89.super.onDraw(canvas);90.paint =newPaint();91.paint.setColor(color);92.paint.setTextSize(dimension);93.paint.setAntiAlias(true);//消除锯齿94.canvas.rotate(90);//画布旋转90度95.canvas.drawText(content ,60, -60, paint);//画文本96.}97.}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。