鵝廠實(shí)習(xí)| 周記(四)

以下是本周的知識(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.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ù)感到未來幾年都只能做分母了...

git命令思維導(dǎo)圖

雖然實(shí)習(xí)的結(jié)束有些倉促和忙亂譬巫,但我知道七月還會(huì)再回來,就不曾有何遺憾督笆。在真正成為社會(huì)人的最后這四個(gè)月里芦昔,希望能順利畢業(yè)、和身邊所有人好好告別娃肿、享受最后一段美好的大學(xué)時(shí)光~

接下來如果有讀書計(jì)劃的話會(huì)繼續(xù)更新筆記咕缎,至于周記就以后再見啦!

美食空間——第三條“腰帶”
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末料扰,一起剝皮案震驚了整個(gè)濱河市锨阿,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌记罚,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,729評(píng)論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件壳嚎,死亡現(xiàn)場(chǎng)離奇詭異桐智,居然都是意外死亡末早,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,226評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門说庭,熙熙樓的掌柜王于貴愁眉苦臉地迎上來然磷,“玉大人,你說我怎么就攤上這事刊驴∽怂眩” “怎么了?”我有些...
    開封第一講書人閱讀 169,461評(píng)論 0 362
  • 文/不壞的土叔 我叫張陵捆憎,是天一觀的道長舅柜。 經(jīng)常有香客問我,道長躲惰,這世上最難降的妖魔是什么致份? 我笑而不...
    開封第一講書人閱讀 60,135評(píng)論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮础拨,結(jié)果婚禮上氮块,老公的妹妹穿的比我還像新娘。我一直安慰自己诡宗,他們只是感情好滔蝉,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,130評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著塔沃,像睡著了一般蝠引。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上芳悲,一...
    開封第一講書人閱讀 52,736評(píng)論 1 312
  • 那天立肘,我揣著相機(jī)與錄音,去河邊找鬼名扛。 笑死谅年,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的肮韧。 我是一名探鬼主播融蹂,決...
    沈念sama閱讀 41,179評(píng)論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼弄企!你這毒婦竟也來了超燃?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,124評(píng)論 0 277
  • 序言:老撾萬榮一對(duì)情侶失蹤拘领,失蹤者是張志新(化名)和其女友劉穎意乓,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體约素,經(jīng)...
    沈念sama閱讀 46,657評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡届良,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,723評(píng)論 3 342
  • 正文 我和宋清朗相戀三年笆凌,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片士葫。...
    茶點(diǎn)故事閱讀 40,872評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡乞而,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出慢显,到底是詐尸還是另有隱情爪模,我是刑警寧澤,帶...
    沈念sama閱讀 36,533評(píng)論 5 351
  • 正文 年R本政府宣布荚藻,位于F島的核電站屋灌,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏鞋喇。R本人自食惡果不足惜声滥,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,213評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望侦香。 院中可真熱鬧落塑,春花似錦、人聲如沸罐韩。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,700評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽散吵。三九已至龙考,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間矾睦,已是汗流浹背晦款。 一陣腳步聲響...
    開封第一講書人閱讀 33,819評(píng)論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留枚冗,地道東北人缓溅。 一個(gè)月前我還...
    沈念sama閱讀 49,304評(píng)論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像赁温,于是被迫代替她去往敵國和親坛怪。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,876評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容