Android中重溫自定義控件01----文本尺寸的測量,圖片尺寸的測量

一、文本尺寸的測量
效果圖:
在這裏插入圖片描述
實現步驟:
activity_main.xml 佈局中定義控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="5dp" >

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentLeft="true"
            android:gravity="center"
            android:text="字體大小:20sp"
            android:textColor="@color/black"
            android:textSize="17sp"
            />
    </RelativeLayout>

    <TextView
        android:id="@+id/tvShowWidthHeight"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="left"
        android:textColor="@color/black"
        android:textSize="20sp" />

    <TextView
        android:id="@+id/tvText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="每逢佳節倍思親"
        android:textColor="@color/black"
        android:background="@color/red"
        android:layout_gravity="center_horizontal"
        />

</LinearLayout>

MainActivity 中測量

/**
 * 文本尺寸  測量
 */
public class MainActivity extends Activity {
    // 顯示文字的寬高
    private TextView tvShowWidthHeight;
    // 顯示文字
    private TextView tvText;
    // 設置字體大小是 20sp
    private int textSize = 20;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // 初始化控件
        tvShowWidthHeight = findViewById(R.id.tvShowWidthHeight);
        tvText = findViewById(R.id.tvText);


        // 設置文字的大小
        tvText.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize);
        // 拿到字符串
        String text = tvText.getText().toString().trim();


        // 計算獲取指定文本的寬度
        int width = (int) MeasureUtil.getTextWidth(text, textSize);
        // 計算獲取指定文本的高度
        int height = (int) MeasureUtil.getTextHeight(text, textSize);
        // 顯示文本的寬高
        tvShowWidthHeight.setText("文字的寬度是"+width+";"+"文字的高度是"+height);

    }


}

MeasureUtil 工具類中,測量代碼:

 /**
     * 獲取指定文本的寬度(其實就是長度)
     * text 字符串
     * textSize 文字的大小
     *
     */
    public static float getTextWidth(String text, float textSize) {
        if (TextUtils.isEmpty(text)) {
            return 0;
        }
        Paint paint = new Paint(); // 創建一個畫筆對象
        paint.setTextSize(textSize); // 設置畫筆的文本大小
        return paint.measureText(text); // 利用畫筆丈量指定文本的寬度
    }

    /**
     * 獲取指定文本的高度
     * text 字符串
     * textSize 文字的大小
     */
    public static float getTextHeight(String text, float textSize) {
        Paint paint = new Paint(); // 創建一個畫筆對象
        paint.setTextSize(textSize); // 設置畫筆的文本大小
        Paint.FontMetrics fm = paint.getFontMetrics(); // 獲取畫筆默認字體的度量衡
        return fm.descent - fm.ascent; // 返回文本自身的高度
        //return fm.bottom - fm.top + fm.leading;  // 返回文本所在行的行高
    }

二、圖片尺寸的測量
測量高度,直接使用 getWidth()
測量寬度,直接使用 getHeight()

源碼下載:
TestCustom ---- app0
https://download.csdn.net/download/zhaihaohao1/11224806

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章