欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android自定義View詳解

 更新時間:2016年06月07日 10:44:27   作者:鴻洋_  
這篇文章主要為大家詳細(xì)介紹了Android自定義View,幫助大家戰(zhàn)勝Android自定義View,為今后的學(xué)習(xí)打下基礎(chǔ),感興趣的小伙伴們可以參考一下

轉(zhuǎn)載請標(biāo)明出處:http://blog.csdn.net/lmj623565791/article/details/24252901
很多的Android入門程序猿來說對于Android自定義View,可能都是比較恐懼的,但是這又是高手進(jìn)階的必經(jīng)之路,所有準(zhǔn)備在自定義View上面花一些功夫,多寫一些文章。先總結(jié)下自定義View的步驟:
1、自定義View的屬性
2、在View的構(gòu)造方法中獲得我們自定義的屬性
[ 3、重寫onMesure ]
4、重寫onDraw
我把3用[]標(biāo)出了,所以說3不一定是必須的,當(dāng)然了大部分情況下還是需要重寫的。
1、自定義View的屬性,首先在res/values/  下建立一個attrs.xml , 在里面定義我們的屬性和聲明我們的整個樣式。

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 
 <attr name="titleText" format="string" /> 
 <attr name="titleTextColor" format="color" /> 
 <attr name="titleTextSize" format="dimension" /> 
 
 <declare-styleable name="CustomTitleView"> 
  <attr name="titleText" /> 
  <attr name="titleTextColor" /> 
  <attr name="titleTextSize" /> 
 </declare-styleable> 
 
</resources> 

我們定義了字體,字體顏色,字體大小3個屬性,format是值該屬性的取值類型:
一共有:string,color,demension,integer,enum,reference,float,boolean,fraction,flag;不清楚的可以google一把。
然后在布局中聲明我們的自定義View

<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.example.customview01" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
 
 <com.example.customview01.view.CustomTitleView 
  android:layout_width="200dp" 
  android:layout_height="100dp" 
  custom:titleText="3712" 
  custom:titleTextColor="#ff0000" 
  custom:titleTextSize="40sp" /> 
 
</RelativeLayout> 

一定要引入 xmlns:custom="http://schemas.android.com/apk/res/com.example.customview01"我們的命名空間,后面的包路徑指的是項目的package

2、在View的構(gòu)造方法中,獲得我們的自定義的樣式

/** 
  * 文本 
  */ 
 private String mTitleText; 
 /** 
  * 文本的顏色 
  */ 
 private int mTitleTextColor; 
 /** 
  * 文本的大小 
  */ 
 private int mTitleTextSize; 
 
 /** 
  * 繪制時控制文本繪制的范圍 
  */ 
 private Rect mBound; 
 private Paint mPaint; 
 
 public CustomTitleView(Context context, AttributeSet attrs) 
 { 
  this(context, attrs, 0); 
 } 
 
 public CustomTitleView(Context context) 
 { 
  this(context, null); 
 } 
 
 /** 
  * 獲得我自定義的樣式屬性 
  * 
  * @param context 
  * @param attrs 
  * @param defStyle 
  */ 
 public CustomTitleView(Context context, AttributeSet attrs, int defStyle) 
 { 
  super(context, attrs, defStyle); 
  /** 
   * 獲得我們所定義的自定義樣式屬性 
   */ 
  TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.CustomTitleView, defStyle, 0); 
  int n = a.getIndexCount(); 
  for (int i = 0; i < n; i++) 
  { 
   int attr = a.getIndex(i); 
   switch (attr) 
   { 
   case R.styleable.CustomTitleView_titleText: 
    mTitleText = a.getString(attr); 
    break; 
   case R.styleable.CustomTitleView_titleTextColor: 
    // 默認(rèn)顏色設(shè)置為黑色 
    mTitleTextColor = a.getColor(attr, Color.BLACK); 
    break; 
   case R.styleable.CustomTitleView_titleTextSize: 
    // 默認(rèn)設(shè)置為16sp,TypeValue也可以把sp轉(zhuǎn)化為px 
    mTitleTextSize = a.getDimensionPixelSize(attr, (int) TypedValue.applyDimension( 
      TypedValue.COMPLEX_UNIT_SP, 16, getResources().getDisplayMetrics())); 
    break; 
 
   } 
 
  } 
  a.recycle(); 
 
  /** 
   * 獲得繪制文本的寬和高 
   */ 
  mPaint = new Paint(); 
  mPaint.setTextSize(mTitleTextSize); 
  // mPaint.setColor(mTitleTextColor); 
  mBound = new Rect(); 
  mPaint.getTextBounds(mTitleText, 0, mTitleText.length(), mBound); 
 
 } 

