码迷,mamicode.com
首页 > 其他好文 > 详细

2、继承和组合,绘制方式定制控件

时间:2015-08-03 00:58:43      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:

                                                                                              定制控件的方式

1、继承其他控件类。 例如,EditText,是继承TextView.

例子:带图标的文本框

技术分享

public IconTextView(Context context, AttributeSet attrs)
    {
        super(context, attrs);

        TypedArray typedArray = context.obtainStyledAttributes(attrs,
                R.styleable.IconTextView);
        
        resourceId = typedArray.getResourceId(R.styleable.IconTextView_iconSrc,
                0);
        if (resourceId > 0)
            bitmap = BitmapFactory.decodeResource(getResources(), resourceId);
        iconPosition = typedArray.getInt(R.styleable.IconTextView_iconPosition,
                0);
    }

    @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();
            int left = 0;
            if (iconPosition == 1)
            {
                left = (int) getPaint().measureText(getText().toString()) + 2;
            }
            target.left = left;
            // 计算图像复制到目录区域的纵坐标。由于TextView中文本内容并不是从最顶端开始绘制的,因此,需要重新计算绘制图像的纵坐标
            target.top = (int) ((getMeasuredHeight() - getTextSize()) / 2) + 1;
            target.bottom = target.top + textHeight;
            // 为了保证图像不变形,需要根据图像高度重新计算图像的宽度
            target.right = left
                    + (int) (textHeight * (bitmap.getWidth() / (float) bitmap
                            .getHeight()));
            // 开始绘制图像
            canvas.drawBitmap(bitmap, src, target, getPaint());
            // 将TextView中的文本向右移动一定的距离(在本例中移动了图像宽度加2个象素点的位置)
            if (iconPosition == 0)
                canvas.translate(target.right + 2, 0);
        }  
        super.onDraw(canvas);

    }

}
[html] view plaincopy
01.<?xml version="1.0" encoding="utf-8"?>  
02.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
03.    xmlns:mobile="http://cn.eoe.icon.textview" android:orientation="vertical"  
04.    android:layout_width="fill_parent" android:layout_height="fill_parent">  
05.    <cn.eoe.widget.IconTextView  
06.        android:layout_width="fill_parent" android:layout_height="wrap_content"  
07.        android:text="第一个图标" mobile:iconSrc="@drawable/android" />  
08.    <cn.eoe.widget.IconTextView  
09.        android:layout_width="fill_parent" android:layout_height="wrap_content"  
10.        android:text="第二个图标" android:textSize="24dp" mobile:iconSrc="@drawable/android" />  
11.    <cn.eoe.widget.IconTextView  
12.        android:layout_width="fill_parent" android:layout_height="wrap_content"  
13.        android:text="第三个图标" android:textSize="36dp" mobile:iconSrc="@drawable/android" />  
14.    <cn.eoe.widget.IconTextView  
15.        android:layout_width="fill_parent" android:layout_height="wrap_content"  
16.        android:text="第四个图标" android:textSize="48dp" mobile:iconSrc="@drawable/android" />  
17.    <cn.eoe.widget.IconTextView  
18.        android:layout_width="fill_parent" android:layout_height="wrap_content"  
19.        android:text="第五个图标" android:textSize="36dp" mobile:iconSrc="@drawable/android" />  
20.    <cn.eoe.widget.IconTextView  
21.        android:layout_width="fill_parent" android:layout_height="wrap_content"  
22.        android:text="第六个图标" android:textSize="24dp" mobile:iconSrc="@drawable/android" />  
23.    <cn.eoe.widget.IconTextView  
24.        android:layout_width="fill_parent" android:layout_height="wrap_content"  
25.        android:text="第七个图标" mobile:iconSrc="@drawable/android" />  
26.     
27.</LinearLayout>    

 

 

2、组合方式:当前控件类从容器类(布局类,例如linearLayout等)继承,并将若干个控件到当前的容器中。

