带icon的textview

 

http://code.google.com/p/ctwo/wiki/IconTextView


实现一个可以在文本前方添加一个图像(可以是任何Android系统支持的图像格式)的TextView组件。在编写代码之前,先看一下Android组件的配置代码。
<TextView android:id="@+id/textview1" android:layout_width="fill_parent"
        android
:layout_height="wrap_content" android:text="textview1" />
上面的代码配置了一个标准的TextView组件。在这段代码中主要有两部分组成:组件标签(<TextView>)和标签属性(android:idandroid:layout_width等)。需要注意的是,在所有的标签属性前面都需要加了一个命名空间(android)。实际上,android命名空间的值是在Android系统中预定义的,所有Android系统原有的组件在配置时都需要在标签属性前加android
   
对于定制组件,可以有如下3种选择。
   
1.  仍然沿用android命名空间。
   
2.  改用其他的命名空间。
   
3.  不使用命名空间。
虽然上面3种选择从技术上说都没有问题,但作者建议使用第2种方式(尤其是对外发布的组件),这是因为在使用定制组件时,可能需要指定相同名称的属性,在这种情况下,可以通过命名空间来区分这些属性,例如,有两个命名空间:androidmobile,这时可以在各自的命名空间下有相同名称的属性,如android:srcmobile:src。在本例中定义了一个mobile命名空间,因此,在配置本例实现的组件时需要在属性前加mobile
   
实现定制组件的一个重要环节就是读取配置文件中相应标签的属性值,由于本例要实现的组件类需要从TextView类继承,因此,只需要覆盖TextView类中带AttributeSet类型参数的构造方法即可,该构造方法的定义如下:
public TextView(Context context, AttributeSet attrs)
在构造方法中可以通过AttributeSet接口的相应getter方法来读取指定的属性值,如果在配置属性时指定了命名空间,需要在使用getter方法获得属性值时指定这个命名空间,如果未指定命名空间,则将命名空间设为null即可。
IconTextView是本例要编写的组件类,该类从TextView继承,在onDraw方法中将TextView中的文本后移,并在文本的前方添加了一个图像,该图像的资源ID通过mobile:iconSrc属性来指定。IconTextView类的代码如下:
package net.blogjava.mobile.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.TextView;

public class IconTextView extends TextView
{
   
//  命名空间的值
   
private final String namespace = "http://net.blogjava.mobile";
   
//  保存图像资源ID的变量
   
private int resourceId = 0;
   
private Bitmap bitmap;
   
public IconTextView(Context context, AttributeSet attrs)
   
{
       
super(context, attrs);
       
//  getAttributeResourceValue方法用来获得组件属性的值,在本例中需要通过该方法的第1个参数指
         
//  定命名空间的值。该方法的第2个参数表示组件属性名(不包括命名空间名称),第3个参数表示默
         
//  认值,也就是如果该属性不存在,则返回第3个参数指定的值
        resourceId
= attrs.getAttributeResourceValue(namespace, "iconSrc", 0);
       
if (resourceId > 0)
             
//  如果成功获得图像资源的ID,装载这个图像资源,并创建Bitmap对象
            bitmap
= BitmapFactory.decodeResource(getResources(), resourceId);
   
}
   
@Override
   
protected void onDraw(Canvas canvas)
   
{
       
if (bitmap != null)
       
{
           
//  从原图上截取图像的区域,在本例中为整个图像
           
Rect src = new Rect();
           
//  将截取的图像复制到bitmap上的目标区域,在本例中与复制区域相同
           
Rect target = new Rect();
            src
.left = 0;
            src
.top = 0;
            src
.right = bitmap.getWidth();
            src
.bottom = bitmap.getHeight();
           
int textHeight = (int) getTextSize();
            target
.left = 0;
           
//  计算图像复制到目标区域的纵坐标。由于TextView组件的文本内容并不是
             
//  从最顶端开始绘制的,因此,需要重新计算绘制图像的纵坐标
            target
.top = (int) ((getMeasuredHeight() - getTextSize()) / 2) + 1;
            target
.bottom = target.top + textHeight;
           
//  为了保证图像不变形,需要根据图像高度重新计算图像的宽度
            target
.right = (int) (textHeight * (bitmap.getWidth() / (float) bitmap.getHeight()));
           
//  开始绘制图像
            canvas
.drawBitmap(bitmap, src, target, getPaint());
           
//  将TextView中的文本向右移动一定的距离(在本例中移动了图像宽度加2个象素点的位置)
            canvas
.translate(target.right + 2, 0);
       
}
       
super.onDraw(canvas);
   
}
}
在编写上面代码时需要注意如下3点:
1.  需要指定命名空间的值。该值将在<LinearLayout>标签的xmlns:mobile属性中定义。
2.  如果在配置组件的属性时指定了命名空间,需要在AttributeSet 接口的相应getter方法中的第1个参数指定命名空间的值,而第2个参数只需指定不带命名空间的属性名即可。
3.  TextView类中的onDraw方法一定要在translate方法后面执行,否则系统不会移动TextView中的文本。
下面在main.xml文件中配置了7IconTextView组件,分别设置了不同的字体大小,同时,文本前面的图像也会随着字体大小的变化而放大或缩小,配置代码如下:
<?xml version="1.0" encoding="utf-8"?>
<!--  在下面的标签中通过xmlns:mobile属性定义了一个命名空间  -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns
:mobile="http://net.blogjava.mobile" android:orientation="vertical"
    android