我們重寫了3個構(gòu)造方法,默認(rèn)的布局文件調(diào)用的是兩個參數(shù)的構(gòu)造方法,所以記得讓所有的構(gòu)造調(diào)用我們的三個參數(shù)的構(gòu)造,我們在三個參數(shù)的構(gòu)造中獲得自定義屬性。
3、我們重寫onDraw,onMesure調(diào)用系統(tǒng)提供的:

@Override 
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) 
 { 
  super.onMeasure(widthMeasureSpec, heightMeasureSpec); 
 } 
 
 @Override 
 protected void onDraw(Canvas canvas) 
 { 
  mPaint.setColor(Color.YELLOW); 
  canvas.drawRect(0, 0, getMeasuredWidth(), getMeasuredHeight(), mPaint); 
 
  mPaint.setColor(mTitleTextColor); 
  canvas.drawText(mTitleText, getWidth() / 2 - mBound.width() / 2, getHeight() / 2 + mBound.height() / 2, mPaint); 
 } 

此時的效果是:

是不是覺得還不錯,基本已經(jīng)實現(xiàn)了自定義View。但是此時如果我們把布局文件的寬和高寫成wrap_content,會發(fā)現(xiàn)效果并不是我們的預(yù)期:


系統(tǒng)幫我們測量的高度和寬度都是MATCH_PARNET,當(dāng)我們設(shè)置明確的寬度和高度時,系統(tǒng)幫我們測量的結(jié)果就是我們設(shè)置的結(jié)果,當(dāng)我們設(shè)置為WRAP_CONTENT,或者M(jìn)ATCH_PARENT系統(tǒng)幫我們測量的結(jié)果就是MATCH_PARENT的長度。
所以,當(dāng)設(shè)置了WRAP_CONTENT時,我們需要自己進(jìn)行測量,即重寫onMesure方法”:
重寫之前先了解MeasureSpec的specMode,一共三種類型:
EXACTLY:一般是設(shè)置了明確的值或者是MATCH_PARENT
AT_MOST:表示子布局限制在一個最大值內(nèi),一般為WARP_CONTENT
UNSPECIFIED:表示子布局想要多大就多大,很少使用
下面是我們重寫onMeasure代碼:

@Override 
protected void onMeasure(int widthMeasureSpec, int 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 ; 
 if (widthMode == MeasureSpec.EXACTLY) 
 { 
  width = widthSize; 
 } else 
 { 
  mPaint.setTextSize(mTitleTextSize); 
  mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds); 
  float textWidth = mBounds.width(); 
  int desired = (int) (getPaddingLeft() + textWidth + getPaddingRight()); 
  width = desired; 
 } 
 
 if (heightMode == MeasureSpec.EXACTLY) 
 { 
  height = heightSize; 
 } else 
 { 
  mPaint.setTextSize(mTitleTextSize); 
  mPaint.getTextBounds(mTitle, 0, mTitle.length(), mBounds); 
  float textHeight = mBounds.height(); 
  int desired = (int) (getPaddingTop() + textHeight + getPaddingBottom()); 
  height = desired; 
 } 
  
  
 
 setMeasuredDimension(width, height); 
} 

現(xiàn)在我們修改下布局文件:

<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.example.customview01" 
 android:layout_width="match_parent" 
 android:layout_height="match_parent" > 
 
 <com.example.customview01.view.CustomTitleView 
  android:layout_width="wrap_content" 
  android:layout_height="wrap_content" 
  custom:titleText="3712" 
  android:padding="10dp" 
  custom:titleTextColor="#ff0000" 
  android:layout_centerInParent="true" 
  custom:titleTextSize="40sp" /> 
 
</RelativeLayout> 

現(xiàn)在的效果是:


完全復(fù)合我們的預(yù)期,現(xiàn)在我們可以對高度、寬度進(jìn)行隨便的設(shè)置了,基本可以滿足我們的需求。
當(dāng)然了,這樣下來我們這個自定義View與TextView相比豈不是沒什么優(yōu)勢,所有我們覺得給自定義View添加一個事件:
在構(gòu)造中添加:

