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);//設置字體


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