:layout_width="fill_parent" android:layout_height="fill_parent">
     
<!--  mobile:iconSrc是可选属性,如果未设置该属性,则IconTextViewTextView的效果相同  -->
   
<!-- 由于IconTextViewMain类不在同一个包中,因此,需要显式指定package -->
   
<net.blogjava.mobile.view.IconTextView
        android
:layout_width="fill_parent" android:layout_height="wrap_content"
        android
:text="第一个笑脸" mobile:iconSrc="@drawable/small" />
   
<net.blogjava.mobile.widget.IconTextView
        android
:layout_width="fill_parent" android:layout_height="wrap_content"
        android
:text="第二个笑脸" android:textSize="24dp" mobile:iconSrc="@drawable/small" />
   
<net.blogjava.mobile.widget.IconTextView
        android
:layout_width="fill_parent" android:layout_height="wrap_content"
        android
:text="第三个笑脸" android:textSize="36dp" mobile:iconSrc="@drawable/small" />
   
<net.blogjava.mobile.widget.IconTextView
        android
:layout_width="fill_parent" android:layout_height="wrap_content"
        android
:text="第四个笑脸" android:textSize="48dp" mobile:iconSrc="@drawable/small" />
   
<net.blogjava.mobile.widget.IconTextView
        android
:layout_width="fill_parent" android:layout_height="wrap_content"
        android
:text="第五个笑脸" android:textSize="36dp" mobile:iconSrc="@drawable/small" />
   
<net.blogjava.mobile.widget.IconTextView
        android
:layout_width="fill_parent" android:layout_height="wrap_content"
        android
:text="第六个笑脸" android:textSize="24dp" mobile:iconSrc="@drawable/small" />
   
<net.blogjava.mobile.widget.IconTextView
        android
:layout_width="fill_parent" android:layout_height="wrap_content"
        android
:text="第七个笑脸" mobile:iconSrc="@drawable/small" />
</LinearLayout>  
运行本实例后,将显示如图1所示的效果。

注意:虽然很多人认为组件的属性必须以android命名空间开头,该命名空间的值必须是http://schemas.android.com/apk/res/android。实际上,只是命名空间的值必须是http://schemas.android.com/apk/res/android而已,命名空间的名称可以是任何值,如下面的代码所示:
<?xml version="1.0" encoding="utf-8"?>
<!--  android换成了abcd  -->
<LinearLayout xmlns:abcd="http://schemas.android.com/apk/res/android"
    abcd
:orientation="vertical" abcd:layout_width="fill_parent"
    abcd
:layout_height="fill_parent">
     
</LinearLayout>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值