1.TypedArrayjava
a.官方介紹:TypedArray是一個存放array值的容器,經過Resources.Theme#obtainStyledAttributes()
和Resources#obtainAttributes()
方法來檢索,完成後要調用recycle()
方法。indices用於從obtainStyledAttributes()
獲得的容器中獲取對應的值。linux
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.android
b.經常使用方法:git
obtainAttributes(AttributeSet set, int[] attrs)
obtainStyledAttributes(int[] attrs)
obtainStyledAttributes(int resid, int[] attrs)
obtainStyledAttributes(AttributeSet set, int[] attrs)
obtainStyledAttributes(AttributeSet set, int[] attrs, int defStyleAttr, int defStyleRes)
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()算法
c.應用:在自定義view時,用於獲取自定義屬性app
d.舉例:如下是獲取自定義屬性的步驟佈局
<?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>
複製代碼
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優化post
//優化前
int value = 0;
if(condition) {
value=1;
} else {
value=2;
}
複製代碼
//優化後
int value = condition ? 1 : 2;
複製代碼
//優化前
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.註解替代枚舉優化
緣由:枚舉中的每一個值在枚舉類中都是一個對象,使用枚舉值會比直接使用常量消耗更多的內存this
使用枚舉:
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語言,加上時間緊任務重,學校那邊剛開學也有很多事,所以這段時間真是忙得不可開交,此刻已經回校處理好事情剛休息一下子,這纔有空補上最後一篇實習週記。
在上篇博文的最後貼了幾張部門團建的照片,當時你們在進行體育拓展活動,沒錯被貼紙擋住的就是本人啦!來了鵝廠這麼久終於出了次遠門,能夠說很是激動了!除了在晚宴的抽獎活動中感覺不太好以外,已經能預感到將來幾年都只能作分母了...
雖然實習的結束有些倉促和忙亂,但我知道七月還會再回來,就未曾有何遺憾。在真正成爲社會人的最後這四個月裏,但願能順利畢業、和身邊全部人好好告別、享受最後一段美好的大學時光~接下來若是有讀書計劃的話會繼續更新筆記,至於週記就之後再見啦!