Android定製控制項-帶映像的TextView

來源:互聯網
上載者:User

標籤:android   style   blog   http   color   io   os   java   ar   

最近在學習Android,邊看書邊敲代碼,看到是李寧的Android開發完全講義,因為是對著書本敲的,

所以代碼跟原書的代碼基本上是一致的

看到第四章,定製控制項,覺得需要記錄下些東西,所有把代碼寫到部落格作為自己以後查閱的資料。

先上

在src建立net.blogjava.mobile.widget/IconTextView.java

代碼如下:

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);        resourceId = attrs.getAttributeResourceValue(namespace, "iconSrc", 0);        if (resourceId > 0)            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);    }}
IconTextView

Res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?><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">    <net.blogjava.mobile.widget.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>                 
Main.xml

這裡 需要說明是,xmlns:mobile="http://net.blogjava.mobile",這一句話是指定命名空間

 

Android定製控制項-帶映像的TextView

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.