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

Android App開發(fā)中自定義View和ViewGroup的實例教程

 更新時間:2016年05月06日 18:17:28   作者:ALIOUS  
這篇文章主要介紹了Android App開發(fā)中自定義View和ViewGroup的實例教程,分別介紹了進度條和圖片上傳并排列的例子,效果很好很強大,需要的朋友可以參考下

View
Android所有的控件都是View或者View的子類,它其實表示的就是屏幕上的一塊矩形區(qū)域,用一個Rect來表示,left,top表示View相對于它的parent View的起點,width,height表示View自己的寬高,通過這4個字段就能確定View在屏幕上的位置,確定位置后就可以開始繪制View的內(nèi)容了。

View繪制過程
View的繪制可以分為下面三個過程:

Measure
View會先做一次測量,算出自己需要占用多大的面積。View的Measure過程給我們暴露了一個接口onMeasure,方法的定義是這樣的,

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {}

View類已經(jīng)提供了一個基本的onMeasure實現(xiàn),

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 setMeasuredDimension(getDefaultSize(getSuggestedMinimumWidth(), widthMeasureSpec),
     getDefaultSize(getSuggestedMinimumHeight(), heightMeasureSpec));
}
public static int getDefaultSize(int size, int measureSpec) {
 int result = size;
 int specMode = MeasureSpec.getMode(measureSpec);
 int specSize = MeasureSpec.getSize(measureSpec);

 switch (specMode) {
 case MeasureSpec.UNSPECIFIED:
   result = size;
   break;
 case MeasureSpec.AT_MOST:
 case MeasureSpec.EXACTLY:
   result = specSize;
   break;
 }
 return result;
}

其中invoke了setMeasuredDimension()方法,設(shè)置了measure過程中View的寬高,getSuggestedMinimumWidth()返回View的最小Width,Height也有對應(yīng)的方法。插幾句,MeasureSpec類是View類的一個內(nèi)部靜態(tài)類,它定義了三個常量UNSPECIFIED、AT_MOST、EXACTLY,其實我們可以這樣理解它,它們分別對應(yīng)LayoutParams中match_parent、wrap_content、xxxdp。我們可以重寫onMeasure來重新定義View的寬高。

Layout
Layout過程對于View類非常簡單,同樣View給我們暴露了onLayout方法

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
}

因為我們現(xiàn)在討論的是View,沒有子View需要排列,所以這一步其實我們不需要做額外的工作。插一句,對ViewGroup類,onLayout方法中,我們需要將所有子View的大小寬高設(shè)置好,這個我們下一篇會詳細(xì)說。

Draw
Draw過程,就是在canvas上畫出我們需要的View樣式。同樣View給我們暴露了onDraw方法

protected void onDraw(Canvas canvas) {
}

默認(rèn)View類的onDraw沒有一行代碼,但是提供給我們了一張空白的畫布,舉個例子,就像一張畫卷一樣,我們就是畫家,能畫出什么樣的效果,完全取決我們。

View中還有三個比較重要的方法
requestLayout
View重新調(diào)用一次layout過程。

invalidate
View重新調(diào)用一次draw過程

forceLayout
標(biāo)識View在下一次重繪,需要重新調(diào)用layout過程。

自定義屬性
整個View的繪制流程我們已經(jīng)介紹完了,還有一個很重要的知識,自定義控件屬性,我們都知道View已經(jīng)有一些基本的屬性,比如layout_width,layout_height,background等,我們往往需要定義自己的屬性,那么具體可以這么做。

1.在values文件夾下,打開attrs.xml,其實這個文件名稱可以是任意的,寫在這里更規(guī)范一點,表示里面放的全是view的屬性。
2.因為我們下面的實例會用到2個長度,一個顏色值的屬性,所以我們這里先創(chuàng)建3個屬性。

<declare-styleable name="rainbowbar">
 <attr name="rainbowbar_hspace" format="dimension"></attr>
 <attr name="rainbowbar_vspace" format="dimension"></attr>
 <attr name="rainbowbar_color" format="color"></attr>
</declare-styleable>

那么到底怎么用呢,我們會看一個實例。

實現(xiàn)一個比較簡單的Google彩虹進度條。
為了簡單起見,這里我只用一種顏色,多種顏色就留給大家了,我們直接上代碼。

201656180823453.png (814×106)

public class RainbowBar extends View {

