Android開發(fā)之自定義View(視圖)用法詳解
本文實(shí)例講述了Android開發(fā)之自定義View(視圖)用法。分享給大家供大家參考,具體如下:
View類是Android的一個(gè)超類,這個(gè)類幾乎包含了所有的屏幕類型。每一個(gè)View都有一個(gè)用于繪圖的畫布,這個(gè)畫布可以進(jìn)行任意擴(kuò)展。在游戲開發(fā)中往往需要自定義視圖(View),這個(gè)畫布的功能更能滿足我們在游戲開發(fā)中的需要。在Android中,任何一個(gè)View類都只需重寫onDraw 方法來實(shí)現(xiàn)界面顯示,自定義的視圖可以是復(fù)雜的3D實(shí)現(xiàn),也可以是非常簡單的文本形式等。
為了實(shí)現(xiàn)自定義View,需要創(chuàng)建一個(gè)新的類,然后重寫onDraw方法,在此需要注意,新創(chuàng)建的類MyView要繼承View基類,同時(shí)還要加入有參數(shù)的兩個(gè)構(gòu)造方法MyView(Context context)和MyView(Contextcontext,AttributeSet attr),否則編譯運(yùn)行無法通過。
在onDraw方法中,初始化了一個(gè)畫筆對象myPaint,設(shè)置畫筆顏色,還有文字大小,填充等屬性。再利用本方法傳入的參數(shù)canvas畫布完成一幅條形統(tǒng)計(jì)圖的繪制。具體代碼如下:
package com.viewTest; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.View; public class MyView extends View { public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } public MyView(Context context,AttributeSet attr) { super(context,attr); } private Paint myPaint; private static final String myString1 = "2006-2011上半年中國移動互聯(lián)網(wǎng)行業(yè)各年度投資情況"; private static final String myString2 = "來源:清科研究中心 2011.08"; @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); myPaint = new Paint(); //繪制標(biāo)題 myPaint.setColor(Color.BLACK); //設(shè)置畫筆顏色 myPaint.setTextSize(18);//設(shè)置文字大小 canvas.drawText(myString1, 20, 20, myPaint); //繪制坐標(biāo)軸 canvas.drawLine(50, 100, 50, 500, myPaint);//縱坐標(biāo)軸 canvas.drawLine(50, 500, 400, 500, myPaint);//橫坐標(biāo)軸 int[] array1 = new int[]{0, 50, 100, 150, 200, 250, 300, 350}; //繪制縱坐標(biāo)刻度 myPaint.setTextSize(10);//設(shè)置文字大小 canvas.drawText("單位:百萬美元", 20, 90, myPaint); for (int i = 0; i < array1.length; i++) { canvas.drawLine(50, 500 - array1[i], 54, 500 - array1[i], myPaint); canvas.drawText(array1[i] + "", 20, 500 - array1[i], myPaint); } //繪制橫坐標(biāo)文字 String[] array2 = new String[]{"2008年", "2009年", "2010年", "2011上半年"}; for (int i = 0; i < array2.length; i++) { canvas.drawText(array2[i], array1[i] + 80, 520, myPaint); } //繪制條形圖 myPaint.setColor(Color.BLUE); //設(shè)置畫筆顏色 myPaint.setStyle(Style.FILL); //設(shè)置填充 canvas.drawRect(new Rect(90, 500 - 56, 110, 500), myPaint);//畫一個(gè)矩形,前兩個(gè)參數(shù)是矩形左上角坐標(biāo),后兩個(gè)參數(shù)是右下角坐標(biāo) canvas.drawRect(new Rect(140, 500 - 98, 160, 500), myPaint);//第二個(gè)矩形 canvas.drawRect(new Rect(190, 500 - 207, 210, 500), myPaint);//第三個(gè)矩形 canvas.drawRect(new Rect(240, 500 - 318, 260, 500), myPaint);//第四個(gè)矩形 myPaint.setColor(Color.BLACK); //設(shè)置畫筆顏色 canvas.drawText("56.32", 88, 500 - 58, myPaint);//第一個(gè)矩形的數(shù)字說明 canvas.drawText("98.00", 138, 500 - 100, myPaint); canvas.drawText("207.65", 188, 500 - 209, myPaint); canvas.drawText("318.30", 238, 500 - 320, myPaint); //繪制出處 myPaint.setColor(Color.BLACK); //設(shè)置畫筆顏色 myPaint.setTextSize(16);//設(shè)置文字大小 canvas.drawText(myString2, 20, 560, myPaint); } }
然后將我們自定義的View 加入到main.xml 布局文件中, 在這里設(shè)置View的背景色為白色,是為了更好地展現(xiàn)其中的內(nèi)容。代碼如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <Button android:layout_width="match_parent" android:layout_height="40dip" android:text="下一張圖" /> <com.viewTest.MyView android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="#FFFFFF"/> </LinearLayout>
初始的activity.Java文件并不需要修改。最后的效果如下圖所示:
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android視圖View技巧總結(jié)》、《Android圖形與圖像處理技巧總結(jié)》、《Android開發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》、《Android布局layout技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計(jì)有所幫助。
相關(guān)文章
Android開發(fā)實(shí)現(xiàn)NFC刷卡讀取的兩種方式
這篇文章主要為大家詳細(xì)介紹了Android開發(fā)中實(shí)現(xiàn)NFC刷卡讀取的兩種方式,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09淺談Android Studio 4.1 更新內(nèi)容
這篇文章主要介紹了淺談Android Studio 4.1 更新內(nèi)容,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-10-10Android屏幕旋轉(zhuǎn) 處理Activity與AsyncTask的最佳解決方案
運(yùn)行時(shí)變更就是設(shè)備在運(yùn)行時(shí)發(fā)生變化(例如屏幕旋轉(zhuǎn)、鍵盤可用性及語言)。發(fā)生這些變化,Android會重啟Activity,這時(shí)就需要保存activity的狀態(tài)及與activity相關(guān)的任務(wù),以便恢復(fù)activity的狀態(tài)。為此,google提供了三種解決方案,本文將對這三種方案進(jìn)行逐一介紹。2016-12-12Android巧用ActionBar實(shí)現(xiàn)下拉式導(dǎo)航
這篇文章主要為大家詳細(xì)介紹了Android巧用ActionBar實(shí)現(xiàn)下拉式導(dǎo)航的相關(guān)資料,具有一定的實(shí)用性和參考價(jià)值,感興趣的小伙伴們可以參考一下2016-05-05Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼
在登錄注冊軟件時(shí),經(jīng)常會要求填寫隨機(jī)驗(yàn)證碼,這篇文章為大家詳細(xì)主要介紹了Android實(shí)現(xiàn)隨機(jī)生成驗(yàn)證碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08解決Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題
對于現(xiàn)在的 App 來說,布局頁面基本都會用到沉浸式狀態(tài)欄,單純的沉浸式狀態(tài)欄很容易解決,但是在華為手機(jī)上存在一個(gè)底部虛擬按鍵的問題,會導(dǎo)致頁面底部和頂部出現(xiàn)很大的問題,下面通過本文給大家分享Android 沉浸式狀態(tài)欄和華為虛擬按鍵沖突問題,一起看看吧2017-07-07Android利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)效果
這篇文章主要為大家詳細(xì)介紹了Android利用RecyclerView實(shí)現(xiàn)列表倒計(jì)時(shí)效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-09-09