以下是本週的知識清單:
- 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;
}
相關基礎:
4.一點小感悟
回校前的一週多開始寫新版本新需求,和另一個iOS開發的畢業生做同個需求,帶我倆的是一個iOS大佬,於是不可避免還要接觸Object-C語言,加上時間緊任務重,學校那邊剛開學也有不少事,因此這段時間真是忙得不可開交,此刻已經回校處理好事情剛休息一會兒,這纔有空補上最後一篇實習週記。
在上篇博文的最後貼了幾張部門團建的照片,當時大家在進行體育拓展活動,沒錯被貼紙擋住的就是本人啦!來了鵝廠這麼久終於出了次遠門,可以說非常激動了!除了在晚宴的抽獎活動中感受不太好之外,已經能預感到未來幾年都只能做分母了…
雖然實習的結束有些倉促和忙亂,但我知道七月還會再回來,就不曾有何遺憾。在真正成爲社會人的最後這四個月裏,希望能順利畢業、和身邊所有人好好告別、享受最後一段美好的大學時光~
接下來如果有讀書計劃的話會繼續更新筆記,至於週記就以後再見啦!