 //progress bar color
 int barColor = Color.parseColor("#1E88E5");
 //every bar segment width
 int hSpace = Utils.dpToPx(80, getResources());
 //every bar segment height
 int vSpace = Utils.dpToPx(4, getResources());
 //space among bars
 int space = Utils.dpToPx(10, getResources());
 float startX = 0;
 float delta = 10f;
 Paint mPaint;

 public RainbowBar(Context context) {
  super(context);
 }

 public RainbowBar(Context context, AttributeSet attrs) {
  this(context, attrs, 0);
 }

 public RainbowBar(Context context, AttributeSet attrs, int defStyleAttr) {
  super(context, attrs, defStyleAttr);
  //read custom attrs
  TypedArray t = context.obtainStyledAttributes(attrs,
      R.styleable.rainbowbar, 0, 0);
  hSpace = t.getDimensionPixelSize(R.styleable.rainbowbar_rainbowbar_hspace, hSpace);
  vSpace = t.getDimensionPixelOffset(R.styleable.rainbowbar_rainbowbar_vspace, vSpace);
  barColor = t.getColor(R.styleable.rainbowbar_rainbowbar_color, barColor);
  t.recycle();  // we should always recycle after used
  mPaint = new Paint();
  mPaint.setAntiAlias(true);
  mPaint.setColor(barColor);
  mPaint.setStrokeWidth(vSpace);
 }

 .......
}

View有了三個構(gòu)造方法需要我們重寫,這里介紹下三個方法會被調(diào)用的場景,

第一個方法,一般我們這樣使用時會被調(diào)用,View view = new View(context);
第二個方法,當(dāng)我們在xml布局文件中使用View時,會在inflate布局時被調(diào)用,
<View layout_width="match_parent" layout_height="match_parent"/>。
第三個方法,跟第二種類似,但是增加style屬性設(shè)置,這時inflater布局時會調(diào)用第三個構(gòu)造方法。
<View style="@styles/MyCustomStyle" layout_width="match_parent" layout_height="match_parent"/>。
上面大家可能會感覺到有點困惑的是,我把初始化讀取自定義屬性hspace,vspace,和barcolor的代碼寫在第三個構(gòu)造方法里面,但是我RainbowBar在線性布局中沒有加style屬性(),那按照我們上面的解釋,inflate布局時應(yīng)該會invoke第二個構(gòu)造方法啊,但是我們在第二個構(gòu)造方法里面調(diào)用了第三個構(gòu)造方法,this(context, attrs, 0); 所以在第三個構(gòu)造方法中讀取自定義屬性,沒有問題,這是一點小細(xì)節(jié),避免代碼冗余-,-

Draw
因為我們這里不用關(guān)注measrue和layout過程,直接重寫onDraw方法即可。

 

//draw be invoke numbers.
int index = 0;
@Override
protected void onDraw(Canvas canvas) {
  super.onDraw(canvas);
  //get screen width
  float sw = this.getMeasuredWidth();
  if (startX >= sw + (hSpace + space) - (sw % (hSpace + space))) {
    startX = 0;
  } else {
    startX += delta;
  }
  float start = startX;
  // draw latter parse
  while (start < sw) {
    canvas.drawLine(start, 5, start + hSpace, 5, mPaint);
    start += (hSpace + space);
  }

  start = startX - space - hSpace;

  // draw front parse
  while (start >= -hSpace) {
    canvas.drawLine(start, 5, start + hSpace, 5, mPaint);
    start -= (hSpace + space);
  }
  if (index >= 700000) {
    index = 0;
  }
  invalidate();
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout   xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginTop="40dp"
android:orientation="vertical" >

<com.sw.demo.widget.RainbowBar 
    android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:rainbowbar_color="@android:color/holo_blue_bright"
  app:rainbowbar_hspace="80dp"
  app:rainbowbar_vspace="10dp"
  ></com.sw.demo.widget.RainbowBar>

</LinearLayout>

其實就是調(diào)用canvas的drawLine方法,然后每次將draw的起點向前推進,在方法的結(jié)尾,我們調(diào)用了invalidate方法,上面我們已經(jīng)說明了,這個方法會讓View重新調(diào)用onDraw方法,所以就達到我們的進度條一直在向前繪制的效果。下面是最后的顯示效果,制作成gif時好像有色差,但是真實效果是藍色的。我們只寫了短短的幾十行代碼,自定義View并不是我們想象中那么難,下一篇我們會繼續(xù)ViewGroup的繪制流程學(xué)習(xí)。

201656181022998.gif (404×720)

自定義ViewGroup
ViewGroup
我們知道ViewGroup就是View的容器類,我們經(jīng)常用的LinearLayout,RelativeLayout等都是ViewGroup的子類,因為ViewGroup有很多子View,所以它的整個繪制過程相對于View會復(fù)雜一點,但是還是三個步驟measure,layout,draw,我們一次說明。

Measure
Measure過程還是測量ViewGroup的大小,如果layout_widht和layout_height是match_parent或具體的xxxdp,就很簡答了,直接調(diào)用setMeasuredDimension()方法,設(shè)置ViewGroup的寬高即可,如果是wrap_content,就比較麻煩了,我們需要遍歷所有的子View,然后對每個子View進行測量,然后根據(jù)子View的排列規(guī)則,計算出最終ViewGroup的大小。

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 int childCount = this.getChildCount();
 for (int i = 0; i < childCount; i++) {
   View child = this.getChildAt(i);
   this.measureChild(child, widthMeasureSpec, heightMeasureSpec);
   int cw = child.getMeasuredWidth();
   // int ch = child.getMeasuredHeight();
 }
}