this.setOnClickListener(new OnClickListener() 
  { 
 
   @Override 
   public void onClick(View v) 
   { 
    mTitleText = randomText(); 
    postInvalidate(); 
   } 
 
  }); 
private String randomText() 
 { 
  Random random = new Random(); 
  Set<Integer> set = new HashSet<Integer>(); 
  while (set.size() < 4) 
  { 
   int randomInt = random.nextInt(10); 
   set.add(randomInt); 
  } 
  StringBuffer sb = new StringBuffer(); 
  for (Integer i : set) 
  { 
   sb.append("" + i); 
  } 
 
  return sb.toString(); 
 } 

下面再來運(yùn)行:


我們添加了一個點(diǎn)擊事件,每次讓它隨機(jī)生成一個4位的隨機(jī)數(shù),有興趣的可以在onDraw中添加一點(diǎn)噪點(diǎn),然后改寫為驗證碼,是不是感覺很不錯。

源碼下載: Android自定義View

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Flutter Http網(wǎng)絡(luò)請求實現(xiàn)詳解

    Flutter Http網(wǎng)絡(luò)請求實現(xiàn)詳解

    這篇文章主要介紹了Flutter Http網(wǎng)絡(luò)請求實現(xiàn)詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • android+json+php+mysql實現(xiàn)用戶反饋功能方法解析

    android+json+php+mysql實現(xiàn)用戶反饋功能方法解析

    相信每個項目都會有用戶反饋建議等功能,這個實現(xiàn)的方法很多,下面是我實現(xiàn)的方法,供大家交流
    2012-11-11
  • 完全解析Android多線程中線程池ThreadPool的原理和使用

    完全解析Android多線程中線程池ThreadPool的原理和使用

    本篇文章給大家通過原理和實例詳細(xì)講述了Android多線程中線程池ThreadPool的原理和使用,對此有興趣的朋友可以跟著參考學(xué)習(xí)下。
    2018-04-04
  • Android應(yīng)用的Material設(shè)計的布局兼容性的一些要點(diǎn)總結(jié)

    Android應(yīng)用的Material設(shè)計的布局兼容性的一些要點(diǎn)總結(jié)

    這篇文章主要介紹了Android應(yīng)用的Material設(shè)計的布局兼容性的一些要點(diǎn)總結(jié),文中還給了一個RecyclerView布局管理的例子,需要的朋友可以參考下
    2016-04-04
  • Android簡單實現(xiàn)無限滾動自動滾動的ViewPager

    Android簡單實現(xiàn)無限滾動自動滾動的ViewPager

    這篇文章主要介紹了Android簡單實現(xiàn)無限滾動自動滾動的ViewPager,百度谷歌上面也有很多關(guān)于這方面的教程,但是感覺都略顯麻煩,而且封裝的都不是很徹底。所以試著封裝一個比較好用的ViewPager,實現(xiàn)思路一起通過本文學(xué)習(xí)吧
    2016-12-12
  • Android?Activity?View加載與繪制流程深入刨析源碼

    Android?Activity?View加載與繪制流程深入刨析源碼

    這篇文章主要介紹了Android?Activity?View的加載與繪制流程源碼分析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2022-08-08
  • Android中Notification用法實例總結(jié)

    Android中Notification用法實例總結(jié)

    這篇文章主要介紹了Android中Notification用法,以實例形式較為詳細(xì)的分析并總結(jié)了Notification的功能與使用技巧,具有一定參考借鑒價值,需要的朋友可以參考下
    2015-10-10
  • Android中自定義View的實現(xiàn)方式總結(jié)大全

    Android中自定義View的實現(xiàn)方式總結(jié)大全

    這篇文章主要總結(jié)了Android中自定義View的實現(xiàn)方式的相關(guān)資料,文中介紹的非常詳細(xì),對各位Android開發(fā)者們學(xué)習(xí)或者使用自定義View具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起看看吧。
    2017-04-04
  • Android SdkVersion的區(qū)別及獲取版本信息方法

    Android SdkVersion的區(qū)別及獲取版本信息方法

    下面小編就為大家?guī)硪黄狝ndroid SdkVersion的區(qū)別及獲取版本信息方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Android控件View打造完美的自定義側(cè)滑菜單

    Android控件View打造完美的自定義側(cè)滑菜單

    這篇文章主要介紹了Android控件View打造完美的自定義側(cè)滑菜單的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-02-02

最新評論