例子:带标签的文本编辑框:技术分享

 

 

  1. package cn.eoe.widget;  
  2.   
  3. import android.content.Context;  
  4. import android.util.AttributeSet;  
  5. import android.view.LayoutInflater;  
  6. import android.widget.LinearLayout;  
  7. import android.widget.TextView;  
  8. import cn.eoe.label.edittext.R;  
  9.    
  10. public class LabelEditText extends LinearLayout  
  11. {  
  12.     private TextView textView;  
  13.     private String labelText;  
  14.     private int labelFontSize;  
  15.     private String labelPosition;  
  16.   
  17.     public LabelEditText(Context context, AttributeSet attrs)  
  18.     {  
  19.         super(context, attrs);  
  20.         //  读取labelText属性的资源ID  
  21.         int resourceId = attrs.getAttributeResourceValue(null, "labelText", 0);  
  22.         //  未获得资源ID,继续读取属性值  
  23.         if (resourceId == 0)  
  24.             labelText = attrs.getAttributeValue(null, "labelText");  
  25.         //  从资源文件中获得labelText属性的值  
  26.         else  
  27.             labelText = getResources().getString(resourceId);  
  28.         //  如果按两种方式都未获得labelTex属性的值,表示未设置该属性,抛出异常  
  29.         if (labelText == null)  
  30.         {  
  31.             throw new RuntimeException("必须设置labelText属性.");  
  32.         }  
  33.         //  获得labelFontSize属性的资源ID  
  34.         resourceId = attrs.getAttributeResourceValue(null, "labelFontSize", 0);  
  35.         //  继续读取labelFontSize属性的值,如果未设置该属性,将属性值设为14  
  36.         if (resourceId == 0)  
  37.             labelFontSize = attrs.getAttributeIntValue(null, "labelFontSize",  
  38.                     14);  
  39.         //  从资源文件中获得labelFontSize属性的值  
  40.         else  
  41.             labelFontSize = getResources().getInteger(resourceId);  
  42.         //  获得labelPosition属性的资源ID  
  43.         resourceId = attrs.getAttributeResourceValue(null, "labelPosition", 0);  
  44.         //  继续读取labelPosition属性的值  
  45.         if (resourceId == 0)  
  46.             labelPosition = attrs.getAttributeValue(null, "labelPosition");  
  47.         //  从资源文件中获得labelPosition属性的值  
  48.         else  
  49.             labelPosition = getResources().getString(resourceId);  
  50.         //  如果未设置labelPosition属性值,将该属性值设为left  
  51.         if (labelPosition == null)  
  52.             labelPosition = "left";  
  53.           
  54.         String infService = Context.LAYOUT_INFLATER_SERVICE;  
  55.         LayoutInflater li;  
  56.         //  获得LAYOUT_INFLATER_SERVICE服务  
  57.         li = (LayoutInflater) context.getSystemService(infService);  
  58.         LinearLayout linearLayout = null;  
  59.         //  根据labelPosition属性的值装载不同的布局文件  
  60.         if("left".equals(labelPosition))  
  61.             linearLayout = (LinearLayout)li.inflate(R.layout.labeledittext_horizontal, this);  
  62.         else if("top".equals(labelPosition))  
  63.             linearLayout = (LinearLayout)li.inflate(R.layout.labeledittext_vertical, this);  
  64.         else  
  65.             throw new RuntimeException("labelPosition属性的值只能是left或top.");  
  66.           
  67.         //  下面的代码从相应的布局文件中获得了TextView对象,并根据LabelTextView的属性值设置TextView的属性  
  68.         textView = (TextView) findViewById(R.id.textview);  
  69.         //textView.setTextSize((float)labelFontSize);             
  70.         textView.setTextSize(labelFontSize);  
  71.         textView.setText(labelText);  
  72.   
  73.     }  
  74. }  

/labeledittext/res/layout/labeledittext_horizontal.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="horizontal" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:id="@+id/textview" android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
 <EditText android:id="@+id/edittext" android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
</LinearLayout>

/labeledittext/res/layout/labeledittext_vertical.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="fill_parent"
 android:layout_height="fill_parent">
 <TextView android:id="@+id/textview" android:layout_width="wrap_content"
  android:layout_height="wrap_content" />
 <EditText android:id="@+id/edittext" android:layout_width="fill_parent"
  android:layout_height="wrap_content" />
</LinearLayout>

3、绘制控件:也就是控件类从View继承,并在onDraw方法中零绘制控件。例如,TextView.

 

2、继承和组合,绘制方式定制控件

标签:

原文地址:http://www.cnblogs.com/evertriely/p/4697082.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!