你可能需要類似上面的代碼,其中g(shù)etChildCount()方法,返回子View的數(shù)量,measureChild()方法,調(diào)用子View的測量方法。

Layout
上面View的自定義中,我們稍微提到了,layout過程其實就是對子View的位置進行排列,onLayout方法給我一個機會,來按照我們想要的規(guī)則自定義子View排列。

@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
 int childCount = this.getChildCount();
 for (int i = 0; i < childCount; i++) {
   View child = this.getChildAt(i);
   LayoutParams lParams = (LayoutParams) child.getLayoutParams();
   child.layout(lParams.left, lParams.top, lParams.left + childWidth,
       lParams.top + childHeight);
 }
}

你同樣可能需要類似上面的代碼,其中child.layout(left,top,right,bottom)方法可以對子View的位置進行設(shè)置,四個參數(shù)的意思大家通過變量名都應(yīng)該清楚了。
Draw
ViewGroup在draw階段,其實就是按照子類的排列順序,調(diào)用子類的onDraw方法,因為我們只是View的容器, 本身一般不需要draw額外的修飾,所以往往在onDraw方法里面,只需要調(diào)用ViewGroup的onDraw默認(rèn)實現(xiàn)方法即可。

LayoutParams
ViewGroup還有一個很重要的知識LayoutParams,LayoutParams存儲了子View在加入ViewGroup中時的一些參數(shù)信息,在繼承ViewGroup類時,一般也需要新建一個新的LayoutParams類,就像SDK中我們熟悉的LinearLayout.LayoutParams,RelativeLayout.LayoutParams類等一樣,那么可以這樣做,在你定義的ViewGroup子類中,新建一個LayoutParams類繼承與ViewGroup.LayoutParams。

public static class LayoutParams extends ViewGroup.LayoutParams {

 public int left = 0;
 public int top = 0;

 public LayoutParams(Context arg0, AttributeSet arg1) {
   super(arg0, arg1);
 }

 public LayoutParams(int arg0, int arg1) {
   super(arg0, arg1);
 }

 public LayoutParams(android.view.ViewGroup.LayoutParams arg0) {
   super(arg0);
 }

}

那么現(xiàn)在新的LayoutParams類已經(jīng)有了,如何讓我們自定義的ViewGroup使用我們自定義的LayoutParams類來添加子View呢,ViewGroup同樣提供了下面這幾個方法供我們重寫,我們重寫返回我們自定義的LayoutParams對象即可。

@Override
public android.view.ViewGroup.LayoutParams generateLayoutParams(
   AttributeSet attrs) {
 return new NinePhotoView.LayoutParams(getContext(), attrs);
}

@Override
protected android.view.ViewGroup.LayoutParams generateDefaultLayoutParams() {
 return new LayoutParams(LayoutParams.WRAP_CONTENT,
     LayoutParams.WRAP_CONTENT);
}

@Override
protected android.view.ViewGroup.LayoutParams generateLayoutParams(
   android.view.ViewGroup.LayoutParams p) {
 return new LayoutParams(p);
}

@Override
protected boolean checkLayoutParams(android.view.ViewGroup.LayoutParams p) {
 return p instanceof NinePhotoView.LayoutParams;
}

實例
我們還是做一個實例來說明,我們今天做一個類似微信朋友圈 存儲要發(fā)送圖片的控件,點擊+號圖片,可以一直加圖片,最多9張。那么微信是4個一排,我們這里是3個一排,因為一般常規(guī)都是三個一排,這些都是細(xì)節(jié)不要在意(另外偷偷告訴大家,微信的實現(xiàn)是用TableLayout,-.-)。

