Android自定义控件之——文字圆形边框(将文字绘制在圆中间)

来源:互联网 发布:mysql 自动开启事件 编辑:程序博客网 时间:2024/05/16 17:02

自定义的控件写了很多跟圆形有关系的,有时候蛮纠结在圆里面画文字的

有两种思路,一种是画圆之后再画字体,将字体控制在居中的位置

另外一种是:重写TextView,控制TextView的gravity居中,再绘制TextView的边框,重点介绍这种用法。


一、绘制之前,掌握RectF对象的用法

RectF 这个类包含一个矩形的四个单精度浮点坐标。矩形通过上下左右4个边的坐标来表示一个矩形。这些坐标值属性可以被直接访问,用width()和 height()方法可以获取矩形的宽和高。注意:大多数方法不会检查这些坐标分类是否错误(也就是left<=right和top<=bottom)。当长和宽相等的时候就绘制成了正方形,也就是right和bottom相等的时候。

RectF一共有四个构造方法:
RectF()构造一个无参的矩形

RectF(float left,float top,float right,float bottom)构造一个指定了4个参数的矩形

RectF(RectF r)根据指定的RectF对象来构造一个RectF对象(对象的左边坐标不变)

RectF(Rect r)根据给定的Rect对象来构造一个RectF对象

RectF提供了很多方法,下面介绍几个方法:

Public Boolean contain(RectF r);判断一个矩形是否在此矩形内,如果在这个矩形内或者和这个矩形等价则返回true,同样类似的方法还有public Boolean contain(float left,float top,float right,float bottom)和public Boolean contain(float x,float y)。

Public void union(float x,float y)更新这个矩形,使它包含矩形自己和(x,y)这个点。

RectF类提供的方法都比较简单,容易理解,再此就不再一一赘述 


Android.graphics.Rect类,这个类同android.graphics.RectF很相似,不同的地方是Rect类的坐标是用整形表示的,而RectF的坐标是用单精度浮点型表示的。这里大家一定要注意啊。


二、自定义文字圆形边框


1、重写TextView类

public class TextCircleView extends TextView {private Paint mPaint;public TextCircleView(Context context) {super(context);// TODO Auto-generated constructor stub}//xml创建TextCircleView调用这个构造函数public TextCircleView(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubinit();}//new TextCircleView调用这个构造函数public TextCircleView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}/** * 初始化画笔 */public void init(){mPaint = new Paint();}/** * 调用onDraw绘制边框 */@Overrideprotected void onDraw(Canvas canvas) {// TODO Auto-generated method stubsuper.onDraw(canvas);//创建一个RectF,用来限定绘制圆弧的范围RectF rectf = new RectF();//设置画笔的颜色mPaint.setColor(getPaint().getColor());//设置画笔的样式,空心mPaint.setStyle(Paint.Style.STROKE);//设置抗锯齿          mPaint.setAntiAlias(true);          //设置画得一个半径,然后比较长和宽,以最大的值来确定长方形的长宽,确定半径          int r = getMeasuredWidth() > getMeasuredHeight() ? getMeasuredWidth() : getMeasuredHeight();          //如果设置的padding不一样绘制出来的是椭圆形。绘制的时候考虑padding        //Log.i("边界", "宽度"+getMeasuredWidth()+"高度"+getMeasuredHeight()+"getPaddingLeft()"+getPaddingLeft()+"getPaddingTop"+getPaddingTop()+"getPaddingRight(): "+getPaddingRight()+"getPaddingBottom()"+getPaddingBottom());        //当padding都为0的时候,绘制出来的就是RectF限定的区域就是一个正方形        rectf.set(getPaddingLeft(),getPaddingTop(),r-getPaddingRight(),r-getPaddingBottom());          //绘制圆弧        canvas.drawArc(rectf,0,360,false,mPaint);  }}

2、使用自定义的控件,以xml的形式(activity_circletextview.xml)

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <com.txfy.CustomView.TextCircleView        android:layout_width="100dp"        android:layout_height="100dp"        android:gravity="center"        android:paddingLeft="30dp"        android:paddingBottom="50dp"        android:text="圆圈"        android:textColor="@android:color/darker_gray"        android:textSize="20sp" /></LinearLayout>



三、图片效果




0 0
原创粉丝点击