鵝廠實習| 週記(四)
以下是本週的知識清單:
- TypedArray
- if-else優化
- 註解替代列舉
- 一點小感悟
1.TypedArray
a. 官方介紹 :TypedArray是一個存放array值的容器,通過 Resources.Theme#obtainStyledAttributes()
和 Resources#obtainAttributes()
方法來檢索,完成後要呼叫 recycle()
方法。indices用於從 obtainStyledAttributes()
得到的容器中獲取對應的值。
Container for an array of values that were retrieved with {@link Resources.Theme#obtainStyledAttributes(AttributeSet, int[], int, int)}or {@link Resources#obtainAttributes}. Be sure to call {@link #recycle} when done with them.The indices used to retrieve values from this structure correspond to the positions of the attributes given to obtainStyledAttributes.
b.常用方法:
- 建立 :兩種方式
- TypedArray typedArray = getResources().obtainAttributes()
-
obtainAttributes(AttributeSet set, int[] attrs)
-
- TypedArray typedArray = Context.obtainStyledAttributes()
obtainStyledAttributes(int[] attrs) obtainStyledAttributes(int resid, int[] attrs) obtainStyledAttributes(AttributeSet set, int[] attrs) obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
- TypedArray typedArray = getResources().obtainAttributes()
- 使用 :typedArray.getXXX()
getBoolean(int index, boolean defValue) getInteger(int index, boolean defValue) getInt(int index, boolean defValue) getFloat(int index, boolean defValue) getString(int index) getColor(int index, boolean defValue) getFraction(int index, int base, int pbase, boolean defValue) getDimension(int index, float defValue)
- 回收 :typedArray.recycle()
推薦閱讀: TypedArray 為什麼需要呼叫recycle()
c. 應用 :在自定義view時,用於獲取自定義屬性
d. 舉例 :以下是獲取自定義屬性的步驟
- 建立自定義屬性 :建立values/attrs.xml檔案,宣告自定義屬性
- 屬性型別 :color顏色值、boolean布林值、dimesion尺寸值、float浮點值、integer整型值、string字串、fraction百分數、enum列舉值、reference引用資原始檔
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MyView"> <attr name="text" format="string" /> <attr name="textColor" format="color"/> <attr name="textSize" format="dimension"/> </declare-styleable> </resources>
- 自定義View類 :在構造方法中通過TypedArray獲取自定義屬性
public class MyCustomView extends View { public MyCustomView(Context context, AttributeSet attrs) { super(context, attrs); //建立 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.MyView); //使用 String text = typedArray.getString(R.styleable.MyView_text); int textColor = typedArray.getColor(R.styleable.MyView_textColor, 20); float textSize = typedArray.getDimension(R.styleable.MyView_textSize, 0xDD333333); Log.i("MyCustomView", "text = " + text + " , textColor = " + textColor+ " , textSize = " + textSize); //回收 typedArray.recycle(); } ... }
- 在佈局檔案中使用自定義屬性 :注意名稱空間
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" ... <com.example.attrtextdemo.MyCustomView android:layout_width="wrap_content" android:layout_height="wrap_content" app:text="我是自定義屬性" app:textColor="@android:color/black" app:textSize="18sp"/> </LinearLayout>
2.if-else優化
- 使用條件三目運算子
//優化前 int value = 0; if(condition) { value=1; } else { value=2; }
//優化後 int value = condition ? 1 : 2;
- 異常/return/continue/break語句前置
//優化前 if(condition) { // TODO somethings } else { return; }
//優化後 if(!condition) { return; } // TODO somethings
- 使用表驅動法:直接修改表,而不是增加新的分支
//優化前 int key = this.getKey(); int value = 0; if(key==1) { value = 1; } else if(key==2) { value = 2; } else if(key==3) { value = 3; } else { throw new Exception(); }
//優化後 Map map = new HashMap(); map.put(1,1); map.put(2,2); map.put(3,3); int key = this.getKey(); if(!map.containsKey(key)) { throw new Exception(); } int value = map.get(key);
- 抽象出另一個方法
//優化前 public void fun1() { if(condition1) { // TODO sometings1 if(condition2) { // TODO something2 if(condition3) { // TODO something3 } } } // TODO something4 }
//優化後 public void fun1() { fun2(); // TODO something4 } private void fun2() { if(!condition1) { return; } // TODO sometings1 if(!condition2) { return; } // TODO something2 if(!condition3) { return; } // TODO something3 }
- 使用策略模式+工廠模式 :把具體的演算法封裝到了具體策略類內部,增強可擴充套件性,隱蔽實現細節
- 抽象策略類:介面或抽象類,包含所有具體策略類所需的介面
- 具體策略類:繼承或實現抽象策略類,實現抽象方法
- 策略排程與執行者:持有一個物件的引用,並執行對應策略(用工廠實現)
3.註解替代列舉
原因:列舉中的每個值在列舉類中都是一個物件,使用列舉值會比直接使用常量消耗更多的記憶體
使用列舉:
public enum WeekDays{ MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY; }
使用註解:
//1.定義常量或字串 public static final int SUNDAY = 0; public static final int MONDAY = 1; public static final int TUESDAY = 2; public static final int WEDNESDAY = 3; public static final int THURSDAY = 4; public static final int FRIDAY = 5; public static final int SATURDAY = 6; //2.註解列舉,可選擇項@IntDef、@StringDef、@LongDef、StringDef @IntDef({SUNDAY, MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY}) @Retention(RetentionPolicy.SOURCE) public @interface WeekDays {} //3.使用 @WeekDays private static int mCurrentDay = SUNDAY; public static void setCurrentDay(@WeekDays int currentDay) { mCurrentDay = currentDay; }
相關基礎:
- Java 列舉(enum) 詳解7種常見的用法
- Java註解 :詳見鵝廠實習| 週記(二)
- Android註解支援(Support Annotations)
4.一點小感悟
回校前的一週多開始寫新版本新需求,和另一個iOS開發的畢業生做同個需求,帶我倆的是一個iOS大佬,於是不可避免還要接觸Object-C語言,加上時間緊任務重,學校那邊剛開學也有不少事,因此這段時間真是忙得不可開交,此刻已經回校處理好事情剛休息一會兒,這才有空補上最後一篇實習週記。
在上篇博文的最後貼了幾張部門團建的照片,當時大家在進行體育拓展活動,沒錯被貼紙擋住的就是本人啦!來了鵝廠這麼久終於出了次遠門,可以說非常激動了!除了在晚宴的抽獎活動中感受不太好之外,已經能預感到未來幾年都只能做分母了...

git命令思維導圖
雖然實習的結束有些倉促和忙亂,但我知道七月還會再回來,就不曾有何遺憾。在真正成為社會人的最後這四個月裡,希望能順利畢業、和身邊所有人好好告別、享受最後一段美好的大學時光~
接下來如果有讀書計劃的話會繼續更新筆記,至於週記就以後再見啦!

美食空間——第三條“腰帶”