201656181132418.png (632×294)

public class NinePhotoView extends ViewGroup {

public static final int MAX_PHOTO_NUMBER = 9;

private int[] constImageIds = { R.drawable.girl_0, R.drawable.girl_1,
   R.drawable.girl_2, R.drawable.girl_3, R.drawable.girl_4,
   R.drawable.girl_5, R.drawable.girl_6, R.drawable.girl_7,
   R.drawable.girl_8 };

// horizontal space among children views
int hSpace = Utils.dpToPx(10, getResources());
// vertical space among children views
int vSpace = Utils.dpToPx(10, getResources());

// every child view width and height.
int childWidth = 0;
int childHeight = 0;

// store images res id
ArrayList<integer> mImageResArrayList = new ArrayList<integer>(9);
private View addPhotoView;

public NinePhotoView(Context context) {
 super(context);
}

public NinePhotoView(Context context, AttributeSet attrs) {
 this(context, attrs, 0);
}

public NinePhotoView(Context context, AttributeSet attrs, int defStyle) {
 super(context, attrs, defStyle);

 TypedArray t = context.obtainStyledAttributes(attrs,
     R.styleable.NinePhotoView, 0, 0);
 hSpace = t.getDimensionPixelSize(
     R.styleable.NinePhotoView_ninephoto_hspace, hSpace);
 vSpace = t.getDimensionPixelSize(
     R.styleable.NinePhotoView_ninephoto_vspace, vSpace);
 t.recycle();

 addPhotoView = new View(context);
 addView(addPhotoView);
 mImageResArrayList.add(new integer());
}

目前為止,都跟上一篇說的大致差不多,另外拍照和從相冊選擇圖片不是我們這一篇的重點,所以我們把圖片硬編碼到代碼中(全是美女...),ViewGroup初始化時我們添加了一個+號按鈕,給用戶點擊添加新的圖片。

Measure

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
 int rw = MeasureSpec.getSize(widthMeasureSpec);
 int rh = MeasureSpec.getSize(heightMeasureSpec);

 childWidth = (rw - 2 * hSpace) / 3;
 childHeight = childWidth;

 int childCount = this.getChildCount();
 for (int i = 0; i < childCount; i++) {
   View child = this.getChildAt(i);
   //this.measureChild(child, widthMeasureSpec, heightMeasureSpec);

   LayoutParams lParams = (LayoutParams) child.getLayoutParams();
   lParams.left = (i % 3) * (childWidth + hSpace);
   lParams.top = (i / 3) * (childWidth + vSpace);
 }

 int vw = rw;
 int vh = rh;
 if (childCount < 3) {
   vw = childCount * (childWidth + hSpace);
 }
 vh = ((childCount + 3) / 3) * (childWidth + vSpace);
 setMeasuredDimension(vw, vh);
}

我們的子View三個一排,而且都是正方形,所以我們上面通過循環(huán)很好去得到所有子View的位置,注意我們上面把子View的左上角坐標(biāo)存儲到我們自定義的LayoutParams 的left和top二個字段中,Layout階段會使用,最后我們算得整個ViewGroup的寬高,調(diào)用setMeasuredDimension設(shè)置。

Layout

@Override
protected void onLayout(boolean arg0, int arg1, int arg2, int arg3, int arg4) {
 int childCount = this.getChildCount();
 for (int i = 0; i < childCount; i++) {
   View child = this.getChildAt(i);
   LayoutParams lParams = (LayoutParams) child.getLayoutParams();
   child.layout(lParams.left, lParams.top, lParams.left + childWidth,
       lParams.top + childHeight);

   if (i == mImageResArrayList.size() - 1 && mImageResArrayList.size() != MAX_PHOTO_NUMBER) {
     child.setBackgroundResource(R.drawable.add_photo);
     child.setOnClickListener(new View.OnClickListener() {

       @Override
       public void onClick(View arg0) {
         addPhotoBtnClick();
       }
     });
   }else {
     child.setBackgroundResource(constImageIds[i]);
     child.setOnClickListener(null);
   }
 }
}

public void addPhoto() {
 if (mImageResArrayList.size() < MAX_PHOTO_NUMBER) {
   View newChild = new View(getContext());
   addView(newChild);
   mImageResArrayList.add(new integer());
   requestLayout();
   invalidate();
 }
}

