以下是本周的知識(shí)清單:
- TypedArray
- if-else優(yōu)化
- 注解替代枚舉
- 一點(diǎn)小感悟
1.TypedArray
a.官方介紹:TypedArray是一個(gè)存放array值的容器诚亚,通過Resources.Theme#obtainStyledAttributes()
和Resources#obtainAttributes()
方法來檢索,完成后要調(diào)用recycle()
方法午乓。indices用于從obtainStyledAttributes()
得到的容器中獲取對(duì)應(yīng)的值站宗。
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.常用方法:
-
創(chuàng)建:兩種方式
- 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 為什么需要調(diào)用recycle()
c.應(yīng)用:在自定義view時(shí),用于獲取自定義屬性
d.舉例:以下是獲取自定義屬性的步驟
-
創(chuàng)建自定義屬性:創(chuàng)建values/attrs.xml文件益愈,聲明自定義屬性
- 屬性類型:color顏色值梢灭、boolean布爾值、dimesion尺寸值蒸其、float浮點(diǎn)值敏释、integer整型值、string字符串摸袁、fraction百分?jǐn)?shù)钥顽、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類:在構(gòu)造方法中通過TypedArray獲取自定義屬性
public class MyCustomView extends View {
public MyCustomView(Context context, AttributeSet attrs) {
super(context, attrs);
//創(chuàng)建
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優(yōu)化
- 使用條件三目運(yùn)算符
//優(yōu)化前
int value = 0;
if(condition) {
value=1;
} else {
value=2;
}
//優(yōu)化后
int value = condition ? 1 : 2;
- 異常/return/continue/break語句前置
//優(yōu)化前
if(condition) {
// TODO somethings
} else {
return;
}
//優(yōu)化后
if(!condition) {
return;
}
// TODO somethings
- 使用表驅(qū)動(dòng)法:直接修改表靠汁,而不是增加新的分支
//優(yōu)化前
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();
}
//優(yōu)化后
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);
- 抽象出另一個(gè)方法
//優(yōu)化前
public void fun1() {
if(condition1) {
// TODO sometings1
if(condition2) {
// TODO something2
if(condition3) {
// TODO something3
}
}
}
// TODO something4
}
//優(yōu)化后
public void fun1() {
fun2();
// TODO something4
}
private void fun2() {
if(!condition1) {
return;
}
// TODO sometings1
if(!condition2) {
return;
}
// TODO something2
if(!condition3) {
return;
}
// TODO something3
}
-
使用策略模式+工廠模式:把具體的算法封裝到了具體策略類內(nèi)部蜂大,增強(qiáng)可擴(kuò)展性闽铐,隱蔽實(shí)現(xiàn)細(xì)節(jié)
- 抽象策略類:接口或抽象類,包含所有具體策略類所需的接口
- 具體策略類:繼承或?qū)崿F(xiàn)抽象策略類奶浦,實(shí)現(xiàn)抽象方法
- 策略調(diào)度與執(zhí)行者:持有一個(gè)對(duì)象的引用兄墅,并執(zhí)行對(duì)應(yīng)策略(用工廠實(shí)現(xiàn))
3.注解替代枚舉
原因:枚舉中的每個(gè)值在枚舉類中都是一個(gè)對(duì)象,使用枚舉值會(huì)比直接使用常量消耗更多的內(nèi)存
使用枚舉:
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.注解枚舉财喳,可選擇項(xiàng)@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;
}
相關(guān)基礎(chǔ):
4.一點(diǎn)小感悟
回校前的一周多開始寫新版本新需求扎瓶,和另一個(gè)iOS開發(fā)的畢業(yè)生做同個(gè)需求,帶我倆的是一個(gè)iOS大佬泌枪,于是不可避免還要接觸Object-C語言概荷,加上時(shí)間緊任務(wù)重,學(xué)校那邊剛開學(xué)也有不少事碌燕,因此這段時(shí)間真是忙得不可開交误证,此刻已經(jīng)回校處理好事情剛休息一會(huì)兒,這才有空補(bǔ)上最后一篇實(shí)習(xí)周記修壕。
在上篇博文的最后貼了幾張部門團(tuán)建的照片愈捅,當(dāng)時(shí)大家在進(jìn)行體育拓展活動(dòng),沒錯(cuò)被貼紙擋住的就是本人啦慈鸠!來了鵝廠這么久終于出了次遠(yuǎn)門蓝谨,可以說非常激動(dòng)了!除了在晚宴的抽獎(jiǎng)活動(dòng)中感受不太好之外青团,已經(jīng)能預(yù)感到未來幾年都只能做分母了...
雖然實(shí)習(xí)的結(jié)束有些倉促和忙亂譬巫,但我知道七月還會(huì)再回來,就不曾有何遺憾督笆。在真正成為社會(huì)人的最后這四個(gè)月里芦昔,希望能順利畢業(yè)、和身邊所有人好好告別娃肿、享受最后一段美好的大學(xué)時(shí)光~
接下來如果有讀書計(jì)劃的話會(huì)繼續(xù)更新筆記咕缎,至于周記就以后再見啦!