天天看點

android中實作自定義畫線,畫圓,畫矩形,使用自定義字型

首先,建立xml檔案,resource type為drawble,root element為shape

一、自定義畫線

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="line" >
    <stroke android:width="1dp"  //線的粗度
        android:color="#33ccff"  //顔色
        android:dashWidth="2dp"  //虛線的線段長度
        android:dashGap="5dp"/>  //虛線的間隔長度
</shape>      

布局xml檔案中可以使用textview控件,設定背景屬性

二、自定義畫圓

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="oval" >
    <solid android:color="#33ccff"/>
    <size android:width="50dp"  //圓或橢圓
    android:height="50dp"/>
</shape>      

布局xml檔案中使用p_w_picpathview控件

三、自定義畫矩形

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <gradient
        android:angle="45"   //漸變角度  45的整數倍
        android:centerColor="#00ff00"   //漸變顔色
        android:endColor="#0000ff"
        android:startColor="#ff0000" />
    <solid android:color="#33ccff" />   //純色
    <size
        android:height="100dp"
        android:width="50dp" />
    <corners android:radius="10dp" />  //圓角
</shape>      

四、使用自定義字型

把字型格式檔案.ttf,拷貝到assets目錄下,讀取字型檔案Typeface.createFromAsset,設定類型setTypeface

TextView textView = (TextView) findViewById(R.id.textView2);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/samplefont.ttf");//讀取字型
textView.setTypeface(tf);//設定字型