we l23x 0说明书:自定义Android组件之带图像的TextView

来源:百度文库 编辑:偶看新闻 时间:2024/05/04 16:39:43
在本例中要实现一个可以在文本前方添加一个图像(可以是任何Android系统支持的图像格式)的TextView组件。在编写代码之前,先看一下Android组件的配置代码。XML/HTML代码
  1. android:layout_height="wrap_content" android:text="textview1" /> ght="fill_parent">
  2.  LinearLayout> 
    上面的代码配置了一个标准的TextView组件。在这段代码中主要有两部分组成:组件标签()和标签属性(android:id、android:layout_width等)。需要注意的是,在所有的标签属性前面都需要加了一个命名空间(android)。实际上,android命名空间的值是在Android系统中预定义的,所有Android系统原有的组件在配置时都需要在标签属性前加android。 对于定制组件,可以有如下3种选择。
1. 仍然沿用android命名空间。
2. 改用其他的命名空间。
3. 不使用命名空间。
    虽然上面3种选择从技术上说都没有问题,但作者建议使用第2种方式(尤其是对外发布的组件),这是因为在使用定制组件时,可能需要指定相同名称的属性,在这种情况下,可以通过命名空间来区分这些属性,例如,有两个命名空间:android和mobile,这时可以在各自的命名空间下有相同名称的属性,如android:src和mobile:src。在本例中定义了一个mobile命名空间,因此,在配置本例实现的组件时需要在属性前加mobile。
    实现定制组件的一个重要环节就是读取配置文件中相应标签的属性值,由于本例要实现的组件类需要从TextView类继承,因此,只需要覆盖TextView类中带AttributeSet类型参数的构造方法即可,该构造方法的定义如下:
Java代码
  1. public TextView(Context context, AttributeSet attrs)  
    在构造方法中可以通过AttributeSet接口的相应getter方法来读取指定的属性值,如果在配置属性时指定了命名空间,需要在使用getter方法获得属性值时指定这个命名空间,如果未指定命名空间,则将命名空间设为null即可。
    IconTextView是本例要编写的组件类,该类从TextView继承,在onDraw方法中将TextView中的文本后移,并在文本的前方添加了一个图像,该图像的资源ID通过mobile:iconSrc属性来指定。IconTextView类的代码如下:
Java代码
  1. package net.blogjava.mobile.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.BitmapFactory;  
  6. import android.graphics.Canvas;  
  7. import android.graphics.Rect;  
  8. import android.util.AttributeSet;  
  9. import android.widget.TextView;  
  10.   
  11. public class IconTextView extends TextView  
  12. {  
  13.     //  命名空间的值  
  14.     private final String namespace = "http://net.blogjava.mobile";  
  15.     //  保存图像资源ID的变量  
  16.     private int resourceId = 0;  
  17.     private Bitmap bitmap;  
  18.     public IconTextView(Context context, AttributeSet attrs)  
  19.     {  
  20.         super(context, attrs);  
  21.         //  getAttributeResourceValue方法用来获得组件属性的值,在本例中需要通过该方法的第1个参数指  
  22.          //  定命名空间的值。该方法的第2个参数表示组件属性名(不包括命名空间名称),第3个参数表示默  
  23.          //  认值,也就是如果该属性不存在,则返回第3个参数指定的值  
  24.         resourceId = attrs.getAttributeResourceValue(namespace, "iconSrc", 0);  
  25.         if (resourceId > 0)  
  26.               //  如果成功获得图像资源的ID,装载这个图像资源,并创建Bitmap对象  
  27.             bitmap = BitmapFactory.decodeResource(getResources(), resourceId);  
  28.     }  
  29.     @Override  
  30.     protected void onDraw(Canvas canvas)  
  31.     {  
  32.         if (bitmap != null)  
  33.         {  
  34.             //  从原图上截取图像的区域,在本例中为整个图像  
  35.             Rect src = new Rect();  
  36.             //  将截取的图像复制到bitmap上的目标区域,在本例中与复制区域相同  
  37.             Rect target = new Rect();  
  38.             src.left = 0;  
  39.             src.top = 0;  
  40.             src.right = bitmap.getWidth();  
  41.             src.bottom = bitmap.getHeight();  
  42.             int textHeight = (int) getTextSize();  
  43.             target.left = 0;  
  44.             //  计算图像复制到目标区域的纵坐标。由于TextView组件的文本内容并不是  
  45.               //  从最顶端开始绘制的,因此,需要重新计算绘制图像的纵坐标  
  46.             target.top = (int) ((getMeasuredHeight() - getTextSize()) / 2) + 1;  
  47.             target.bottom = target.top + textHeight;  
  48.             //  为了保证图像不变形,需要根据图像高度重新计算图像的宽度  
  49.             target.right = (int) (textHeight * (bitmap.getWidth() / (float) bitmap.getHeight()));  
  50.             //  开始绘制图像  
  51.             canvas.drawBitmap(bitmap, src, target, getPaint());  
  52.             //  将TextView中的文本向右移动一定的距离(在本例中移动了图像宽度加2个象素点的位置)  
  53.             canvas.translate(target.right + 2, 0);  
  54.         }  
  55.         super.onDraw(canvas);  
  56.     }  
  57. }  

    在编写上面代码时需要注意如下3点:
1. 需要指定命名空间的值。该值将在标签的xmlns:mobile属性中定义。
2. 如果在配置组件的属性时指定了命名空间,需要在AttributeSet 接口的相应getter方法中的第1个参数指定命名空间的值,而第2个参数只需指定不带命名空间的属性名即可。
3. TextView类中的onDraw方法一定要在translate方法后面执行,否则系统不会移动TextView中的文本。
    下面在main.xml文件中配置了7个IconTextView组件,分别设置了不同的字体大小,同时,文本前面的图像也会随着字体大小的变化而放大或缩小,配置代码如下:
XML/HTML代码
  1.   
  2.   
  3.     xmlns:mobile="http://net.blogjava.mobile" android:orientation="vertical"  
  4.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  5.        
  6.       
  7.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  8.         android:text="第一个笑脸" mobile:iconSrc="@drawable/small" />  
  9.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  10.         android:text="第二个笑脸" android:textSize="24dp" mobile:iconSrc="@drawable/small" />  
  11.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  12.         android:text="第三个笑脸" android:textSize="36dp" mobile:iconSrc="@drawable/small" />  
  13.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  14.         android:text="第四个笑脸" android:textSize="48dp" mobile:iconSrc="@drawable/small" />  
  15.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  16.         android:text="第五个笑脸" android:textSize="36dp" mobile:iconSrc="@drawable/small" />  
  17.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  18.         android:text="第六个笑脸" android:textSize="24dp" mobile:iconSrc="@drawable/small" />  
  19.             android:layout_width="fill_parent" android:layout_height="wrap_content"  
  20.         android:text="第七个笑脸" mobile:iconSrc="@drawable/small" />  
  21.     
    运行本实例后,将显示如图1所示的效果。
图1 带图像的TextView组件














    注意:虽然很多人认为组件的属性必须以android命名空间开头,该命名空间的值必须是http://schemas.android.com/apk/res/android。实际上,只是命名空间的值必须是http://schemas.android.com/apk/res/android而已,命名空间的名称可以是任何值,如下面的代码所示:
XML/HTML代码
  1.   
  2.   
  3.     abcd:orientation="vertical" abcd:layout_width="fill_parent"  
  4.     abcd:layout_height="fill_parent">  
  5.        
  6.