android學習9#--自定義View之繪製過程分析

上一節講了view的繪製過程、瞭解了四個不同個構造函數的調用邏輯。
這一節講我學習view創建時所掌握的知識點。我個人傾向於通過xml來佈局我們的界面,以上一節的構造函數public CustomText(Context context, AttributeSet attrs, int defSytleAttr)爲例。

先來了解Context類,SDK的註釋如下:
Interface to global information about an application environment. This is an abstract class whose implementation is provided by the Android system. It allows access to application-specific resources and classes, as well as up-calls for application-level operations such as launching activities, broadcasting and receiving intents, etc.
總的意思是:context是一個抽象類,描述的是應用程序環境的全局信息,即我們通常說的上下文。通過它我們能獲取到應用程序的資源和類,也包含一些應用級別操作,比如:啓動一個activity,發送廣播,接收intent信息等。

view類的content怎麼來的呢

好,問題來了。view類的content是怎麼來的呢。在開發android時必須的有一個activity,查看SDK,我們發現activity其實是context的子類。因此構造函數CustomText的第一個參數應該是activity傳下來的。

AttributeSet公共接口類

再來看看AttributeSet,他是一個公共接口類,從指定新添加的XML文件中採集特徵、屬性等說明。通常你不想直接使用這個接口,而是用它傳遞資源。通過Theme.obtainstyledattributes()這個方法來解析屬性。

TypedArray類

TypedArray是一個用於存放 obtainStyledAttributes(AttributeSet, int[], int, int)接口或obtainStyledAttributes(AttributeSet, int[])需要注意這裏,官網說是obtainAttributes(AttributeSet, int[]),但SDK裏面並沒有這個方法,而是重載了方法obtainStyledAttributes接口解析出來的各種view屬性的這麼一個數組容器,當操作完成後,一定要調用recycle()方法,釋放數組容器,以後複用。
查看SDK源碼發現函數obtainStyledAttributes(AttributeSet, int[])其實調用的是obtainStyledAttributes(AttributeSet, int[], int, int)。
另外通過此類的成員函數getString()、getColor()等接口獲取對應屬性的value值。
到了這裏我們已經知道如何獲取view的各種屬性了,接下來要到繪製了。

Paint類

paint即畫筆,在繪圖過程中起到了極其重要的作用,畫筆主要保存了顏色,樣式等繪製信息,指定了如何繪製文本和圖形。
paint可以根據我們要畫的類型,選擇不同的筆以及不同的顏色、不同顏料的筆。Paint有三個構造函數。

  • public Paint():Create a new paint with default settings.
  • public Paint(int flags):在構造的時候可以傳入一些定義好的屬性,eg:Paint.ANTI_ALIAS_FLAG --用於繪製時抗鋸齒。可以通過setFlags()來改變屬性。
  • public Paint(Paint paint):使用構造函數中Paint的屬性生成一個新的Paint。

paint類有很多屬性設置方法,比如:

  • setARGB(int a,int r,int g,int b):設置繪製的顏色,a代表透明度,r,g,b代表顏色值。
  • setAlpha(int a):設置繪製圖形的透明度。
  • setColor(int color):設置繪製的顏色,使用顏色值來表示,該顏色值包括透明度和RGB顏色。
  • setTextSize(float textSize):設置繪製文字的字號大小
  • public void setStyle(Style style):設置畫筆的風格,一般都選FILL,但本例選擇STROKE。

等等很多,具體可以參考https://developer.android.com/reference/android/graphics/Paint.html

Canvas類

canvas即畫布,與panit類緊密相連。因爲paint必須在畫布上繪製東西。此類含有很多繪製的工具,比如繪製直線、矩形、點、圓形等等。具體可以參閱:安卓自定義View進階:繪製基本形狀安卓自定義View進階: 畫布操作。還有一個就是[譯] android圖形系統詳解一:Canvas

到目前位置view的繪製基本上講完了,先來看下效果圖。
這裏寫圖片描述
自定義的view源碼見上節: android學習8#–自定義View之view類簡單分析
此activity的xml佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:custom="http://schemas.android.com/apk/res/com.uudou.study.customattr"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity">

    <com.uudou.study.customattr.CustomText
        android:layout_width="180dp"
        android:layout_height="250dp"
        custom:texttitle="北京歡迎你"
        custom:textcolor="#ff0000"
        custom:textsize="30sp"
        />
</RelativeLayout>

對應的源碼:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_custom_text);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章