Android自定義View多種效果解析
最近項(xiàng)目里涉及到自定義View的東西還是挺多的,所以打算在自定義View上多花點(diǎn)時(shí)間,也順便分享給大家。
先總結(jié)下自定義View的步驟:
1、自定義View的屬性
2、在View的構(gòu)造方法中獲得我們自定義的屬性
[3、重寫onMeasure]
4、重寫onDraw
1.首先在我們的res/values/目錄下建立一個(gè)attrs.xml文件,然后在里面聲明我們我們需要的自定義屬性
我們定義了矩形的顏色,矩形的高度,矩形的寬度3個(gè)屬性,format是指該屬性的取值類型:
一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;
不太明白的可以google一下
然后在布局中聲明我們的自定義View
需要引入xmls:app="http://schemas.android.com/apk/res-auto"這樣就會(huì)自動(dòng)查找我們的自定義屬性,也可以采用另外一種引入方式,xmlns:app="http://schemas.android.com/apk/res/com.example.administrator.demoone.customeview.CustomeRectView"
我們的命名空間,后面的包路徑指的是項(xiàng)目的package
2.在View的構(gòu)造方法里獲取我們的自定義屬性
public class CustomeRectView extends View { private Paint mPiant;//定義畫筆對(duì)象 private int rectColor;//矩形的顏色 private int rectHeight;//矩形的高度 private int rectWidth;//矩形的寬度 public CustomeRectView(Context context) { this(context, null); } public CustomeRectView(Context context, @Nullable AttributeSet attrs) { this(context, attrs, 0); } public CustomeRectView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); /** * 獲取我們自定義的屬性 */ TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.CustomeRectView, defStyleAttr, 0); int arrayCount = array.getIndexCount(); for (int i = 0; i < arrayCount; i++) { int index = array.getIndex(i); switch (index) { case R.styleable.CustomeRectView_rectColor: //getColor(int index,int defaultValue) rectColor=array.getColor(R.styleable.CustomeRectView_rectColor, Color.BLACK); break; case R.styleable.CustomeRectView_rectHeight: /** * 獲取dimension值得時(shí)候會(huì)有3個(gè)不同的方法getDimension()、getDimensionPixelSize()和getDimenPixelOffset() * 結(jié)果值都是將資源文件中定義的dip值乘以屏幕密度,即rectHeight*屏幕密度,只是getDimension()返回的是float, * 其余兩個(gè)返回的是int, 其中g(shù)etDimensionPixelSize()返回的是實(shí)際數(shù)值的四舍五入, * 而getDimensionPixelOffset返回的是實(shí)際數(shù)值去掉后面的小數(shù)點(diǎn); */ rectHeight=array.getDimensionPixelOffset(R.styleable.CustomeRectView_rectHeight,200); break; case R.styleable.CustomeRectView_rectWidth: rectWidth=array.getDimensionPixelOffset(R.styleable.CustomeRectView_rectWidth,200); break; } } array.recycle(); } }
我們重寫了3個(gè)構(gòu)造方法,默認(rèn)的布局文件調(diào)用的是兩個(gè)參數(shù)的構(gòu)造方法,所以記得讓所有的構(gòu)造調(diào)用我們的三個(gè)參數(shù)的構(gòu)造,我們?cè)谌齻€(gè)參數(shù)的構(gòu)造中獲得自定義屬性。
3.重寫OnDraw,調(diào)用系統(tǒng)的onMeasure
@Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPiant=new Paint(); mPiant.setColor(rectColor);//設(shè)置畫筆的顏色 mPiant.setStyle(Paint.Style.FILL);//設(shè)置畫筆的樣式 mPiant.setAntiAlias(true);//去除鋸齒 /** * Draw the specified Rect using the specified paint. The rectangle will * be filled or framed based on the Style in the paint. * * @param left The left side of the rectangle to be drawn * @param top The top side of the rectangle to be drawn * @param right The right side of the rectangle to be drawn * @param bottom The bottom side of the rectangle to be drawn * @param paint The paint used to draw the rect */ canvas.drawRect(0,0,getWidth(),getHeight(),mPiant); } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); }
此時(shí)的效果是:
但是此時(shí)如果我們把布局文件的寬和高寫成wrap_content,去掉我們自定義的寬高屬性
運(yùn)行效果如下
系統(tǒng)幫我們測(cè)量的高度和寬度都是MATCH_PARNET,當(dāng)我們?cè)O(shè)置明確的寬度和高度時(shí),系統(tǒng)幫我們測(cè)量的結(jié)果就是我們?cè)O(shè)置的結(jié)果,當(dāng)我們?cè)O(shè)置為WRAP_CONTENT,或者M(jìn)ATCH_PARENT系統(tǒng)幫我們測(cè)量的結(jié)果就是MATCH_PARENT的長(zhǎng)度。
所以,當(dāng)設(shè)置了WRAP_CONTENT時(shí),我們需要自己進(jìn)行測(cè)量,即重寫onMesure方法”:
重寫之前先了解MeasureSpec的specMode,一共三種類型:
EXACTLY:一般是設(shè)置了明確的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一個(gè)最大值內(nèi),一般為WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用
@Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode=MeasureSpec.getMode(widthMeasureSpec); int widthSize=MeasureSpec.getSize(widthMeasureSpec); int heightMode=MeasureSpec.getMode(heightMeasureSpec); int heightSize=MeasureSpec.getSize(heightMeasureSpec); int width; int height; //如果設(shè)置了明確的大小 if(widthMode==MeasureSpec.EXACTLY){ width=widthSize; }else{ //設(shè)置寬 width=dpToPx(getContext(),200); } if(heightMode==MeasureSpec.EXACTLY){ height=heightSize; }else{ //設(shè)置高 height=dpToPx(getContext(),200); } setMeasuredDimension(width,height); } /** * 根據(jù)手機(jī)的分辨率從 dp 的單位 轉(zhuǎn)成為 px(像素) */ public int dpToPx(Context context, float dpValue){ final float scale=context.getResources().getDisplayMetrics().density; return (int)(dpValue*scale+0.5f); }
此時(shí)的效果如下,就和我們?cè)诓季治募镌O(shè)置的一樣了
好了,后續(xù)還會(huì)陸續(xù)更新。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Kotlin Select協(xié)程多路復(fù)用的實(shí)現(xiàn)詳解
select是Kotlin 1.6中的特性,即選擇最快的結(jié)果。select與async、Channel結(jié)合使用,可以大大提高程序的響應(yīng)速度,還可以提高程序的靈活性、擴(kuò)展性2022-09-09Android程序開發(fā)之Fragment實(shí)現(xiàn)底部導(dǎo)航欄實(shí)例代碼
流行的應(yīng)用的導(dǎo)航一般分為兩種,一種是底部導(dǎo)航,一種是側(cè)邊欄。本文給大家介紹Fragment實(shí)現(xiàn)底部導(dǎo)航欄,對(duì)Fragment實(shí)現(xiàn)底部導(dǎo)航欄相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-03-03Android中應(yīng)用前后臺(tái)切換監(jiān)聽的實(shí)現(xiàn)詳解
這篇文章主要給大家介紹了關(guān)于Android中應(yīng)用前后臺(tái)切換監(jiān)聽實(shí)現(xiàn)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面跟著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-07-07Android實(shí)現(xiàn)簡(jiǎn)易瀏覽器遇到問題的解決方法
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)簡(jiǎn)易瀏覽器遇到的一系列問題的解決方法,感興趣的小伙伴們可以參考一下2016-06-06Android開發(fā)中總結(jié)的Adapter工具類【附完整源碼下載】
這篇文章主要介紹了Android開發(fā)中總結(jié)的Adapter工具類,簡(jiǎn)單說明了Adapter的功能,并結(jié)合實(shí)例形式分析了Adapter工具類的相關(guān)使用方法,并附帶完整源碼供讀者下載參考,需要的朋友可以參考下2017-11-11Android布局技巧之創(chuàng)建可重用的UI組件
這篇文章主要為大家詳細(xì)介紹了Android布局技巧之創(chuàng)建可重用的UI組件,文中提到了include標(biāo)簽的使用方法,感興趣的小伙伴們可以參考一下2016-05-05android實(shí)現(xiàn)人臉識(shí)別技術(shù)的示例代碼
本篇文章主要介紹了android人臉識(shí)別技術(shù)的示例代碼,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-03-03如何在Android中實(shí)現(xiàn)一個(gè)簡(jiǎn)易的Http服務(wù)器
這篇文章主要介紹了如何在Android中實(shí)現(xiàn)一個(gè)簡(jiǎn)易的Http服務(wù)器,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-05-05