public void addPhotoBtnClick() {
 final CharSequence[] items = { "Take Photo", "Photo from gallery" };

 AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
 builder.setItems(items, new DialogInterface.OnClickListener() {

   @Override
   public void onClick(DialogInterface arg0, int arg1) {
     addPhoto();
   }

 });
 builder.show();
}

最核心的就是調(diào)用layout方法,根據(jù)我們measure階段獲得的LayoutParams中的left和top字段,也很好對每個子View進行位置排列。然后判斷在圖片未達到最大值9張時,默認(rèn)最后一張是+號圖片,然后設(shè)置點擊事件,彈出對話框供用戶選擇操作。

Draw
不需要重寫,使用ViewGroup默認(rèn)實現(xiàn)即可。
附上布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="40dp"
android:orientation="vertical" >

<com.sw.demo.widget.NinePhotoView
  android:id="@+id/photoview"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  app:ninephoto_hspace="10dp"
  app:ninephoto_vspace="10dp"
  app:rainbowbar_color="@android:color/holo_blue_bright" >

</com.sw.demo.widget.NinePhotoView>

</LinearLayout>

最后還是加上程序運行的效果圖,今天自定義ViewGroup的講解就這么多了,祝大家每天都有新收獲,每天都有好心情~~~


相關(guān)文章

  • Android AlertDialog多種創(chuàng)建方式案例詳解

    Android AlertDialog多種創(chuàng)建方式案例詳解

    這篇文章主要介紹了Android AlertDialog多種創(chuàng)建方式案例詳解,本篇文章通過簡要的案例,講解了該項技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下
    2021-08-08
  • Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問題及解決

    Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問題及解決

    這篇文章主要介紹了Android Studio全局搜索快捷鍵(Ctrl+Shift+F)失效問題及解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-01-01
  • Android開發(fā)實現(xiàn)的獲取sdcard大小及內(nèi)存大小工具類

    Android開發(fā)實現(xiàn)的獲取sdcard大小及內(nèi)存大小工具類

    這篇文章主要介紹了Android開發(fā)實現(xiàn)的獲取sdcard大小及內(nèi)存大小工具類,涉及Android針對手機硬件SD卡及內(nèi)存相關(guān)操作技巧,需要的朋友可以參考下
    2017-11-11
  • Android用文件存儲數(shù)據(jù)的方法

    Android用文件存儲數(shù)據(jù)的方法

    這篇文章主要為大家詳細(xì)介紹了Android用文件存儲數(shù)據(jù)的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-10-10
  • android視頻截屏&手機錄屏實現(xiàn)代碼

    android視頻截屏&手機錄屏實現(xiàn)代碼

    本篇文章主要介紹了android視頻截屏&手機錄屏實現(xiàn)代碼,具有一定的參考價值,感興趣的小伙伴們可以參考一下。
    2017-07-07
  • Jetpack navigation組件超詳細(xì)講解

    Jetpack navigation組件超詳細(xì)講解

    首先Navigation是一個架構(gòu)組件,因為切換Activity是一個Binder通信的過程,所以Activity是屬于比較重的組件。而Fragment的切換其實只是View的切換,比較輕量級。因此單Activity加Fragment切換成為了比較常見的架構(gòu)方式
    2022-10-10
  • 老生常談Listview中onItemClick中的各個參數(shù)(推薦)

    老生常談Listview中onItemClick中的各個參數(shù)(推薦)

    下面小編就為大家?guī)硪黄仙U凩istview中onItemClick中的各個參數(shù)(推薦)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-04-04
  • android 仿微信聊天氣泡效果實現(xiàn)思路

    android 仿微信聊天氣泡效果實現(xiàn)思路

    微信聊天窗口的信息效果類似iphone上的短信效果,以氣泡的形式展現(xiàn),實現(xiàn)這種效果主要用到ListView和BaseAdapter,配合布局以及相關(guān)素材,接下來為大家介紹下如何實現(xiàn)
    2013-04-04
  • Android后臺模擬點擊探索(附源碼)

    Android后臺模擬點擊探索(附源碼)

    這篇文章主要介紹了Android后臺模擬點擊探索(附源碼),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路

    Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路

    這篇文章主要介紹了Android 判斷網(wǎng)絡(luò)狀態(tài)及開啟網(wǎng)路的相關(guān)資料,在開發(fā)網(wǎng)路狀態(tài)的時候需要先判斷是否開啟之后在提示用戶進行開啟操作,需要的朋友可以參考下
    2017-08-08

最新評論