本文的合集已經(jīng)編著成書裹虫,高級Android開發(fā)強(qiáng)化實(shí)戰(zhàn)杖爽,歡迎各位讀友的建議和指導(dǎo)葫松。在京東即可購買:https://item.jd.com/12385680.html
介紹關(guān)于Android的一些有趣的小知識點(diǎn). 本文是第六篇, 歡迎閱讀.
其余第一篇, 第二篇, 第三篇, 第四篇, 第五篇.
1. 控件位置
獲取控件四個(gè)角的位置.
代碼
public static PointF getTopLeftCorner(View view) {
float src[] = new float[8];
float[] dst = new float[]{0, 0, view.getWidth(), 0, 0, view.getHeight(), view.getWidth(), view.getHeight()};
view.getMatrix().mapPoints(src, dst);
PointF cornerPoint = new PointF(view.getX() + src[0], view.getY() + src[1]);
return cornerPoint;
}
public static PointF getTopRightCorner(View view) {
float src[] = new float[8];
float[] dst = new float[]{0, 0, view.getWidth(), 0, 0, view.getHeight(), view.getWidth(), view.getHeight()};
view.getMatrix().mapPoints(src, dst);
PointF cornerPoint = new PointF(view.getX() + src[2], view.getY() + src[3]);
return cornerPoint;
}
public static PointF getBottomLeftCorner(View view) {
float src[] = new float[8];
float[] dst = new float[]{0, 0, view.getWidth(), 0, 0, view.getHeight(), view.getWidth(), view.getHeight()};
view.getMatrix().mapPoints(src, dst);
PointF cornerPoint = new PointF(view.getX() + src[4], view.getY() + src[5]);
return cornerPoint;
}
public static PointF getBottomRightCorner(View view) {
float src[] = new float[8];
float[] dst = new float[]{0, 0, view.getWidth(), 0, 0, view.getHeight(), view.getWidth(), view.getHeight()};
view.getMatrix().mapPoints(src, dst);
PointF cornerPoint = new PointF(view.getX() + src[6], view.getY() + src[7]);
return cornerPoint;
}
2. 異常Error:(216) Apostrophe not preceded by ...
原因是字符串中包含單引號('), 導(dǎo)致, 添加轉(zhuǎn)義符號即可.
<string name="hello_kotlin">Hello Kotlin, I\'m Spike! </string>
3. Editable和String的轉(zhuǎn)換
Editable是EditText使用的字符串格式. 與String可以相互轉(zhuǎn)換.
Editable editable = new SpannableStringBuilder("Pass a string here");
String str = editable.toString();
4. 設(shè)置WRAP_CONTENT和VERTICAL
在Kotlin中, 使用屬性擴(kuò)展時(shí), 需要記住類.
WRAP_CONTENT屬于LayoutParams, VERTICAL屬于LinearLayout.
import android.widget.LinearLayout.LayoutParams
import android.widget.LinearLayout
layoutParams = LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)
orientation = LinearLayout.VERTICAL
5. 刪除Commit
刪除本地的Commit, 使用
git reset --hard HEAD~1
HEAD~1表示前1個(gè), 可以自由設(shè)置數(shù)字.
6. 標(biāo)準(zhǔn)的gitignore
這是標(biāo)準(zhǔn)的gitignore, 默認(rèn)項(xiàng)目會生成, 舊的項(xiàng)目可能沒有, 需要補(bǔ)充.
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
7. String轉(zhuǎn)換16進(jìn)制
DeviceID可能是字母, 會導(dǎo)致解析失敗, 解析前需要轉(zhuǎn)換String為16進(jìn)制數(shù).
// String轉(zhuǎn)換40位16進(jìn)制, 防止DeviceID是字母, @Thx 秋爽&康康
public String toHex(String arg) {
return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}
That's all! Enjoy it!